I have tried to pass a sql_query result ($result) to a function.
Within the function is a WHILE loop that parses out the associative array. Works fine on the first pass.
On the second call to the function, the associtive array pointer appears to be at the end, so there are no results.
I have tried reset() - and tried storing the original resource ID to a new $var - still no good, same resource ID.
Is there any way to reset the pointer of the associtive array (resource ID) to BOF.
The only working solution I have now is to put the SELECT inside the function and hit the DB each time the function is run.... not a good solution.
function get_sub_cats($main_category_id,$result)
{
$select_cats = "SELECT category_id, parent_id, name, items_counter,is_subcat FROM categories" ;
$result = mysql_query($select_cats) or die('could not execute query') ;
while ($sub_cats = mysql_fetch_array($result))
{
if($sub_cats['parent_id']==$main_category_id )
{
echo "<tr>\n
<td></td>\n<td> $sub_cats[name]" ;
if($sub_cats[items_counter]>0)
{
echo " (" . $sub_cats[items_counter] . ")" ;
echo " </td>\n
</tr>\n ";
}
}
}
return;
}
Within the function is a WHILE loop that parses out the associative array. Works fine on the first pass.
On the second call to the function, the associtive array pointer appears to be at the end, so there are no results.
I have tried reset() - and tried storing the original resource ID to a new $var - still no good, same resource ID.
Is there any way to reset the pointer of the associtive array (resource ID) to BOF.
The only working solution I have now is to put the SELECT inside the function and hit the DB each time the function is run.... not a good solution.
function get_sub_cats($main_category_id,$result)
{
$select_cats = "SELECT category_id, parent_id, name, items_counter,is_subcat FROM categories" ;
$result = mysql_query($select_cats) or die('could not execute query') ;
while ($sub_cats = mysql_fetch_array($result))
{
if($sub_cats['parent_id']==$main_category_id )
{
echo "<tr>\n
<td></td>\n<td> $sub_cats[name]" ;
if($sub_cats[items_counter]>0)
{
echo " (" . $sub_cats[items_counter] . ")" ;
echo " </td>\n
</tr>\n ";
}
}
}
return;
}