Jump to content

split a aray up into two with table?


redarrow

Recommended Posts

<?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


<?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>";



?>

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.

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>";

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.

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
			}
?>

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>";

}

?>
<?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?

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>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.