redarrow Posted March 7, 2013 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","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 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>"; ?> 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 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. 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>"; 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. 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 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 } ?> 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>"; } ?> 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 >> 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 You need to turn on error reporting, and you'd see a big problem. Edit: Uh oh, what have I done? :-P 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? 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 Done, i just limited 2 tabels to 10, i used two select's and two while loops. dont no any other way. 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>"; 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
Archived
This topic is now archived and is closed to further replies.