[PHP] Looping through array
Say I have an array containing ten items, and I want to display them in a table as follows: 5 4 3 2 1 10 9 8 7 6 What's the best way to loop through that array to do that? My thinking gets me to create a loop for 5 through 1, repeated twice, and the second time I add '5' to the index value. There's got to be a saner way... -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looping through array
Ashley M. Kirchner wrote: Say I have an array containing ten items, and I want to display them in a table as follows: 5 4 3 2 1 10 9 8 7 6 What's the best way to loop through that array to do that? My thinking gets me to create a loop for 5 through 1, repeated twice, and the second time I add '5' to the index value. There's got to be a saner way... Something like this perhaps... $arr = array(...); $per_row = 5; $elem = count($arr); for($i=0; $i$elem; $i++) { if( $i == 0 ) echo tr; if( $i % $per_row == 0 ) echo /trtr; echo td$arr[$i]/td; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looping through array
Ashley M. Kirchner wrote: Say I have an array containing ten items, and I want to display them in a table as follows: 5 4 3 2 1 10 9 8 7 6 What's the best way to loop through that array to do that? My thinking gets me to create a loop for 5 through 1, repeated twice, and the second time I add '5' to the index value. There's got to be a saner way... Something like this perhaps... $arr = array(...); $per_row = 5; $elem = count($arr); for($i=0; $i$elem; $i++) { if( $i == 0 ) echo tr; else if( $i % $per_row == 0 ) echo /trtr; echo td$arr[$i]/td; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looping through array
?php $arr = array(5, 4, 3, 2, 1, 10, 9, 8, 7, 6,); echo 'table border=1'; echo 'tr'; for ($x=0,$y=sizeof($arr); $x$y; ++$x) { echo td{$arr[$x]}/td; if ($x == 4) { echo '/trtr'; } } echo '/tr'; echo '/table'; ? On Nov 16, 2006, at 1:19 PM, Ashley M. Kirchner wrote: Say I have an array containing ten items, and I want to display them in a table as follows: 5 4 3 2 1 10 9 8 7 6 What's the best way to loop through that array to do that? My thinking gets me to create a loop for 5 through 1, repeated twice, and the second time I add '5' to the index value. There's got to be a saner way... -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Darrell Brogdon [EMAIL PROTECTED] http://darrell.brogdon.net * ** Prepare for PHP Certication!** ** http://phpflashcards.com** *
Re: [PHP] Looping through array
Brad Bonkoski wrote: Something like this perhaps... $arr = array(...); $per_row = 5; $elem = count($arr); for($i=0; $i$elem; $i++) { if( $i == 0 ) echo tr; else if( $i % $per_row == 0 ) echo /trtr; echo td$arr[$i]/td; } That simply displays things in order, 1 through 5, then 6 through 10. -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looping through array
If you know the array elements, you may not need to loop. Why not just echo the particular array elements i.e. td?php echo $array[2]; ?/td for example?
Re: [PHP] Looping through array
$arr = array(...); $first_row = ''; $second_row = ''; for ($i=4; $i=0; $i--){ $first_row = $first_row . td{$arr[$x]}/td; $second_row = $second_row . td{$arr[$x + 5]}/td; } print 'tr' . $first_row . '/tr'; print 'tr' . $second_row . '/tr'; On Nov 16, 2006, at 3:19 PM, Ashley M. Kirchner wrote: Say I have an array containing ten items, and I want to display them in a table as follows: 5 4 3 2 1 10 9 8 7 6 What's the best way to loop through that array to do that? My thinking gets me to create a loop for 5 through 1, repeated twice, and the second time I add '5' to the index value. There's got to be a saner way... -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- 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] Looping through array
So in other words, you have an array like $arr = array (1,2,3,4,5,6,7,8,9,10) but you want it to render on the page as: 5 4 3 2 1 10 9 8 7 6 right? -D On Nov 16, 2006, at 1:35 PM, Ashley M. Kirchner wrote: Darrell Brogdon wrote: $arr = array(5, 4, 3, 2, 1, 10, 9, 8, 7, 6,); The array is in order, from 1 to 10. It was populated that way. -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. Darrell Brogdon [EMAIL PROTECTED] http://darrell.brogdon.net * ** Prepare for PHP Certication!** ** http://phpflashcards.com** *
Re: [PHP] Looping through array
Darrell Brogdon wrote: So in other words, you have an array like $arr = array(1,2,3,4,5,6,7,8,9,10) but you want it to render on the page as: 5 4 3 2 1 10 9 8 7 6 right? That would be correct. James Tu provided a solution that I think will work. I'm always open to other suggestions of course. -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looping through array
At 1:19 PM -0700 11/16/06, Ashley M. Kirchner wrote: Say I have an array containing ten items, and I want to display them in a table as follows: 5 4 3 2 1 10 9 8 7 6 What's the best way to loop through that array to do that? My thinking gets me to create a loop for 5 through 1, repeated twice, and the second time I add '5' to the index value. There's got to be a saner way... Ashley: Look into array_chunk() and array_flip(). The first can break your main array into two and the second can reverse the results. tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looping through array
At 11/16/2006 12:19 PM, Ashley M. Kirchner wrote: Say I have an array containing ten items, and I want to display them in a table as follows: 5 4 3 2 1 10 9 8 7 6 What's the best way to loop through that array to do that? My thinking gets me to create a loop for 5 through 1, repeated twice, and the second time I add '5' to the index value. There's got to be a saner way... In order to figure out the best PHP logic to generate the series, I'd first make sure the markup is solid. I realize that you've already indicated a table markup in two rows, but I'd like to examine that. Can you tell us why the numbers are in this particular sequence? Do the numbers represent items that are conceptually in ascending order but are presented in reverse order on the screen? If so, I'd wonder whether someone reading the page with assistive technology might be confused by the reverse order, and I'd try to find a way to mark them up ascending and then change the sequence stylistically. Are they split into two rows because they represent two discrete groups in the data set or because of display considerations? If the latter, I'd argue that they don't really belong in two table rows; that using tables to force presentation is misapplying the tool. Have you considered an unordered list, floated right, wrapped in a container whose width naturally forces a wrap after the fifth item? I like that solution because it allows you to mark up the numbers in sequence and in future change the number of items in the sequence and/or change the way the series is presented visually without having to mess with the logic generating the markup. Regards, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] looping through array with list()/each()
OK so I've done this hundreds of times, but this time I cannot get it to work. Please cast an eye over the following code and output and tell me why $k and $v are not being set: Code: - $ser = array( 'first', 'second', 'third', 'fourth', 'fifth' ); reset($ser); while ( list($key, $val) = each($ser) ); { echo \$key = $key, \$val = $valbr; } - Output: -- $key = , $val = -- I tried giving the $ser array numeric keys and string keys, both made no difference. thanks, - Kev ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses. www.mimesweeper.com ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] looping through array with list()/each()
I meant $key and $val of course :o) - Kev -Original Message- From: Kevin Porter [SMTP:[EMAIL PROTECTED]] Sent: 25 September 2002 12:00 To: '[EMAIL PROTECTED]' Subject: [PHP] looping through array with list()/each() OK so I've done this hundreds of times, but this time I cannot get it to work. Please cast an eye over the following code and output and tell me why $k and $v are not being set: Code: - $ser = array( 'first', 'second', 'third', 'fourth', 'fifth' ); reset($ser); while ( list($key, $val) = each($ser) ); { echo \$key = $key, \$val = $valbr; } - Output: -- $key = , $val = -- I tried giving the $ser array numeric keys and string keys, both made no difference. thanks, - Kev ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses. www.mimesweeper.com ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses. www.mimesweeper.com ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] looping through array with list()/each()
while ( list($key, $val) = each($ser) ); --- ; should not be here -Original Message- From: Kevin Porter [mailto:[EMAIL PROTECTED]] Sent: 25 September 2002 12:13 To: Kevin Porter; '[EMAIL PROTECTED]' Subject: RE: [PHP] looping through array with list()/each() I meant $key and $val of course :o) - Kev -Original Message- From: Kevin Porter [SMTP:[EMAIL PROTECTED]] Sent: 25 September 2002 12:00 To: '[EMAIL PROTECTED]' Subject: [PHP] looping through array with list()/each() OK so I've done this hundreds of times, but this time I cannot get it to work. Please cast an eye over the following code and output and tell me why $k and $v are not being set: Code: - $ser = array( 'first', 'second', 'third', 'fourth', 'fifth' ); reset($ser); while ( list($key, $val) = each($ser) ); { echo \$key = $key, \$val = $valbr; } - Output: -- $key = , $val = -- I tried giving the $ser array numeric keys and string keys, both made no difference. thanks, - Kev ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses. www.mimesweeper.com ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses. www.mimesweeper.com ** -- 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] looping through array with list()/each()
Thank you, thank you, thank you. And, er, D'oh! :o) - Kev -Original Message- From: Karl Phillipson [SMTP:[EMAIL PROTECTED]] Sent: 25 September 2002 12:13 To: [EMAIL PROTECTED] Subject: RE: [PHP] looping through array with list()/each() while ( list($key, $val) = each($ser) ); --- ; should not be here -Original Message- From: Kevin Porter [mailto:[EMAIL PROTECTED]] Sent: 25 September 2002 12:13 To: Kevin Porter; '[EMAIL PROTECTED]' Subject: RE: [PHP] looping through array with list()/each() I meant $key and $val of course :o) - Kev -Original Message- From: Kevin Porter [SMTP:[EMAIL PROTECTED]] Sent: 25 September 2002 12:00 To: '[EMAIL PROTECTED]' Subject:[PHP] looping through array with list()/each() OK so I've done this hundreds of times, but this time I cannot get it to work. Please cast an eye over the following code and output and tell me why $k and $v are not being set: Code: - $ser = array( 'first', 'second', 'third', 'fourth', 'fifth' ); reset($ser); while ( list($key, $val) = each($ser) ); { echo \$key = $key, \$val = $valbr; } - Output: -- $key = , $val = -- I tried giving the $ser array numeric keys and string keys, both made no difference. thanks, - Kev ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses. www.mimesweeper.com ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses. www.mimesweeper.com ** -- 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 ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses. www.mimesweeper.com ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php