scm22ri Posted August 2, 2012 Share Posted August 2, 2012 Hi Everyone, I want to display ID, Year, Make, Model etc ... once. In the below URL those titles keep displaying multiple times and I'm not sure why they do that. Below is my syntax. What am I doing wrong? Thanks everyone! http://whatsmyowncarworth.com/auto-practice/display-database.php <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#FFFFFF" text="#000000"> <?php include_once "init.php"; $query = "SELECT * FROM car_info WHERE make='chevrolet'"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $id = mysql_real_escape_string ($row['id']); $year = mysql_real_escape_string ($row['year']); $make = mysql_real_escape_string ($row['make']); $model = mysql_real_escape_string ($row['model']); $price = mysql_real_escape_string ($row['price']); $extcolor = mysql_real_escape_string ($row['exteriorcolor']); $intcolor = mysql_real_escape_string ($row['interiorcolor']); $engine = mysql_real_escape_string ($row['engine']); $trans = mysql_real_escape_string ($row['transmission']); $fuel = mysql_real_escape_string ($row['fueltype']); $mileage = mysql_real_escape_string ($row['mileage']); $state = mysql_real_escape_string ($row['state']); $city = mysql_real_escape_string ($row['city']); echo "<table border='1'>"; echo "<tr> <th>ID</th> <th>Year</th> <th>Make</th> <th>Model</th> <th>Price</th> <th>Exterior Color</th> <th>Interior Color</th> <th>Engine</th> <th>Tranmission</th> <th>Fuel Type</th> <th>Mileage</th> <th>State</th> <th>City</th> </tr>"; // keeps getting the next row until there are no more to get /*while ($row = mysql_fetch_array($result))*/ { // Print out the contents of each row into a table ?> <tr> <td><?php echo "$id"; ?></td> <td><?php echo "$year"; ?></td> <td><?php echo "$make"; ?></td> <td><?php echo "$model"; ?></td> <td><?php echo "$price"; ?></td> <td><?php echo "$extcolor"; ?></td> <td><?php echo "$intcolor"; ?></td> <td><?php echo "$engine"; ?></td> <td><?php echo "$trans"; ?></td> <td><?php echo "$fuel"; ?></td> <td><?php echo "$mileage"; ?></td> <td><?php echo "$state"; ?></td> <td><?php echo "$city"; ?></td> </tr> <?php } /*echo "</table>";*/ } /*else*/ { /*trigger_error(mysql_error()); // for development only; remove when in production*/ } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/266578-tables-in-php-display-only-one-time/ Share on other sites More sharing options...
scm22ri Posted August 2, 2012 Author Share Posted August 2, 2012 Hi Everyone, Please disregard this question. I figured it out. Quote Link to comment https://forums.phpfreaks.com/topic/266578-tables-in-php-display-only-one-time/#findComment-1366180 Share on other sites More sharing options...
Pikachu2000 Posted August 2, 2012 Share Posted August 2, 2012 Mark the thread solved using the green "Mark Solved" button, please. Quote Link to comment https://forums.phpfreaks.com/topic/266578-tables-in-php-display-only-one-time/#findComment-1366182 Share on other sites More sharing options...
jcbones Posted August 2, 2012 Share Posted August 2, 2012 I had this response ready, and there was 1 glaring problem in your code block, so I will post what I had. 1. mysql_real_escape_string is for escaping for database INSERTION, not SELECTION. (glaring problem). 2. You are creating the table inside the while LOOP, instead of just creating a new table row. It should be close to: <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#FFFFFF" text="#000000"> <?php include_once "init.php"; //include this file. $query = "SELECT * FROM car_info WHERE make='chevrolet'"; //build database query string. $result = mysql_query($query) or trigger_error(mysql_error()); // for development only; remove when in production (run query string, throw an error if it fails.) if(msyql_num_rows($result) > 0) { //if database returns at least 1 row. echo "<table border='1'>"; //build the table element. echo "<tr> <th>ID</th> <th>Year</th> <th>Make</th> <th>Model</th> <th>Price</th> <th>Exterior Color</th> <th>Interior Color</th> <th>Engine</th> <th>Tranmission</th> <th>Fuel Type</th> <th>Mileage</th> <th>State</th> <th>City</th> </tr>"; //build table header row. while ($row = mysql_fetch_assoc($result)) { //while there are rows available in the result resource. //make variables (optional). $id = $row['id']; $year = $row['year']; $make = $row['make']; $model = $row['model']; $price = $row['price']; $extcolor = $row['exteriorcolor']; $intcolor = $row['interiorcolor']; $engine = $row['engine']; $trans = $row['transmission']; $fuel = $row['fueltype']; $mileage = $row['mileage']; $state = $row['state']; $city = $row['city']; //echo each table row. echo <<<EOT <tr> <td>{$id}</td> <td>{$year}</td> <td>{$make}</td> <td>{$model}</td> <td>{$price}</td> <td>{$extcolor}</td> <td>{$intcolor}</td> <td>{$engine}</td> <td>{$trans}</td> <td>{$fuel}</td> <td>{$mileage}</td> <td>{$state}</td> <td>{$city}</td> </tr> EOT; } //all table rows have been echo'd echo "</table>"; //so close the table. } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/266578-tables-in-php-display-only-one-time/#findComment-1366191 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.