Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Comparing a variable against an array and outputting data from a directly proportiona

    Comparing a variable against an array and outputting data from a directly proportional array I can't see why the code isn't working. I have tried echoing variables at almost all stages but I can't get my head around it.

    This code populates the arrays.
    PHP Code:
    $sqlquery "SELECT forum_name, forum_id FROM phpbb_forums";
    $result $db->sql_query($sqlquery);
    $f_num 0;
    $forumid = array();
    $forumname = array();
       while ( (
    $row $db->sql_fetchrow($result)))
        {
        
    $forumid[$f_num] = $row["forum_id"];
        
    $forumname[$f_num] = $row["forum_name"];
        ++
    $f_num;
        }
    $db->sql_freeresult($result); 
    then another query is performed which is most of the output is generated. then this is the code that I'm having problems with

    PHP Code:
                while ( $f_num )
                {
                    if ( 
    $row["forum_id"] == $forumid[$f_num] )
                    {
                       
    $f_name $forumname[$f_num];
                       
    $f_id_num $f_num;
                    }
                    
    $f_num--;
                } 
    This code needs to replace $row['forum_id'] with the correct variable from the $forumname[] array each time "while ( ($row = $db->sql_fetchrow($result)))" runs.

  • #2
    New Coder
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post
    in the first case $f_num is 0-5
    in the second case $f_num is 5->1. Not = 0.

    In my code below,
    when $f_count is = 5, the indexes will be:
    0,1,2,3,4
    PHP Code:
    <?php

    // a new variable, that holds number of forumid
    $f_count count($forumid);

    $f_num 0;
    while ( 
    $f_num $f_count )
                {
                    if ( 
    $row["forum_id"] == $forumid[$f_num] )
                    {
                       
    $f_name $forumname[$f_num];
                       
    $f_id_num $f_num;
                    }
                    
    $f_num++;
                }

    ?>
    echo "PHP 5.2.5";
    exit ( "Script End" );

  • #3
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    a shorter way of doing the same is
    PHP Code:
    while ( $f_num >= )
    {
    etc...

    this was part of the problem, cheers the next bit is going to be to get the while loop to change a different variable leaving $f_num intact for the next row.


  •  

    Posting Permissions

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