Re: [PHP] Stepping through an array more than once
Actually, it's not accurate for me to say stepping through an array more than once, as I want to step through the database ( $result) more than once, without having to make connections to the db again. Can this be done? On Wed, 2002-08-28 at 13:58, Petre Agenbag wrote: Hi I would like to do something like the following: I have a table that contains a bunch of info at various stages. To optimize the load on the db, I was thinking it would be better to do a select * from table , and then use PHP to sort through the results, rather than have 3 SQL's with where status=xxx . There are essentially 3 stages or states at which the data in the table can be sorted, so I would need to loop through the array 3 times in order to display the entire contents of the table ordered by the 3 statges. This is my first attempt: $sql_it = 'select * from tickets '; $result_it = mysql_query($sql_it); echo 'New Tickets br'; $count = 0; while ($myrow_it = mysql_fetch_assoc($result_it)) { $status = $myrow_it[status]; if ($status == OPEN) { $company = $myrow_it[company]; $title = $myrow_it[title]; $content = $myrow_it[content]; echo $company.' :: '.$title.' :: '.$content.'br'; $count++; } } echo 'Total New:'.$count.'br'; echo 'Current Tickets br'; while ($myrow_it = mysql_fetch_assoc($result_it)) { $status = $myrow_it[status]; if ($status == CURRENT) { $company = $myrow_it[company]; $title = $myrow_it[title]; $content = $myrow_it[content]; echo $company.' :: '.$title.' :: '.$content.'br'; } } echo 'Old Tickets br'; while ($myrow_it = mysql_fetch_assoc($result_it)) { $status = $myrow_it[status]; if ($status == OLD) { $company = $myrow_it[company]; $title = $myrow_it[title]; $content = $myrow_it[content]; echo $company.' :: '.$title.' :: '.$content.'br'; } } Now, obviously this only echoes the first part, as it seems the array is at the end when it tries to loop through again. I tried a reset($myrow_it) , but it drops an error about $myrow_it not being an array. I'm not sure if my logic here is correct. Is this the way to re-loop the array? Hope I explained what I am trying to do well enough. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Stepping through an array more than once
From: Petre Agenbag [mailto:[EMAIL PROTECTED]] Sent: Wednesday, August 28, 2002 8:17 AM To: Petre Agenbag Cc: [EMAIL PROTECTED] Subject: Re: [PHP] Stepping through an array more than once Actually, it's not accurate for me to say stepping through an array more than once, as I want to step through the database ( $result) more than once, without having to make connections to the db again. Can this be done? Yes, see mysql_data_seek() http://www.php.net/manual/en/function.mysql-data-seek.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Stepping through an array more than once
Try unset($myrow_it). You don't want to reset the array, you want to clear it, then use a new array (with the same name, and the same $result) for another while loop. FWIW, I'm pretty sure I'd just use three distinct queries. Infact, it'd be worth you timing your scripts with both these versions to see what works out quicker -- three queries, or lots of if() statements [remember, you're in a while loop, so 5000 rows * 3 if statements = 15000!!] Put this at the top: ? $show_timer = 1; function getmicrotime() { list($usec, $sec) = explode( ,microtime()); return ((float)$usec + (float)$sec); } $time_start = getmicrotime(); ? And this at the end: ? if($show_timer) { $time_end = getmicrotime(); $timer = $time_end - $time_start; echo Timer: {$timer}BR; } ? Run the script with both versions about 10 times each, and take the average :) There are many many ways I can see that you could clean up and optimise the script you've shown (not intended as an insult AT ALL!) -- quite possibly some of these would make more of a difference to your performance than limiting yourself to just one query... at the very least, it would make updating the script easier, and reduce the lines of code. Good luck, Justin French -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Stepping through an array more than once
On Wednesday 28 August 2002 20:16, Petre Agenbag wrote: Actually, it's not accurate for me to say stepping through an array more than once, as I want to step through the database ( $result) more than once, without having to make connections to the db again. Can this be done? mysql_data_seek() -- Jason Wong - Gremlins Associates - www.gremlins.com.hk Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* I don't know who my grandfather was; I am much more concerned to know what his grandson will be. -- Abraham Lincoln */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php