Jump to content

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


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.

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.