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

Edited by redarrow
Link to comment
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>";



?>
Link to comment
Share on other sites

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 by redarrow
Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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 by redarrow
Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.