Jump to content

Displaying array in 2 columns


easyedy

Recommended Posts

Hi I'm trying to rearrange my code so I can display my array in 2 columns with each column being 1 cell from each row being a cell like I got now. Any suggestions?

 

Code I have now:

 

define ("NUMCOLS",2);

$count = 0;
$counter= 1;
echo "<table border='0' width='100%'>";
foreach ($result[$Array] as $row) {

    if ($count % NUMCOLS == 0) echo "<tr>\n";  # new row
    echo '<td width="50%"><a href="$row['ID'].'">'.$row['Name'].'</a></td>';
    $count++;
    $counter++;

    if ($count % NUMCOLS == 0) echo "</tr>\n";  # end row
}

# end row if not already ended

if ($count % NUMCOLS != 0) {
   foreach ($result[$FA][$SA] as $row) echo "<td> </td>";
   echo "</tr>\n";
}
echo "</table>";

 

It displays now as:

<td>1</td><td>2</td>
<td>3</td><td>4</td>

 

when I need it to be:

<td>1<br>2</td><td>3<br>4</td>

 

I just can't get my code to work. thx.

Link to comment
https://forums.phpfreaks.com/topic/84230-displaying-array-in-2-columns/
Share on other sites

<?
define ("NUMCOLS",2);
$count = 0;
$counter= 1;
echo "<table border='0' width='100%'>";
foreach ($result[$Array] as $row) {
    if ($count % NUMCOLS == 0){
	 echo "<tr>\n";  # new row
	 echo '<td width="50%"><a href="'.$row['ID'].'">'.$row['Name'].'</a></td>';
	$count++;
	$counter++;
	continue;
}
	echo '<td width="50%"><a href="'.$row['ID'].'"><br />'.$row['Name'].'</a></td>';
	$count++;
	$counter++;


    if ($count % NUMCOLS == 0) echo "</tr>\n";  # end row
}

# end row if not already ended

if ($count % NUMCOLS != 0) {
   foreach ($result[$FA][$SA] as $row) echo "<td> </td>";
   echo "</tr>\n";
}
echo "</table>";

 

ok may this ...

<?
define ("NUMCOLS",2);
$count = 0;
$counter= 1;
echo "<table border='0' width='100%'>";
foreach ($result[$Array] as $row) {
    if ($count % NUMCOLS == 0){
	 echo "<tr>\n";  # new row
	 echo '<td width="50%"><a href="'.$row['ID'].'">'.$row['Name'].'</a></td>';
	$count++;
	$counter++;
	continue;
}
	echo '<td width="50%"><a href="'.$row['ID'].'"><br />'.$row['Name'].'</a></td>';
	$count++;
	$counter++;


    if ($count % NUMCOLS == 0) echo "</tr>\n";  # end row
}

# end row if not already ended

if ($count % NUMCOLS != 0) {
   foreach ($result[$FA][$SA] as $row) echo "<td> </td>";
   echo "</tr>\n";
}
echo "</table>";

 

ok may this ...

 

No makes new table rows for each $row... I want 1 table row and 2 columns. ie: 1/2 of the array is displayed in the first <td></td> and the other half in the other <td></td>

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.