Hi Justin
OK, a quick feedback on your previous suggestion:

I tried to unset the $myrow_it, but it still didn't produce any output.
The only way I could get it to work was with the same method you
suggested in this e-mail.
I had to create 2 new vars ( which basically boils down to 2 more
SQL's).

I think I'm not 100% understanding what the $result variable contains
$sql = 'select * from table';
$result = mysql_query($sql);

As I would think, $result contains the entire table in some sort of
"structure" or matrix right?
And doing a
 while ($myrow = mysql_fetch_assoc($result))

creates a one dimensional array of each row in the table with each pass.
I was just looking for a way to step through $result again and again,
without having to query the DB again...
But the only way I can get it to work is to do:
$result2 = mysql_query($sql);
$result3 = mysql_query($sql);

and then I can

while ($myrow = mysql_fetch_assoc($result2))


and later 

while ($myrow = mysql_fetch_assoc($result3))

It's like the contents of $result can only be used once...Is that the
case?

I tried to do 
$test = $result; immediately after doing the query and then using $test
in the while , but that doesn't work either...

Anyway, not a train smash, will carry on with the trusty old ways.


 


On Wed, 2002-08-28 at 18:30, Justin French wrote:
> Hi,
> 
> I haven't got a heap of time tonight, but you should really look at arrays.
> What I noticed with your code was that you basically did the same thing
> (same layout and presentation three times -- and it might be 50 times
> later!!).
> 
> This sort of thing cries out for an array or function -- or both!!
> 
> Let's start by recognising your 3 loops: OPEN, CURRENT, OLD.  Arrays come in
> many shapes, but generally a one-dimensional array is in the form of 'key'
> => 'value'.  In this case, I've chosen to use the three types of status as
> the key, and the natural language or heading as the value.
> 
> <?
> $ticketTypes = array(
>     'OPEN' => 'New Tickets',
>     'CURRENT => 'Current Tickets',
>     'OLD' => 'Old Tickets'
>     );
> ?>
> 
> Then you can loop through this array with a foreach, and do the same thing
> to all three elements.
> 
> Now, we have to take the $type (key) and $heading (value) of each array
> element (the three options) and use them within the loop to produce the
> different results.
> 
> <?
> 
> // array of ticket types
> $ticketTypes = array(
>     'OPEN' => 'New Tickets',
>     'CURRENT => 'Current Tickets',
>     'OLD' => 'Old Tickets'
>     );
> 
> 
> foreach($ticketTypes as $type => $heading)
>     {
>     // print heading
>     echo "<B>{$heading}</B><BR>";
> 
>     $sql = "SELECT * FROM tickets WHERE status='{$type}'";
>     $result = mysql_query($sql);
>     if(!$result) 
>         {
>         echo "There was a database error: " . mysql_error() . "<BR>";
>         }
>     else
>         {
>         // no need to to do an $i++ count... just use this:
>         $total = mysql_num_rows($result);
>         while ($myrow = mysql_fetch_array($result))
>             {
>             // a little trick i have to make each column
>             // name (eg "status") into it's own var (eg $status)
>             foreach($myrow as $k => $v) { $$k = $v; }
>             echo "{$company} :: {$title} :: {$content}<br>";
>             }
>         echo "Total New: {$total}<br>";
>         }
>     }
> ?>
> 
> 
> Now, I haven't tested the above, so gimmie a yell if it breaks...
> 
> When I strip all the bullshit and comments out, and condense the script down
> to it rawest form, it's like 19 lines of code -- that's including error
> handling and all sorts of stuff!!  And it will work for 2 ticket types
> (still less lines of code than your 40+) and for 50 ticket types -- just
> make the array bigger.
> 
> The other point is, if this script was called for many different pages, then
> you could include it all in a function... but that's for another day!
> 
> 
> Sure, you're doing three sql queries, but each one of them is returning a
> more focused result set... I'd have to do some tests, but I reckon there's
> very little difference between ONE QUERY + MASSIVE AMOUNTS OF PHP CODING
> if() sections etc etc, versus THREE QUERIES + SOME CODING.  The nature of
> what you want to achieve with this script is conducive to using three
> separate queries.
> 
> I know less queries == faster, but it's not always the case, and not always
> worth worrying about, unless you've got a HUGE site with millions of hits a
> day... you just won't notice the benefit, in comparison to the advantage.
> 
> 
> Some pages on a site I'm working on right now, hinge.net.au, have 5-10
> queries on them, for sessions, users, content, data, counters, logging,
> message boards, etc etc.  And I've NEVER noticed a performance problem, or
> got any complaints.
> 
> 
> When you've got maybe 500 rows in there (or 5,000, or 50,000), it'd be nice
> to run an a/b test with a script timer, and get some averages to find out
> definitively.
> 
> 
> Enjoy,
> 
> Justin
> 
> 
> 
> on 28/08/02 11:16 PM, Petre Agenbag ([EMAIL PROTECTED]) wrote:
> 
> > Justin, 
> > Thanks for the reply.
> > I frequently see your comments on threads here, and don't worry, I
> > wouldn't take any as insults as I can see you want to help, and I
> > appreciate that!
> > 
> > So, you are more than welcome to show me alternatives, I am by no means
> > an ace programmer, and I eagerly lap up all comments and suggestions.
> > 
> > I will try the suggestion, and will report back.
> > 
> > Just to sort of explain my thinking process.
> > I would think that limiting queries to a db to the absolute minimum
> > would be the "best", BUT, I can now see that there are no easy way, as
> > the IF iterations clearly show...
> > 
> > I would me very interested in your suggestions as to "cleaning" up the
> > code, for I have seen plenty of examples where you can write the same
> > functionality in many many different ways, and the only difference is
> > normally in the way people think about the problem and the solution.
> > It can't be taught,and will only come with experience, which I can
> > hopefully get a head start on with this list...
> > 
> > Thanks alot.
> > 
> > On Wed, 2002-08-28 at 14:52, Justin French wrote:
> >> 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

Reply via email to