Jump to content

how to list php in more than one column!


jeger003

Recommended Posts

hi guys,

i made this code list US states in six columns....the problem is that it lists it across rather than down

 

it lists it like thi

 

Alabama    Alaska    Arizona    blah1    blah2

blah3            blah4      blah5      blah6

 

when I want it to list like this

 

Alabama  blah2 

Alaska      blah3

Arizona    blah 4

blah1      blah5

 

but not just two columns....I want 4 columns

 

here is my code

 

$query = mysql_query("SELECT * FROM states ORDER BY name ASC");
echo '<center><table width="250" border="0" cellpadding="5" cellspacing="0">
<tr>';

while($show = mysql_fetch_array($query))
{

$column = 1+$column;

if($column == 6)
{
	$column = '1';
	$break = '</td></tr>';
}
else
{
	$break = '';
}
echo '<td>
<table >
<tr class="style5">
  <td width="50%" align="left" height="23" nowrap>'.$show['name'].'</td>
  <td height="23" valign="middle" > 
    </td>
</tr>
</table>
'.$break.'';
}


echo '</table></center>';

 

Link to comment
Share on other sites

I had to do something similar in wordpress using the array_chunk() function

http://php.net/manual/en/function.array-chunk.php

 

This may give you some ideas  -  basically you are splitting your $show array into 3 new arrays and printing each in their own column

 

//empty arrays 
$firstn = array();
$lastn = array();
$lastn2 = array();
$link = array();


//populate the above arrays
$tags = get_tags();
foreach ($tags as $tag ) {
$piece = $tag->slug;
$pieces = explode("-",$piece);
array_push($firstn,$pieces[0]);
array_push($lastn,$pieces[1]);
array_push($lastn2,$pieces[2]);
array_push($link, get_tag_link ($tag->term_id));
}

//extract values of arrays and sort alpha by last name
$firstname = array_values($firstn);
$lastname = array_values($lastn);
asort($lastname);
$lastname2 = array_values($lastn2);
$taglinks = array_values($link);
//new array that is combination of first, last and link
$all  = array();
//populate $all
foreach($lastname as $key=>$value) {
$all[$value][0] = $value;
$all[$value][1] = $firstname[$key];
$all[$value][2] = $taglinks[$key];
$all[$value][3] = $lastname2[$key];
//count number of names
$number  = count($all);
// get number for chunk value
$chunk_value = round($number / 3) ;
}
//array of 3 columns
$col = array();
//split into 3 
$col = array_chunk($all, $chunk_value, true);
//column 1 values
$column1 = array_values($col[0]);
//colum 2 values
$column2 = array_values($col[1]);
//column 3 values
$column3 = array_values($col[2]);
//display content
?>


<?php foreach ($column1 as $key=>$value) {
echo '<a href="'. $value[2] .'" rel="tag">'.
$value[1].' '.$value[0].' '.$value[3].'</a><br />' ;
} ?><?php foreach ($column2 as $key=>$value) {
echo '<a href="'. $value[2] .'" rel="tag">'.
$value[1].' '.$value[0].' '.$value[3].'</a><br />' ;
} ?>
<?php foreach ($column3 as $key=>$value) {
echo '<a href="'. $value[2] .'" rel="tag">'.
$value[1].' '.$value[0].' '.$value[3].'</a><br />' ;
} ?>

 

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.