0

I am trying to show all products in a database with a certain category in a HTML table. However I'm not sure how to limit the table to three columns only.

Here is my code:

<table>
    <?php
  $catagory=$_GET["q"];


$con = mysql_connect("localhost","cl49-XXX","XXX");
if (!$con) 
  {
   die('Could not connect: ' . mysql_error());
   }

mysql_select_db("cl49-XXX", $con)or die( "Unable to select database");


$result=mysql_query("SELECT * FROM products WHERE catagory  = '$catagory' ")or die('You need enter a catagory ' );

  for ($i = 0; $i < mysql_num_rows($result); $i++)
    {
        $row = mysql_fetch_array($result);

        $prodname = $row['prodname'];
        $prodID = $row['prodID'];
        if ($i % 5 == 0 || $i == 0) {
            echo "<tr>";
        }
        echo "
        <td>
            <b>$prodname </b><br />
            Product ID: $prodID<br />
            <img src='/userpics/$prodID.jpg' height='200' width='200'>
        </td>";

        if ($i % 3 == 0 || $i == (mysql_num_rows($result)-1)) {
            echo "</tr>";
        }
    }
    ?>
<table>

I am waiting to show prodID, prodtitle and image all in the same "cell" but only have three columns (three products per row).

How do I do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shane
  • 244
  • 1
  • 4
  • 17
  • 1
    Try `category` instead of `catagory`. It won't fix your problem, but it will give you extra marks in your final class report (wink) – Funk Forty Niner Aug 02 '13 at 13:18
  • @Fred - Ha thanks! However any idea how i can do what i need to? – Shane Aug 02 '13 at 13:19
  • Hint: What's the difference between `SELECT *` and `SELECT cola, colb, colc` ? – O. Jones Aug 02 '13 at 13:20
  • @Fred any idea how to fix my issue? – Shane Aug 02 '13 at 13:21
  • 2
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 02 '13 at 13:24
  • @Fred - doesn't look like they do:( – Shane Aug 02 '13 at 13:25
  • "3 products per row" - Don't use tables for layout. – Quentin Aug 02 '13 at 13:25
  • @Quentin how else can i show this? – Shane Aug 02 '13 at 13:26
  • @Shane — stylesheets. – Quentin Aug 02 '13 at 13:26
  • @Quentin I'm not sure this is layout in the general sense. If the OP is showing a list of products and associated data, a table seems to be exactly the right tool for the job. Adding multiple pieces of data to to a single cell is a bit odd, but that can be corrected. – George Cummins Aug 02 '13 at 15:09
  • @GeorgeCummins — "three products per row" says it is layout, not structural. – Quentin Aug 02 '13 at 15:20

3 Answers3

2
echo "<tr>"; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
    $row = mysql_fetch_array($result);

    $prodname = $row['prodname'];
    $prodID = $row['prodID'];

    echo "
    <td>
        <b>$prodname </b><br />
        Product ID: $prodID<br />
        <img src='/userpics/$prodID.jpg' height='200' width='200'>
    </td>";

    if ($i % 3 == 0) {
        echo "</tr> <tr>"; // it's time no move to next row
    }
}
echo "</tr>"; // last row ending

Note that $i is now starting from 1 and it loops while <= of num_rows, not <.

klkvsk
  • 670
  • 4
  • 7
1

This does not look good.

if ($i % 5 == 0 || $i == 0) {

I think it should be

if ($i % 3 == 0 || $i == 0) {

Otherwise a new <tr> Won't be opened when you close the old one.

Expanding on this, you could make this a whole lot easier for yourself.

echo "<table><tr>"; // Open the first row
for ($i ..... etc.) {

    -- SNIP --

   if ($i % 3 == 0) {
        echo "</tr><tr>"; // imediately open new row
   }
}
echo "</tr></table>"; // Close last row as well as table
AmazingDreams
  • 3,136
  • 2
  • 22
  • 32
1
<table>
<?php    
    $con = mysql_connect("localhost","cl49-XXX","XXX");
    if (!$con) {
       die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("cl49-XXX", $con)or die( "Unable to select database");

    $catagory = mysql_real_escape_string($_GET['q']); // prevent SQL injections

    if(empty($catagory)) {
        die('You need to enter a catagory');
    }

    $result = mysql_query("SELECT * FROM products WHERE catagory = '$catagory';") or die(mysql_error());
    // a general error might have occurred,
    // may or may not be related to not entering a catagory

    $rows = mysql_num_rows($result); // cache the number for performance

    for ($i = 0; $i < $rows; ++$i) {
        $row = mysql_fetch_array($result);

        $prodname = $row['prodname'];
        $prodID = $row['prodID'];
        if ($i % 3 === 0) {
            echo "<tr>";
        }
        echo "
        <td>
            <b>$prodname</b><br />
            Product ID: $prodID<br />
            <img src='/userpics/$prodID.jpg' height='200' width='200'>
        </td>";

        if ($i % 3 === 0) {
            echo "</tr>";
        }
    }

    if($i % 3 > 0) {
        // last row was not full
        echo '</tr>';
    }
?>
</table>
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156