Jump to content

multi-column table


mdburden

Recommended Posts

I am using php 5 on an apache 2.2 server and a mySQL 6 database.

 

I am trying to create a catalog page that will display items founs as a result of the database.

 

I can get the data to display correctly in a table with one item per row.

I can get the table to display in 3 columns, but the item is the same in each column of the row.

 

The next row has a new item, but that is also displayed in each column of that row.

 

I am using the following php code:

 

// Setup and run query

 

$query = "select * from items where name like '%".$searchterm."%'";

$result = $db->query($query);

$num_results = $result->num_rows;

echo "<p>Number of items found: ".$num_results."</p>";

 

// Display results

echo "<table border= 1>";

 

  for ($i=0; $i <$num_results; $i++)

{

 

$row = $result->fetch_assoc();

$Imge = stripslashes($row['imge']);

echo "<tr>";

for ($c=0; $c < 3; $c++)

{

echo "<td>";

echo "<p><strong>Number: ";

echo stripslashes($row['number']);

echo "\t \t";

echo htmlspecialchars(stripslashes($row['title']));

echo stripslashes($row['name']);

echo "</strong><br /></p>";

echo "<p align='center'><img border=\"0\" src=\"images/".$Imge."\"><br /></p>";

echo stripslashes($row['description']);

echo "<br />Price: ";

echo stripslashes($row['price']);

echo "</p>";

echo "</td>";

}

echo "</tr>";

}

echo "</table>";

 

Can anyone help?

Link to comment
Share on other sites

what r u trying to achieve?

 

you want to show records in 3 columns?

 

try this..

 

<?php 
// Setup and run query

$query = "select * from items where name like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of items found: ".$num_results."</p>";

// Display results
   echo "<table border= 1>";

     for ($i=0; $i <$num_results; $i=$i+3) 
      {
        echo "<tr>";
        for ($c=0; $c < 3; $c++)
       {
           if((($i*3) + $c) < $num_results) {
               $row = $result->fetch_assoc();
               $Imge = stripslashes($row['imge']);
                  echo "<td>";
                     echo "<p><strong>Number: ";
                     echo stripslashes($row['number']);
                     echo "\t \t";
                     echo htmlspecialchars(stripslashes($row['title']));
                     echo stripslashes($row['name']);
                     echo "</strong><br /></p>";
                     echo "<p align='center'><img border=\"0\" src=\"images/".$Imge."\"><br /></p>";
                     echo stripslashes($row['description']);
                     echo "<br />Price: ";
                     echo stripslashes($row['price']);
                     echo "</p>";
                  echo "</td>";
              } else {
               echo "<td> </td>";
              }
      }                           
   echo "</tr>";         
   }
   echo "</table>";


?>

PS: the code is not tested.

 

EDIT: updated if condition to multiply by 3

 

 

 

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.