SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict Huscy's Avatar
    Join Date
    Jul 2002
    Location
    Newcastle, UK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Strange problem - variable s changing mid-line??

    heres the code...
    PHP Code:
    mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error());
    mysql_select_db($db_name) or die(mysql_error());
    $cat_sql mysql_query("select distinct category from $db_forumindex") or die(mysql_error());

            for(
    $i=0$i mysql_num_rows($cat_sql); $i++)
            {
                while(
    $row mysql_fetch_array($cat_sql))
                {
                echo(
    "<br>Category: ".$row[$i]."<br>");
                    
                    
    $display_forum_sql "select forum_name from $db_forumindex where category = \"$row[$i]\"";
                    
    $display_forum mysql_query($display_forum_sql) or die(mysql_error());
                    for(
    $j=0$j mysql_num_rows($display_forum); $j++)
                    {
                        while(
    $row2 mysql_fetch_array($display_forum))
                        {
                            echo(
    "Forum: <a href=index.php?f=".$j.">".$row2[$j]."</a><br>");
                        }
                    }
                }        
            } 
    the problem is on the line:
    echo("Forum: <a href=index.php?f=".$j.">".$row2[$j]."</a><br>");

    that line is printing this:
    <br><br>Category: Discussion<br>Forum: <a href=index.php?f=0>General Discussion</a><br>Forum: <a href=index.php?f=0>Sport Discussion</a><br><br>Category: Rants<br>Forum: <a href=index.php?f=0>Argument forum</a><br>Forum: <a href=index.php?f=0>Milk Arguments!</a><br>


    as you can see, every time it loops through the $j is incrementing in one part (displaying the forum name) but seemingly not in the f=$j part, where it is printing f=0 every time


    ..... am confused

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because you have both
    for($j=0; $j < mysql_num_rows($display_forum); $j++) ...
    and
    while($row2 = mysql_fetch_array($display_forum))....

    ...which looks totally wrong

    What's the purpose of $j ?
    You use it as forum id in ...index.php?f=".$j... and as array index in ...$row2[$j]... !?

    Shouldn't the code look like this?
    Code:
    $display_forum_sql = "select forum_id, forum_name 
      from $db_forumindex 
      where category = \"$row[$i]\"";
    $display_forum = mysql_query($display_forum_sql) or die(mysql_error());
    while($row2 = mysql_fetch_array($display_forum))
    {
      echo("Forum: <a href=index.php?f=".$row2[0].">".$row2[1]."</a><br>" );
    }

  3. #3
    SitePoint Addict Huscy's Avatar
    Join Date
    Jul 2002
    Location
    Newcastle, UK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    erm i dont think so, i want to display all the possible forums, so $row2[1] would only display one, and $row[0] would only show one as well, i want to display all possible forums for a category, which then link to f= their forum id (which is 0,1,2,3,etc.)

    currently it prints out this:

    Category: Discussion
    Forum: General Discussion
    Forum: Sport Discussion

    Category: Rants
    Forum: Argument forum
    Forum: Milk Arguments!

    so as you see, the $j is working nicely where it is used as $row2[$j], however f=$j is printing f=0 EVERY time, the variable $j seems to be printing different values on the same line

  4. #4
    SitePoint Addict Huscy's Avatar
    Join Date
    Jul 2002
    Location
    Newcastle, UK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You use it as forum id in ...index.php?f=".$j... and as array index in ...$row2[$j]... !?
    yes, i would like to have a list like
    <a href=index.php?f=0>forum with id=0</a>
    <a href=index.php?f=1>forum with id=1</a>
    <a href=index.php?f=2>forum with id=2</a>

    etc. does this make it any clearer?

  5. #5
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then please explain the for loop
    If you want to add the forum id to the url, shouldn't you get the correct id from the db, not from a variable iterating from 0 to N?

  6. #6
    SitePoint Addict Huscy's Avatar
    Join Date
    Jul 2002
    Location
    Newcastle, UK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol probably, but now the problem has arisen i want to know what is wrong with it (not the logic, the actual code)

    but in any case surely that method should work regardless, seeing as the forum ids are 0-3 anyway??

  7. #7
    SitePoint Addict Huscy's Avatar
    Join Date
    Jul 2002
    Location
    Newcastle, UK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its ok, i just changed it to
    Code:
    		for($i=0; $i < mysql_num_rows($cat_sql); $i++)
    		{
    			while($row = mysql_fetch_array($cat_sql))
    			{
    			echo("<br>Category: ".$row[$i]."<br>");	
    			$display_forum_sql = "select * from $db_forumindex where category = \"$row[$i]\"";
    			$display_forum = mysql_query($display_forum_sql) or die(mysql_error());
    				while($row2 = mysql_fetch_array($display_forum))
    				{
    				echo("Forum: <a href=index.php?f=".$row2['forum_id'].">".$row2['forum_name']."</a><br>");
    				}
    			}		
    		}
    and it works - thanks for the help tho


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •