...

View Full Version : Comparing a variable against an array and outputting data from a directly proportiona



psykx
11-12-2007, 03:34 PM
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.


$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



while ( $f_num > 0 )
{
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.

phpBuddy
11-12-2007, 07:19 PM
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

// 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++;
}

?>

psykx
11-13-2007, 01:02 AM
a shorter way of doing the same is


while ( $f_num >= 0 )
{
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum