homepage Welcome to WebmasterWorld Guest from 54.161.247.22
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to group values in a while loop
saideep2007




msg:4308666
 10:29 am on May 6, 2011 (gmt 0)

Hi everyone,

can anyone help me out how to group a loop values

e.g

after runnin a simple while loop i got this values

1,
2,
3,
.
.
.
.
10


Now what i want is to value 1 and value 2 must be in one group and vice versa


Output should be like this in a html table


1,2

3,4

5,6

7,8

9,10




any help is appreciated


Thanks All

 

saideep2007




msg:4308672
 10:37 am on May 6, 2011 (gmt 0)

Sorry the output should be like this

Output should be like this in a html table


1
2

3
4

5
6

7
8

9
10




any help is appreciated


Thanks All

rocknbil




msg:4308854
 5:58 pm on May 6, 2011 (gmt 0)


<?php
header("content-type:text/html");
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<style type="text/css">
body { font-family:arial,helvetica,sans-serif; }
p,table { text-align: center; }
table { width: 400px; margin: auto; }
.wht { background: #fff; }
.gray { background: #eaeaea; }
</style>
</head>
<body>
<p>Output columns.</p>
';
// I make them odd for a reason, watch.
//
$list_array = array (1,2,3,4,5,6,7,8,9,10,11);
$assoc_array = array (
1 => 'One',
2 => 'Two',
3 => 'Three',
4 => 'Four',
5 => 'Five',
6 => 'Six',
7 => 'Seven',
8 => 'Eight',
9 => 'Nine',
10 => 'Ten',
11 => 'Eleven'
);
$bg=null;
$colcount=0;
//
echo '<table border=1">';
foreach ($list_array as $scalar) {
if ($colcount==0) {
echo "<tr>";
$bg = ($bg=='wht')?'gray':'wht';
}
echo "<td class=\"$bg\">$scalar</td>";
$colcount++;
if ($colcount >= 2) { echo "</tr>\n"; $colcount = 0; }
}
//
if ($colcount==1) { echo "<td>&nbsp;</td></tr>\n"; }
echo '</table><p>&nbsp;</p><table border="1">';
//
$colcount=0;
foreach ($assoc_array as $key => $value) {
if ($colcount==0) {
echo "<tr>";
$bg = ($bg=='wht')?'gray':'wht';
}
echo "<td class=\"$bg\">$key</td><td class=\"$bg\">$value</td>";
$colcount++;
if ($colcount >= 2) { echo "</tr>\n"; $colcount = 0; }
}
//
if ($colcount==1) { echo "<td>&nbsp;</td><td>&nbsp;</td></tr>\n"; }
echo '</table>';
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved