Forum Moderators: coopster

Message Too Old, No Replies

For loop woes

meh

         

supermanjnk

4:24 am on Jan 21, 2005 (gmt 0)

10+ Year Member



Alright first off i'm not sure exactly how to explain this... I have a this forloop which woks, it's for a javascript menu, that pulls it's title from mysql.
In my database I have an auto incrementing column called article. I'd like to find a way to make it so that the ./ on this line:
echo"\n['$field3', './', null],";
can be the article number that goes with the the article, I hope this make sense, I'm not the best with explaining when it comes to php and what i want it to do... I need it to pull the article number, and the title into two seperate variables, but somehow keep them together... if you don't understand i'll try and clarify it more...

<?
mysql_select_db($db , $link) or die("Couldn't open $db: ".mysql_error());
$result = mysql_query( "SELECT DISTINCT title FROM contentdb WHERE title = cat AND enabled='1'" ) or die("SELECT Error: ".mysql_error());
while ($get_info = mysql_fetch_row($result)){
foreach ($get_info as $field)
print "<h2>$field</h2>\n";
print"<script language='Javascript'>\n";
print"var TREE_NODES = [\n";

$result2 = mysql_query( "SELECT DISTINCT title FROM contentdb WHERE cat='$field' AND subcat1 = title AND enabled='1'" ) or die("SELECT Error: ".mysql_error());
while ($get_info2 = mysql_fetch_row($result2)){
foreach ($get_info2 as $field2)
print "['$field2', './', null, ";

$result3 = mysql_query( "SELECT DISTINCT title FROM contentdb WHERE subcat2='$field2' AND enabled='1' " ) or die("SELECT Error: ".mysql_error());
while ($get_info3 = mysql_fetch_row($result3)) {
foreach ($get_info3 as $field3)
echo"\n['$field3', './', null],";
}
print"],\n";
}
echo"];";
?>
</script>
<script language='JavaScript'>
var tree<? echo $field{0};?> = new COOLjsTreePRO('menu<? echo $field{0};?>', TREE_NODES, TREE_FORMAT);
</script>
<script language='JavaScript'>
tree<? echo $field{0};?>.init();
tree<? echo $field{0};?>.expandNode(<? echo $field{0};?>);
</script>
<script language='JavaScript'>
RedrawAllTrees()
</script>
<?
}
?>

grandpa

7:50 am on Jan 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



auto incrementing column called article.

..SELECT DISTINCT title FROM..

You won't get the column article with your SELECT statement. It is currently only returning title.

You might try: SELECT DISTINCT title, article FROM

supermanjnk

7:33 pm on Jan 21, 2005 (gmt 0)

10+ Year Member



Thanks, I've got it working now, with help from what you said, plus a few other changes.

few more questions.

so now my links look like
widgets.com/articles/index.php?article=1
where 1 is the number of the article so 1 equals a number. What would i need to do make it so that when you click on one of the links, it will pull the article from the link, and user it in an sql query to pull the content from the database with the article number equal to that of the link. So the above link would pull article 1. hope this makes sense

<?
mysql_select_db($db , $link) or die("Couldn't open $db: ".mysql_error());
$result = mysql_query( "SELECT DISTINCT title FROM contentdb WHERE title = cat AND enabled='1'" ) or die("SELECT Error: ".mysql_error());
while ($get_info = mysql_fetch_object($result)){
print "<h2>$get_info->title</h2>\n";
print"<script language='Javascript'>\n";
print"var TREE_NODES = [\n";

$result2 = mysql_query( "SELECT DISTINCT title, article FROM contentdb WHERE cat='$get_info->title' AND subcat1 = title AND enabled='1'" ) or die("SELECT Error: ".mysql_error());
while ($get_info2 = mysql_fetch_object($result2)){
print "['$get_info2->title', '/articles/index.php?article=$get_info2->article', null, ";

$result3 = mysql_query( "SELECT DISTINCT title, article FROM contentdb WHERE subcat2='$get_info2->title' AND enabled='1' " ) or die("SELECT Error: ".mysql_error());
while ($get_info3 = mysql_fetch_object($result3)) {
echo"\n['$get_info3->title', '/articles/index.php?article=$get_info3->article', null],";
}
print"],\n";
}
echo"];";
?>
</script>
<script language='JavaScript'>
var tree<? echo $get_info->title{0};?> = new COOLjsTreePRO('menu<? echo $get_info->title{0};?>', TREE_NODES, TREE_FORMAT);
</script>
<script language='JavaScript'>
tree<? echo $get_info->title{0};?>.init();
tree<? echo $get_info->title{0};?>.expandNode(<? echo $get_info->title{0};?>);
</script>
<script language='JavaScript'>
RedrawAllTrees()
</script>
<?
}
?>