Rebekah
Rebekah

Reputation: 35

Why is only one result displaying in my table?

I am using php my mysqli and when the user picks a house id from the database and show display the results in a table using a select query. For reason it only displays one row even it there is two rows using the same number, any idea how to fix this.

<form action="back.php" method="post"> 

<p>Chose a house ID</p>

<select name="HouseID"> 


  <?php

include "connect.php";

$query = "SELECT distinct `HouseID` from purchase";
$result = mysqli_query($con, $query) or die("Invalid query");  
while($rows = mysqli_fetch_array($result))

{ 

      echo "<option value=\"" . $rows[0] . "\">" . $rows[0] ."</option>"; 
} 

echo "</select>"; 
         

mysqli_close($con); 
?> 

<p>Choose the ORDER BY attribute</p>

<select name ="list">
 
  <option value="PurchaseID">PurchaseID</option>
  <option value="HouseID">HouseID</option>
  <option value="DatePurchased">DatePurchased</option>
  <option value="DatePurchased">DatePurchased</option>
  <option value="AskingFor">AskingFor</option>
  <option value="SoldFor">SoldFor</option>
  <option value="AgentID">AgentID</option>
  <option value="CustomerID">CustomerID</option>
</select>

<p>Choose ASC or DESC attribute order</p>

ASC<input type="radio" name="order" value="ASC" checked>

DESC<input type="radio" name="order" value="DESC" checked>

<br>
<br>
<input type="submit" value="Submit Value">
</form></body></html> 

<?php

$HouseID = $_POST["HouseID"];
$list = $_POST["list"];
$order = $_POST["order"];

include "connect.php";

$query = "SELECT `HouseID`,`AskingPrice`,`SoldFor`,`DatePurchased` FROM purchase where `HouseID` = $HouseID ORDER BY  ";
$result = mysqli_query($con, $query) or die($mysqli->connect_error); 

echo "<table border='1'><tr><th>House ID</th><th>Asking Price</th><th>Sold For</th><th>Date Purchased</th></tr>";  
$row = mysqli_fetch_array($result);


echo "<tr><td>" . $row[0] .  "</td><td>" .  $row[1] .  "</td><td>" .  $row[2] .  "</td><td>" .  $row[3] .  "</td></tr>";
echo "</table>"; 
mysqli_free_result($result);
mysqli_close($conn); 

?>

Upvotes: 0

Views: 95

Answers (3)

Alexandru  Gojinetchi
Alexandru Gojinetchi

Reputation: 194

In your select you use WHERE and ,this clause return just 1 row;

Upvotes: 0

I thought you meant that the options were not shown completely

Well for the just add a loop:

Replace this:

echo "<table border='1'><tr><th>House ID</th><th>Asking Price</th><th>Sold For</th><th>Date Purchased</th></tr>";  
$row = mysqli_fetch_array($result);


echo "<tr><td>" . $row[0] .  "</td><td>" .  $row[1] .  "</td><td>" .  $row[2] .  "</td><td>" .  $row[3] .  "</td></tr>";
echo "</table>"; 
mysqli_free_result($result);
mysqli_close($conn); 

With this:

echo "<table border='1'><tr><th>House ID</th><th>Asking Price</th><th>Sold For</th><th>Date Purchased</th></tr>";  
while($row = mysqli_fetch_array($result))  {

echo "<tr><td>" . $row[0] .  "</td><td>" .  $row[1] .  "</td><td>" .  $row[2] .  "</td><td>" .  $row[3] .  "</td></tr>";
}
echo "</table>"; 
mysqli_free_result($result);
mysqli_close($conn); 

So, i think, should work...

Upvotes: 0

Pupil
Pupil

Reputation: 23948

You need to loop trough the array.

Otherwise, it will show only last element.

echo "<table border='1'><tr><th>House ID</th><th>Asking Price</th><th>Sold For</th><th>Date Purchased</th></tr>";  
while ($row = mysqli_fetch_array($result)) {
  echo "<tr><td>" . $row[0] .  "</td><td>" .  $row[1] .  "</td><td>" .  $row[2] .  "</td><td>" .  $row[3] .  "</td></tr>";
}
echo "</table>"; 
mysqli_free_result($result);
mysqli_close($conn);

Upvotes: 1

Related Questions