Jump to content

Tables in PHP, Display only one time?


scm22ri

Recommended Posts

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>

Link to comment
Share on other sites

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>

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.