Re: mysql php - while loops

2002-02-27 Thread DL Neil

Craig,
[BTW there is a set of PHP lists, including PHP-DB - mentioned because 'purists' 
sometimes object to 'PHP
questions'!?]

> >>The following lists 12 items from a fruits table.
> >>
> >>$results = mysql_query("SELECT ID, date, appleprice, orangeprice, pearprice
> >>FROM fruits");
> >>while ($data = mysql_fetch_array($results))
> >>{
> >>?>
> >> -  -
> >> - 
> >> >>}
> >>
> >>How would I modify the loop to display 4 items then a row of fruit images
> >>then 4 more items then another row of different fruit images and then
> >>finally finish the loop displaying the final 4 items?
> >
> >That's a good question, given that the query doesn't retrieve any images,
> >nor is it evident where else they might come from. :-)
> >
> >But it sounds like you're talking about three loops, not one.
>
> if you decide to display only first 4 item try to used limit clause after
> the SQL statement.  otherwise use any count variable within your loop.


Performing multiple hits against MySQL is likely to be much more expensive that 
manipulating within PHP. Make
one query and be done!

Am a little surprised at the suggestion of "three loops" - perhaps Paul has spotted 
something that I've missed.

You have not said whether you will only ever have 12 rows in the recordset. You have 
said that you want a
'break' after every four rows of db results. Further that after the final set of four 
rows, there will not be a
'break' (nor by implication will there be one prior to the first set of rows).

If you can count on the "12" then hard-coding becomes possible - but stand away from 
me when I say that because
the pure-code police will strike me down with a lightning bolt. If you can't count on 
the "12", then are you
prepared to have an additional 'break' (line of fruit images) and a single (orphan) 
row in the case of (say) a
13-row resultset?

You say "different fruit images" which may mean that each break row features a 
different set of images (cf the
same image-row each time). It has already been observed that the source of this/these 
is 'unknown'. (I have
(below) pushed out that functionality to a mythical function, to 'bury' the whole 
question)

The current state of the nation:

query db to get resultset
num_rows is 12
while there's still data in the resultset
{
  ECHO row data
}

You could add a simple check for the need to add a 'break line':

query db to get resultset
num_rows is 12
row_ctr = 0
while there's still data in the resultset
{
  ECHO row data
  row_ctr ++
  if ( row_ctr divides evenly by 4 )
fnDisplayFruitImages;
}

I haven't understood why there is a WHILE loop here, but it's your code (and my warped 
mind)! Knowing that there
are 12 rows seems to indicate a FOR loop but I doubt there's any major performance 
difference, so I'll try not
to get snooty about it. The advantage of using a FOR, would be to save those lines 
that set and update row_ctr
(currently appearing in the 'body' of the code) and move them into the FOR command 
(where they do not 'intrude'
on the primary logic of the code).

Trouble is, the above exception condition kicks in after line 12 too - which you don't 
want, so:

  if ( row_ctr divides evenly by 4 AND row_ctr <> 12 )
fnDisplayFruitImages;

Yuk! Or relying upon the "12", even:

  if ( row_ctr = 4 or row_ctr = 8 )
fnDisplayFruitImages;

and then, if I don't mention it, someone else will even point out (that we should 
stick with the WHILE
choice(!!!***???)) and then we can have:

  if ( row_ctr = 4 )
{
  fnDisplayFruitImages;
  row_ctr = 0
}

Which brings us neatly to how to handle the situation if there are more than 12 
rows/we don't have advanced
knowledge of the number of rows that will be in the resultset. The exception portion 
of the last example will
continue to work (according to the above-assumed criteria), no matter how many rows 
you throw at it! Of course,
the code-police would be after us for the equi-relationship, instead insisting upon 
">="; and the
structure-police will be after us because you 'should' reflect the structure more 
'properly' with multiple loops
(at which point I'm going to invoke the '5th', blame it all on you, and claim 
witness-protection):

query db to get resultset
get num_rows as last_row  //put in this change because now we 
don't know how many rows until
we ask
fetch first row//start processing the resultset
row_ctr = 0

while there's still data in the resultset //row_ctr < num_rows***
{
  set_ctr = 0

  while ( set_ctr < 4 AND there's still data in the resultset*** )
  {
ECHO row data
set_ctr ++
fetch next row
  }
  row_ctr += 4
  if ( row_ctr < num_rows ) //or, use 'lookahead', ie ask if there 
is valid data in the
latest row fetched from the resultset
fnDisplayFruitImages;
}

BTW I haven't tested a single line of code (after all this typing you can guess why), 
so E&OE. Please let me
know if you try to implement

Re: mysql php - while loops

2002-02-26 Thread Sommai Fongnamthip

if you decide to display only first 4 item try to used limit clause after 
the SQL statement.  otherwise use any count variable within your loop.

Sommai

At 20:33 26/2/2002 -0600, Paul DuBois wrote:
>At 18:42 -0600 2/26/02, Craig Westerman wrote:
>>The following lists 12 items from a fruits table.
>>
>>$results = mysql_query("SELECT ID, date, appleprice, orangeprice, pearprice
>>FROM fruits");
>>while ($data = mysql_fetch_array($results))
>>{
>>?>
>> -  -
>> - 
>>>}
>>
>>How would I modify the loop to display 4 items then a row of fruit images
>>then 4 more items then another row of different fruit images and then
>>finally finish the loop displaying the final 4 items?
>
>That's a good question, given that the query doesn't retrieve any images,
>nor is it evident where else they might come from. :-)
>
>But it sounds like you're talking about three loops, not one.
>
>>
>>Thanks
>>
>>Craig ><>
>>[EMAIL PROTECTED]
>
>
>-
>Before posting, please check:
>   http://www.mysql.com/manual.php   (the manual)
>   http://lists.mysql.com/   (the list archive)
>
>To request this thread, e-mail <[EMAIL PROTECTED]>
>To unsubscribe, e-mail 
><[EMAIL PROTECTED]>
>Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

--
Please be informed that all e-mail which are addressing to
thaithanakit.co.th will need to be changed to
BTsecurities.com by March 1, 2002 Thank you. :-)
--

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: mysql php - while loops

2002-02-26 Thread Paul DuBois

At 18:42 -0600 2/26/02, Craig Westerman wrote:
>The following lists 12 items from a fruits table.
>
>$results = mysql_query("SELECT ID, date, appleprice, orangeprice, pearprice
>FROM fruits");
>while ($data = mysql_fetch_array($results))
>{
>?>
> -  -
> - 
>}
>
>How would I modify the loop to display 4 items then a row of fruit images
>then 4 more items then another row of different fruit images and then
>finally finish the loop displaying the final 4 items?

That's a good question, given that the query doesn't retrieve any images,
nor is it evident where else they might come from. :-)

But it sounds like you're talking about three loops, not one.

>
>Thanks
>
>Craig ><>
>[EMAIL PROTECTED]


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




mysql php - while loops

2002-02-26 Thread Craig Westerman

The following lists 12 items from a fruits table.

$results = mysql_query("SELECT ID, date, appleprice, orangeprice, pearprice
FROM fruits");
while ($data = mysql_fetch_array($results))
{
?>
 -  -
 - 
<>
[EMAIL PROTECTED]


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php