Jump to content

[SOLVED] Display Results in 2 Columns from left to right not top to bottom


affc

Recommended Posts

Hey,

I am trying to now change the code so if diplays from this:

 

1      4

2      5

3      6

 

to this

 

1    2

3    4

5    6

 

Here is my code but I have no idea how to change it, hopefully its easy enough, thanx ;)

 

<?php
$columns = 2;
$sql="SELECT * FROM newsdetails ORDER BY year DESC, fileno DESC";
$result = mysql_query($sql) or die("Couldn't execute query.");

$num_rows = mysql_num_rows($result);

$rows = ceil($num_rows / $columns);

while($row = mysql_fetch_array($result)) {
$data[] = $row['TEXT'];
$data1[] = $row['Date'];
$data2[] = $row['file'];
$data3[] = $row['Title'];

}
?>
              <table width="600" border="0">
                <?
for($i = 0; $i < $rows; $i++) {
?>
                <tr>
<?
for($j = 0; $j < $columns; $j++) {
if(isset($data[$i + ($j * $rows)])) {
?>
                  <td width="150"><img src="images/<? echo $data1[$i + ($j * $rows)]; ?>_<? echo $data2[$i + ($j * $rows)]; ?>_001_L.gif" width="170"></td>
                  <td width="150" valign="top"><a href="index.php?year=<? echo $data1[$i + ($j * $rows)]; ?>&no=<? echo $data2[$i + ($j * $rows)]; ?>"><font color="#FFFFFF" size="2" face="Times New Roman, Times, serif"><strong><? echo $data3[$i + ($j * $rows)]; ?></strong></font></a><br><br>
<font
    color="#00FFFF" size="2" face="Times New Roman, Times, serif"><em><? echo $data[$i + ($j * $rows)]; ?></em></font></td>
<?
}

}
?></tr>
<?
} 
?></table>

You don't show how you're displaying the data in two columns at the moment, simply building an array.

<?php
$columns = 2;
$sql="SELECT * FROM newsdetails ORDER BY year DESC, fileno DESC";
$result = mysql_query($sql) or die("Couldn't execute query.");

$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
   $row = 0;
   while($row = mysql_fetch_array($result)) {
      if ($row % $columns == 0) {
         if ($row > 0) echo '</tr>';
         echo '<tr>';
      }
      echo '<td>';
      echo $row['TEXT'];
      echo $row['Date'];
      echo $row['file'];
      echo $row['Title'];
      echo '</td>';
      $row++;
   }
   if ($row % $columns != 0) echo '<td></td>';
   echo '</tr>';
} else {
   echo 'No data returned';
}

I dont understand what you mean? The code I have their displays everything I need in columns etc just displays it like this:

 

1    4

2    5

3    6

 

As some of the code is HTML and the other part PHP, I cannot get this forum to highlight the whole lot but all the code is above you should see the Table tags etc,

<?php
$columns = 2;
$sql="SELECT * FROM newsdetails ORDER BY year DESC, fileno DESC";
$result = mysql_query($sql) or die("Couldn't execute query.");

echo '<table><tr>';
$count = 0;
while ($row = mysql_fetch_assoc($result)) {
     if (!empty($count) && $count % $columns == 0) echo '</tr><tr>';
     echo '<td>' , $row['TEXT'] , '<br />' , $row['Date'] , '<br />' , $row['file'] , '<br />' , $row['Title'] , '</td>';
     $count++;
}

 

I think that should do it.

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.