<?php
include "dbinfo.php";
// connect to SQL database
if (!($connection = @ mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD)))
showerror();
// use the databse shown in dbinfo.php
if (!mysql_select_db(DB_DATABASENAME, $connection))
showerror();
// create the SQL query
$query = "SELECT * FROM ad_neck WHERE StockCount > 0 ORDER BY StockCode DESC";
// execute the query
if (!($result = @ mysql_query ($query, $connection)))
showerror();
// echo results
//create a counter
$i = 0;
$table_rows = "";
while ($row = @ mysql_fetch_array($result))
if (!$i){
//if we have no items in the table row (as indicated by i) then add one.
//note how this row we are adingis not finished with the </tr> yet...
//we need to add another item to the row
//in the next loop
$table_rows = "<tr><td>{$row["StockCode"]}</td>
<td>{$row["Description"]}</td>
<td>{$row["Price"]}</td>
<td><p><a href='{$row["IMGURL"]}'><img src='{$row["IMG"]}'></img></p></td>
<td><center><a href='{$row["URL"]}'>$row[url]</a></center></td>";
$i++;//increment the counter telling how many items we have in the last table row to 1
}else{
//if we have 1 item in the row already then add another (using .=) and finish the row with </tr>
$table_rows .= "<td>{$row["StockCode"]}</td>
<td>{$row["Description"]}</td>
<td>{$row["Price"]}</td>
<td><p><a href='{$row["IMGURL"]}'><img src='{$row["IMG"]}'></img></p></td>
<td><center><a href='{$row["URL"]}'>$row[url]</a></center></td></tr>";
$i = 0;//reset the counter since we have just put the 2nd item in the row and next loop we
//want to start an new row
//echo $table_rows;//uncomment this if you choose to echo from within the loop (see noted below)
}
endwhile; //i use end while as i find it easier on the eye... even though its not efficent code
//now we are out of the loop we need to catch one error straight away. If the loop finished and there was only one item
//added to the last row. We can check the $i counter to see if it is =1, if it is then we should really pad out the
//rest of the final row with non breaking spaces and end the row with </.tr>
if ($i)
$table_rows .= "<td> </td><td> </td><td> </td><td> </td><td> </td></tr>";
//at this point you have the whole table stored in the variable $tables_rows I noted that your code here did not include
//the tables header <Table> so i assume you have created the table above this php script. If you have you will need
//to edit this to include coloumn titles for the new columns. You will also note we havn't echoed anything out yet, to save
//operations (not that it really maters on this script) I echo to whole table now. If you disagree you will note that I
//have left an echo (which i commented out) in the above while loop; you can uncomment this echo in the loop and comment
//comment out the echo below. You may need to do this if the string $table_rows is getting huge!
echo $table_rows;
?>