please help put array into columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rickyspires
Forum Newbie
Posts: 1
Joined: Thu Jun 03, 2010 9:14 am

please help put array into columns

Post by rickyspires »

please help put array into columns

hello.
the below code puts the most recent row added into a list.
im trying to put the list in to 2 colums
how can i do that?

Code: Select all

<?php
	$shop = array(); 

	$query = "SELECT * FROM form1 WHERE id='$user_id'";
	
	$result = mysql_query($query) or die(mysql_error());
	
	while($row = mysql_fetch_array($result)){
	
	$shop[] = $row['Fname'];
	$shop[] = $row['Sname'];
	$shop[] = $row['description'];
	}


	foreach($shop as $key => $details)	{
	$key++;
	echo $key . ": ". $details . "<br/>";
	}
?>
RESULTS:

1: ricky
2: spires
3: my description



I WOULD LIKE THIS TO BE ECHOED :

result 1 ----------------result 2
result 3 ----------------result 4
result 5 ----------------result 6
result 7 ----------------result 8
result 9 ----------------result 10


thanks
rick
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: please help put array into columns

Post by markusn00b »

Check out this (slightly hacky) code:

Code: Select all

<?php

$array = array(
    'one', 'two', 'three', 'four', 'five'
);

$count = count($array);

for ($i = 0; $i < $count; $i++) {
    
    echo $array[$i];
    if (isset($array[$i + 1])) {
        echo " " . $array[$i + 1];
        $i++;
    }
    echo "\n";    
}
Post Reply