Forum Moderators: coopster

Message Too Old, No Replies

Drop Down List Now Working

Just need advice on layout

         

AliTaylor4411

8:58 pm on Aug 17, 2010 (gmt 0)

10+ Year Member



I have left a post previously about a drop down list form which I have now resolved but the results (which now work!) just come back in rows and I wondered how I could make it look better!

I would really like it to be in a table with borders say like this:

Price:£?
Property Type:?
Property Address : ?
Number of Bedrooms :?
Description:?


This is my code

<?php

// This page retrieves data from the database table "property" and this code should be used to create the list of properties.

//Open the connection
$conn = mysql_connect("*&*&*&*&","&*&*&**&*&€", "*&*&*&*&");

//Select the Database
mysql_select_db("*&*&*&*", $conn);

//Create the MySQL Command to retrieve the record
$sql= "SELECT * FROM `property`
WHERE `price` >= '".$_POST['minprice']."'
AND `price` <= '".$_POST['maxprice']."'
AND `bedrooms` >= '".$_POST['bedrooms']."'
AND `propertytype` = '".$_POST['propertytype']."'";

echo "$sql<br>";

//Execute the MySQL statement and convert the result to an array
$result = mysql_query($sql, $conn);
while ($array = mysql_fetch_array($result)) {
$propertyid = $array ['propertyid'];
$propertyaddress = $array['propertyadd1'].$array['propertyadd2'].$array['propertyadd3'].$array['postcode'];
$propertytype = $array['propertytype'];
$bedrooms = $array['bedrooms'];
$price = $array['price'];
$description = $array['description'];
echo "<p> <a href= propertydetailsbox.php?propertyid=$propertyid>$propertyaddress</a></br>$propertytype</br>$bedrooms</br>$price</br>$description</p>";
}
?>

rocknbil

2:12 am on Aug 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace your while {} loop with this.


$results = null;
while ($array = mysql_fetch_array($result)) {
$propertyid = $array ['propertyid'];
$propertyaddress = $array['propertyadd1'].
$array['propertyadd2'].
$array['propertyadd3'].
$array['postcode'];
$propertytype = $array['propertytype'];
$bedrooms = $array['bedrooms'];
$price = $array['price'];
$description = $array['description'];
$results .= "
<tr>
<td class=\"right-align\">Price:£ </td><td> $price </td>
<td class=\"right-align\">Property Type: </td><td> $propertytype </td>
<td class=\"right-align\">Property Address: </td>
<td>
<a href=\"propertydetailsbox.php?propertyid=$propertyid\">$propertyaddress</a>
</td>
<td class=\"right-align\">Number of Bedrooms: </td><td> $bedrooms </td>
<td class=\"right-align\" valign=\"top\">Description: </td><td> $description </td>
</tr>
";
}
if ($results) {
echo "<table id=\"search-result\">$results</table>";
}
else { echo "<p>No results were found with that search.</p>"; }


Then add to your style sheet:

#search-result {
width: 640px; /* or whatever */
margin: auto;
border: 1px solid #000;
}

.right-align {
text-align: right;
}

AliTaylor4411

2:53 pm on Aug 19, 2010 (gmt 0)

10+ Year Member



Hi Rocknbil,

I have amended my code (not just copied and pasted yours as want to learn how and why you do things)

I am now getting a result in a table which is great but I am only getting one result table (when I know there is more than one result to be shown). SO I think my table needs to be include in my while loop? I think!

Here is my code - I wondered if you could tell me where I am going wrong!

// This page retrieves data from the database table "property" and this code should be used to create the list of properties with a table)

//Open the connection
$conn = mysql_connect("***","***", "***");

//Select the Database
mysql_select_db("***", $conn);

//Create the MySQL Command to retrieve the record
$sql= "SELECT * FROM `property`
WHERE `price` >= '".$_POST['minprice']."'
AND `price` <= '".$_POST['maxprice']."'
AND `bedrooms` >= '".$_POST['bedrooms']."'
AND `propertytype` = '".$_POST['propertytype']."'";

echo "Properties matching your search are shown below";

//Execute the MySQL statement and convert the result to an array
$result = mysql_query($sql, $conn);
while ($array = mysql_fetch_array($result)) {
$propertyid = $array ['propertyid'];
$propertyaddress = $array['propertyadd1'] . $array['propertyadd2'] . $array['propertyadd3'] . $array['postcode'];
$propertytype = $array['propertytype'];
$bedrooms = $array['bedrooms'];
$price = $array['price'];
$description = $array['description'];
}
?>
<p>
</p>
<table width="80%" border="1" cellpadding="1">
<tr>
<td colspan="2"><?php echo "Property Address:"?></br></td>
<td><?php echo $propertyaddress?>
<td width="32%" rowspan="4"><?php echo "Property Image to be Inserted" ?></tr>
<tr>
<td colspan="2"><?php echo "Property Type:"?></br></td>
<td width="48%"><?php echo $propertytype?>&nbsp;</td>
</tr>
<tr>
<td colspan="2"><?php echo "Number of Bedrooms:"?></br>&nbsp;</td>
<td><?php echo $bedrooms?>&nbsp;</td>
</tr>
<tr>
<td colspan="2"><?php echo "Property Price:"?></br></td>
<td><?php echo £.$price?>&nbsp;</td>
</tr>
<tr>
<td colspan="2"><?php echo "Property Description:"?></br>&nbsp;</td>
<td colspan="2"><?php echo $description?>&nbsp;</td>
</tr>
</table>

rocknbil

6:12 pm on Aug 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And you would be (for the most part) correct. Look how you're ending the PHP, then have a static HTML section after it, it will only hold one record - the last one. Your approach is similar to mine in that there is indeed one table, but look how mine outputs an error if there's no results. You can't do that using the alternating php/html method.

You **do** want to have rows in the while, but not the entire table. I suggest you use my sample and modify the row output within the while, but as an exercise, using the method you have here you would do

<table width="80%" border="1" cellpadding="1">
<?php echo $results ?>
</table>

I combined this with your other thread about counting rows . . .