Re: [PHP] Re: Stepping through an array more than once (offlist)

2002-08-29 Thread Justin French

on 29/08/02 5:06 PM, Petre Agenbag ([EMAIL PROTECTED]) wrote:

> Jason
> Thanks
> the mysql_data_seek() works.
> With Justin's help, I used the mysql_data_seek() to reset the $result
> "matrix" to 0, and re-issued the same while statement.
> It now produces the desired output.
> Thanks.

Of course I only figured out what you needed by seeing Jason's post, going
to the manual, reading the documentation, and guessing what needed to be
done... something you could have done yourself :)


Justin


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Re: Stepping through an array more than once (offlist)

2002-08-28 Thread Petre Agenbag

Jason
Thanks
the mysql_data_seek() works.
With Justin's help, I used the mysql_data_seek() to reset the $result
"matrix" to 0, and re-issued the same while statement.
It now produces the desired output.
Thanks.

On Thu, 2002-08-29 at 08:46, Jason Wong wrote:
> On Thursday 29 August 2002 14:16, Petre Agenbag wrote:
> > 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).
> 
> [snip]
> 
> I've said it once already:
> 
>   mysql_data_seek()
> 
> -- 
> Jason Wong -> Gremlins Associates -> www.gremlins.com.hk
> Open Source Software Systems Integrators
> * Web Design & Hosting * Internet & Intranet Applications Development *
> 
> /*
> Heller's Law:
>   The first myth of management is that it exists.
> 
> Johnson's Corollary:
>   Nobody really knows what is going on anywhere within the
>   organization.
> */
> 
> 
> -- 
> 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] Re: Stepping through an array more than once (offlist)

2002-08-28 Thread Petre Agenbag

Jason
Sorry, I completely forgot to comment on your suggestion:

I did try mysql_data_seek(), but it didn't work for me ( guess I don't
know how exactly it works), BUT, it did look like it was meant for
stepping through a one dimensional array, and $result is not a one
dimensional array, right?
Maybe I'm just not thinking correctly, 
Justin has also just commented on your suggestion and he seems to give
some sort of an "implementation" of using mysql_data_seek(), maybe it
will explain it a better for me, I'll quickly see if I can get it to
work.

Thanks for the input!

On Thu, 2002-08-29 at 08:46, Jason Wong wrote:
> On Thursday 29 August 2002 14:16, Petre Agenbag wrote:
> > 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).
> 
> [snip]
> 
> I've said it once already:
> 
>   mysql_data_seek()
> 
> -- 
> Jason Wong -> Gremlins Associates -> www.gremlins.com.hk
> Open Source Software Systems Integrators
> * Web Design & Hosting * Internet & Intranet Applications Development *
> 
> /*
> Heller's Law:
>   The first myth of management is that it exists.
> 
> Johnson's Corollary:
>   Nobody really knows what is going on anywhere within the
>   organization.
> */
> 
> 
> -- 
> 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




[PHP] Re: Stepping through an array more than once (offlist)

2002-08-28 Thread Justin French

1. did my code work?





2. It appears that you have to reset the mysql_fetch_*(), not reset the
$myrow. Try calling:

mysql_data_seek($result_it, 0)

before

while ($myrow = mysql_fetch_assoc($result))

It would appear that it resets the $result_it for a call to
mysql_fetch_row().  I would assume that it would also work for
mysql_fetch_array() and mysql_fetch_assoc()

Worth a try.


Justin



on 29/08/02 4:16 PM, Petre Agenbag ([EMAIL PROTECTED]) wrote:

> 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 "{$heading}";
>> 
>> $sql = "SELECT * FROM tickets WHERE status='{$type}'";
>> $result = mysql_query($sql);
>> if(!$result) 
>> {
>> echo "There was a database error: " . mysql_error() . "";
>> }
>> 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}";
>> }
>> echo "Total New: {$total}";
>> }
>> }
>> ?>
>> 
>> 
>> 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 advantag

Re: [PHP] Re: Stepping through an array more than once (offlist)

2002-08-28 Thread Jason Wong

On Thursday 29 August 2002 14:16, Petre Agenbag wrote:
> 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).

[snip]

I've said it once already:

  mysql_data_seek()

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *

/*
Heller's Law:
The first myth of management is that it exists.

Johnson's Corollary:
Nobody really knows what is going on anywhere within the
organization.
*/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Re: Stepping through an array more than once (offlist)

2002-08-28 Thread Petre Agenbag

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 "{$heading}";
> 
> $sql = "SELECT * FROM tickets WHERE status='{$type}'";
> $result = mysql_query($sql);
> if(!$result) 
> {
> echo "There was a database error: " . mysql_error() . "";
> }
> 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}";
> }
> echo "Total New: {$total}";
> }
> }
> ?>
> 
> 
> 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