redarrow Posted March 7, 2013 Share Posted March 7, 2013 (edited) <?php $a=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"); echo "<table border='4'><t><td>result1</td><td>result2</td></tr></table>"; ?> how do i split a array up using foreach or a for loop forgot HELP please.... i want the table to have the results in the colums from the array but split correctly... thank you. john Edited March 7, 2013 by redarrow Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/ Share on other sites More sharing options...
redarrow Posted March 7, 2013 Author Share Posted March 7, 2013 <?php $a=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","v","w","x","y","z"); $columns = 2; echo "<table border='4'><tr>"; for($i = 0; $i < count($a); $i++) { if($i%$columns == 0) echo "</tr><tr>"; echo "<td>".$a[$i]."</td>"; } echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417158 Share on other sites More sharing options...
redarrow Posted March 8, 2013 Author Share Posted March 8, 2013 (edited) wontl work in my app going mad <?phph while ($category = mysql_fetch_array($category_query)) { ?> <a class="mainpagekeyword" href="<?php echo generate_link('category.php',$category['slug']);?>"><div align="center" class="mainpagekeyword" style="color: #<?php echo $catcolour ?>;"><?php echo $category['category']; ?></div></a> <?php } ?> i need to split the array so there more keywords side by side with a table like my above first example.... the above code gives you words with links and then you can go to a dynamic other page. now if i add another 10 words, i need it side by side like my first above example , if i leave it a very long list. but how does it split with a while loop this time ... need real help. advance thank you. Edited March 8, 2013 by redarrow Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417509 Share on other sites More sharing options...
Barand Posted March 8, 2013 Share Posted March 8, 2013 easier to use floating divs instead of a table $a=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"); $cols = 2; $i = 0; foreach ($a as $item) { if ($i % $cols == 0 && $i) { echo "<div style='clear:left'></div>"; } echo "<div style='float:left'>$item</div>"; ++$i; } echo "<div style='clear:left'></div>"; Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417519 Share on other sites More sharing options...
Psycho Posted March 8, 2013 Share Posted March 8, 2013 Your post title and first two posts were about any ARRAY and your last post is using a while() loop from a database result. Those are very different things. Doing what you want with an array is very easy using array_chunk(). foreach(array_chunk($array, $column) as $row) { echo "<tr>\n"; foreach($row as $item) { echo "<td>{$item}</td>\n"; } echo "</tr>\n"; } Of course, you could always dump the DB results into an array first. Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417526 Share on other sites More sharing options...
redarrow Posted March 8, 2013 Author Share Posted March 8, 2013 (edited) can you kindly show me from this code your code working never got it going. i want two colmuns of data from a while loop cheers. [ need help with a while loop now cheers... i want the information below in two colums but how <?php while ($category = mysql_fetch_array($category_query)) { ?> <a class="mainpagekeyword" href="<?php echo generate_link('category.php',$category['slug']);?>"><div align="center" class="mainpagekeyword" style="color: #<?php echo $catcolour ?>;"><?php echo $category['category']; ?></div></a> <?php } ?> Edited March 8, 2013 by redarrow Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417529 Share on other sites More sharing options...
redarrow Posted March 8, 2013 Author Share Posted March 8, 2013 dont work <?php while ($category = mysql_fetch_array($category_query)) { if(row_num_is_odd) { $first_column += $category['category']; } else { $second_column += $category['category']; } print "<div='1st-column'>" . $first_column . "</div>"; print "<div='2nd-column'>" . $second_column . "</div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417535 Share on other sites More sharing options...
Psycho Posted March 8, 2013 Share Posted March 8, 2013 << Unfollowing topic >> Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417536 Share on other sites More sharing options...
Jessica Posted March 8, 2013 Share Posted March 8, 2013 (edited) You need to turn on error reporting, and you'd see a big problem. Edit: Uh oh, what have I done? :-P Edited March 8, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417537 Share on other sites More sharing options...
redarrow Posted March 8, 2013 Author Share Posted March 8, 2013 <?php while ($category = mysql_fetch_array($category_query)) { ?> <a class="mainpagekeyword" href="<?php echo generate_link('category.php',$category['slug']);?>"><div align="center" class="mainpagekeyword" style="color: #<?php echo $catcolour ?>;"><?php echo $category['category']; ?></div></a> <?php } ?> the web mason,, can you split this or not to two tables? Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417538 Share on other sites More sharing options...
redarrow Posted March 8, 2013 Author Share Posted March 8, 2013 (edited) Done, i just limited 2 tabels to 10, i used two select's and two while loops. dont no any other way. Edited March 8, 2013 by redarrow Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417540 Share on other sites More sharing options...
Barand Posted March 8, 2013 Share Posted March 8, 2013 try $i = 0; while ($category = mysql_fetch_array($category_query)) { $ref = generate_link('category.php',$category['slug']); if ($i % 2 ==0 && $i) echo "<div style='clear:left'></div>"; echo <<<TXT <div align="center" class="mainpagekeyword" style="float:left; color: #$catcolour"> <a class="mainpagekeyword" href="$ref"> $category['category'] </a> </div> TXT; $i++; } echo "<div style='clear:left'></div>"; Quote Link to comment https://forums.phpfreaks.com/topic/275360-split-a-aray-up-into-two-with-table/#findComment-1417566 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.