SV: [PHP-DB] Current row of query
Actually you dont need to keep track of the row number. Yoy could just alternate the two colors like this: while ( $row = mysql_fetch_array($result) ) { $bgCol = ( $bgCol == #EADBC6}?#EFE1CE:#EADBC6; echo ..; } hth Henrik Hornemann -Oprindelig meddelelse- Fra: Patrick Lebon [mailto:[EMAIL PROTECTED]] Sendt: 26. september 2002 17:20 Til: [EMAIL PROTECTED] Emne: Re: [PHP-DB] Current row of query Thanks, im currently doing something similar to this but I was wondering if there was an already defined variable that i could use rather then have to create a value and increment it myself. I guess am being picky, but was just curious as im new to php. This is how im currently doing it... $rowNum = 0; while ( $row = mysql_fetch_array($result) ) { $rowNum++; if ($rowNum % 2) { $bgCol = #EADBC6; } else { $bgCol = #EFE1CE; } echo ..; } -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DB] Current row of query
Is there a mysql or array variable for the current row of a query array? I want to alternate background colours for each row of the query output so i need to know the current row number. Thanks -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Current row of query
oops... I meant: (if $i is odd) ... because $i would be the current index in the array. Brad Bonkoski wrote: why not this? $result = mysql_query(Select * from table'); $num_rows = mysql_num_rows($rows); for ($i=0; $i$num_rows, $i++) { $row = mysql_fetch_array($result); if ($num_rows is odd) { print $row with BG color of red; } else { printf $row with BG color of purple; } } HTH -Brad Patrick Lebon wrote: Is there a mysql or array variable for the current row of a query array? I want to alternate background colours for each row of the query output so i need to know the current row number. Thanks -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Current row of query
why not if (!($1%2)) { echo even!; } else { echo odd!; } Adam On Thu, 26 Sep 2002, Brad Bonkoski wrote: oops... I meant: (if $i is odd) ... because $i would be the current index in the array. Brad Bonkoski wrote: why not this? $result = mysql_query(Select * from table'); $num_rows = mysql_num_rows($rows); for ($i=0; $i$num_rows, $i++) { $row = mysql_fetch_array($result); if ($num_rows is odd) { print $row with BG color of red; } else { printf $row with BG color of purple; } } HTH -Brad Patrick Lebon wrote: Is there a mysql or array variable for the current row of a query array? I want to alternate background colours for each row of the query output so i need to know the current row number. Thanks -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Current row of query
Not sure here, but you might also run into a problem if you start your if statement with $i=0. If PHP does not treat the value 0 as even or odd, your first row is most likely going to be purple along with your second row. I know it's nitpicky, but I could see myself spending half an hour trying to figure out why the code works, but not the way I want it to. -Original Message- From: Brad Bonkoski [mailto:[EMAIL PROTECTED]] Sent: Thursday, September 26, 2002 11:10 AM To: Patrick Lebon; [EMAIL PROTECTED] Subject: Re: [PHP-DB] Current row of query oops... I meant: (if $i is odd) ... because $i would be the current index in the array. Brad Bonkoski wrote: why not this? $result = mysql_query(Select * from table'); $num_rows = mysql_num_rows($rows); for ($i=0; $i$num_rows, $i++) { $row = mysql_fetch_array($result); if ($num_rows is odd) { print $row with BG color of red; } else { printf $row with BG color of purple; } } HTH -Brad Patrick Lebon wrote: Is there a mysql or array variable for the current row of a query array? I want to alternate background colours for each row of the query output so i need to know the current row number. Thanks -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Current row of query
Thanks, im currently doing something similar to this but I was wondering if there was an already defined variable that i could use rather then have to create a value and increment it myself. I guess am being picky, but was just curious as im new to php. This is how im currently doing it... $rowNum = 0; while ( $row = mysql_fetch_array($result) ) { $rowNum++; if ($rowNum % 2) { $bgCol = #EADBC6; } else { $bgCol = #EFE1CE; } echo ..; } Brad Bonkoski [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... why not this? $result = mysql_query(Select * from table'); $num_rows = mysql_num_rows($rows); for ($i=0; $i$num_rows, $i++) { $row = mysql_fetch_array($result); if ($num_rows is odd) { print $row with BG color of red; } else { printf $row with BG color of purple; } } HTH -Brad Patrick Lebon wrote: Is there a mysql or array variable for the current row of a query array? I want to alternate background colours for each row of the query output so i need to know the current row number. Thanks -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Current row of query
then add an extra conditional to check if $i is equal to 0, or start $ off as 1so, for ($i=1, $i$num_rows+1; $i++) -or- if ($i==0) //treat as even either way woudl work. -Brad Hutchins, Richard wrote: Not sure here, but you might also run into a problem if you start your if statement with $i=0. If PHP does not treat the value 0 as even or odd, your first row is most likely going to be purple along with your second row. I know it's nitpicky, but I could see myself spending half an hour trying to figure out why the code works, but not the way I want it to. -Original Message- From: Brad Bonkoski [mailto:[EMAIL PROTECTED]] Sent: Thursday, September 26, 2002 11:10 AM To: Patrick Lebon; [EMAIL PROTECTED] Subject: Re: [PHP-DB] Current row of query oops... I meant: (if $i is odd) ... because $i would be the current index in the array. Brad Bonkoski wrote: why not this? $result = mysql_query(Select * from table'); $num_rows = mysql_num_rows($rows); for ($i=0; $i$num_rows, $i++) { $row = mysql_fetch_array($result); if ($num_rows is odd) { print $row with BG color of red; } else { printf $row with BG color of purple; } } HTH -Brad Patrick Lebon wrote: Is there a mysql or array variable for the current row of a query array? I want to alternate background colours for each row of the query output so i need to know the current row number. Thanks -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Current row of query
Last I checked there was no way to retrieve the current index into the result array, you can set the index manually with mysql_seek_data(), but I don't think that is what you want, so the best solution IMHO, is to do what you are doing and manually keep track of the index. -Brad Patrick Lebon wrote: Thanks, im currently doing something similar to this but I was wondering if there was an already defined variable that i could use rather then have to create a value and increment it myself. I guess am being picky, but was just curious as im new to php. This is how im currently doing it... $rowNum = 0; while ( $row = mysql_fetch_array($result) ) { $rowNum++; if ($rowNum % 2) { $bgCol = #EADBC6; } else { $bgCol = #EFE1CE; } echo ..; } Brad Bonkoski [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... why not this? $result = mysql_query(Select * from table'); $num_rows = mysql_num_rows($rows); for ($i=0; $i$num_rows, $i++) { $row = mysql_fetch_array($result); if ($num_rows is odd) { print $row with BG color of red; } else { printf $row with BG color of purple; } } HTH -Brad Patrick Lebon wrote: Is there a mysql or array variable for the current row of a query array? I want to alternate background colours for each row of the query output so i need to know the current row number. Thanks -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Current row of query
-Original Message- From: Patrick Lebon [mailto:[EMAIL PROTECTED]] Sent: 26 September 2002 16:20 This is how im currently doing it... $rowNum = 0; while ( $row = mysql_fetch_array($result) ) { $rowNum++; if ($rowNum % 2) { $bgCol = #EADBC6; } else { $bgCol = #EFE1CE; } echo ..; } Looks like a good 'un to me, although I wonder why you're incrementing $rowNum and then doing a separate access to it, when the ++ operator is designed precisely to avoid the need for this; the following is identical in functionality to the above: $rowNum = 0; while ( $row = mysql_fetch_array($result) ) { if ($rowNum++ % 2) { $bgCol = #EADBC6; } else { $bgCol = #EFE1CE; } echo ..; } And, having got this far, I'd probably then go on to rewrite the if line using the ?: operator instead: $bgCol = ($rowNum++ % 2) ? #EADBC6 : #EFE1CE; Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Current row of query
You can use instead of %, and it may be quicker. $rowNum = 0; while ( $row = mysql_fetch_array($result) ) { $bgcolor = ($rowNum++ 1) ? #EADBC6 : #EFE1CE; ... ---John Holmes... -Original Message- From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED]] Sent: Thursday, September 26, 2002 11:42 AM To: 'Patrick Lebon'; [EMAIL PROTECTED] Subject: RE: [PHP-DB] Current row of query -Original Message- From: Patrick Lebon [mailto:[EMAIL PROTECTED]] Sent: 26 September 2002 16:20 This is how im currently doing it... $rowNum = 0; while ( $row = mysql_fetch_array($result) ) { $rowNum++; if ($rowNum % 2) { $bgCol = #EADBC6; } else { $bgCol = #EFE1CE; } echo ..; } Looks like a good 'un to me, although I wonder why you're incrementing $rowNum and then doing a separate access to it, when the ++ operator is designed precisely to avoid the need for this; the following is identical in functionality to the above: $rowNum = 0; while ( $row = mysql_fetch_array($result) ) { if ($rowNum++ % 2) { $bgCol = #EADBC6; } else { $bgCol = #EFE1CE; } echo ..; } And, having got this far, I'd probably then go on to rewrite the if line using the ?: operator instead: $bgCol = ($rowNum++ % 2) ? #EADBC6 : #EFE1CE; Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Current row of query
Is there a mysql or array variable for the current row of a query array? I want to alternate background colours for each row of the query output so i need to know the current row number. There is a way with SQL variables, but you're better off just letting PHP keep track of it. ---John Holmes... -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Current row of query
Thanks for your help. As i said im quite new to php so all your help in simplifying my code has been appeciated. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php