Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reset associative array

Status
Not open for further replies.

cc4mail

Technical User
Jan 8, 2002
47
US
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;

}




 
try adding this line to the start of the function
Code:
mysql_data_sek($result, 0);

reset is for arrays.
 
data_seek worked perfect!

I spent 7 hours on this yesterday...

you replied in 11 minutes... thanks.

Now, knowing what works, I found a one sentence reference to it in my 1000 page PHP manual....

I need to leave this project to the pros...

thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top