Jump to content

Making a table from results


spearchilduser

Recommended Posts

Ive gotten some results user selcts check box on first page

The php page will say which brackets it falls between example price is between 100-300 say

 

I dont seem to be able to populate a table with the data in the database:S

 

$row_number= 0;

while ($row = mysql_fetch_array($result3)) 
    {
	If  (($row["price"] = $price_low) && ($row["price"] <= $price_high)) //price
		If (($row["storage"] = $storage_low) && ($row["storage"] <= $storage_high)) //storage
			if (($row["Processor "] = $processor_low) && ($row["Processor"] <= $processor_high))  //Processor 
				 { $row_number++; 

?>

		<tr> 

			<td align="center"><?php print $row["Computer_Price"]; ?> </td>
			<td align="center"><?php print $row["Computer_Storage"]; ?> </td>
			<td align="center"><?php print $row["Computer_ProcessorSpeed"]; ?> </td>

		</tr>
</table>

 

Thats wat im usign atm the price_low and price_high are what sets the low and high price for the search

Link to comment
Share on other sites

sorry i missed the beginning

HTML>
<BODY>
<table width="75%" border="1" align="center">
  <th> 
  <td align="center">SELECTED Computers</td>
  <tr>
    <td>Price</td>
<td>Storage</td>
<td>Processor</td>
  </tr>
<?PHP

 

is just above the previosu code so if i move the table outside of the while loop it shoudl be ok ? from the syntax etc nothgin else i might need to add?

Link to comment
Share on other sites

This is how you would usually set up the loops to create a table (psuedo code):

 

<table>

while loop condition {

  <tr><td>

    //add content to the table cell

  </td></tr>

 

}//close while loop

</table>

 

That code will create one table, will multiple table rows, each with only one column.

Link to comment
Share on other sites

Ok cheers but im confused as to how to do this not the way i have

 

as i cant wrap the whoel thing in PHP tags cos itll geta  error so as ive doen ive ended them after the whiel loop then made the 2nd row of the table and doen the titel row before the whiel loop is there a different way to do it ?

<table width="75%" border="1" align="center">
  <th> 
  <td align="center">SELECTED Computers</td>
  <tr>
    <td>Price</td>
<td>Storage</td>
<td>Processor</td>
  </tr>
  
<?PHP
  
while ($row = mysql_fetch_array($result)) 
    {
	If  (($row["price"] = $price_low) && ($row["price"] <= $price_high)) //price
		If (($row["storage"] = $storage_low) && ($row["storage"] <= $storage_high)) //storage
			if (($row["Processor "] = $processor_low) && ($row["Processor"] <= $processor_high))  //Processor 
				 { $row_number++; 
				 }};
?>

		<tr> 

			<td align="center"><?php print $row["Price"]; ?> </td>
			<td align="center"><?php print $row["Storage"]; ?> </td>
			<td align="center"><?php print $row["Processor"]; ?> </td>

		</tr>
</table>

 

Im relatively new so i cant think of another way to do it sorry

Link to comment
Share on other sites

yes i can ill brefily exaplin the point : its a very simpel recomendation system where the first page the user selects a check box.

On the php page it works out the average of each attibute in the databse and stores these in functions.i also have fucntiosn set up to work out 75% of the average 125% of the average.

Then a switch will say the low and high values of the selection using those average fucntions and hopefully show the results for it.

 

if that makes any scence

 

<?php
mysql_connect("localhost", "root") or die(mysql_error());											// makes a connection
mysql_select_db("computers") or die('I cannot connect to the database because: ' . mysql_error()); //conects to the database

$query = "SELECT * FROM computer_details";       //selects all data from the computer details table and stores it
$query1 = "SELECT * FROM computer_details";		//selects all data from the computer details table and stores it
$query2 = "SELECT * FROM computer_details";		//selects all data from the computer details table and stores it
$query3 = "SELECT * FROM computer_details";		//selects the data from the computer price field in the computer details table and stores it


// store the records of the queries into a reuslts variable
$result = mysql_query($query)
or die(mysql_error());  
$result1 = mysql_query($query1)
or die(mysql_error());  
$result2 = mysql_query($query2)
or die(mysql_error());  
$result3 = mysql_query($query3)
or die(mysql_error());  




$total_price = 0; 
$total_storage = 0; 
$total_processor = 0;                                 
$number_of_items = 0;
@$price = $_POST['price'];
@$processor=$_POST['processor'];
@$storage=$_POST['storage'];


?>

<?php
while($row = mysql_fetch_array($result))             
    {
		$total_price = $row["Computer_Price"] + $total_price ;      
		$total_items = $number_of_items = $number_of_items + 1;

} 

$av_price = $total_price/$total_items; 
// finds the average of the price in the computer price field int he datase by addign all the values up and dividing by the total items
echo 'Average Price: ' ;
print $av_price;  print "<br>";
// displays the average price
while($row = mysql_fetch_array($result1))             
    {
		$total_storage = $row["Computer_Storage"] + $total_storage ;      
		$total_items = $number_of_items = $number_of_items + 1;

} 

$av_storage = $total_storage/$total_items; 
// finds the average of the storage space in the computer price field int he datase by addign all the values up and dividing by the total items

echo 'Average Storage: ' ;
print $av_storage;  print "<br>";

while($row = mysql_fetch_array($result2))             
    {
		$total_processor = $row["Computer_ProcessorSpeed"] + $total_processor ;      
		$total_items = $number_of_items = $number_of_items + 1;

} 

$av_processor = $total_processor/$total_items; 
// finds the average of the processor speed in the computer price field int he datase by addign all the values up and dividing by the total items

echo 'Average Processor Speed: ' ;
print $av_processor; print "<br>";print "<br>";print "<br>";



//Price
$lowav_price = 0;
$medlowav_price = 0.75 * $av_price;
$highlowav_price = 1.25 * $av_price;
$lowav_storage = 0;
$medlowav_storage = 0.75 * $av_storage;
$highlowav_storage = 1.25 * $av_storage;
$lowav_processor = 0;
$medlowav_processor= 0.75 * $av_processor;
$highlowav_processor = 1.25 * $av_processor;
// works out a percentage of the average from the $av_price/storage/processor variables


// for the follwing bit the user will select a check box which will have a value and these are the cases for each option.
//each option is set ot be either 0 - 75% of the average or 75%- 125 % of the average or 125% and above based on the average of the Data in the table
switch ($price)
{

case '1' : $price_low = 0; $price_high = $medlowav_price ; print " (I would like to pay less and get less specifications) < ".$price_low."-".$price_high.">"; break;  
case '2' : $price_low = $medlowav_price; $price_high = $highlowav_price; print " (I would like to pay for what I get so what I get is equivalent to the price) < ".$price_low."-".$price_high.">"; break; 
case '3' : $price_low = $medlowav_price; $price_high = $highlowav_price ; print " (I would prefer to Pay as much as possible to get the best computer) < ".$price_low."-".$price_high.">"; break; 
case '4' : $price_low = 0; $price_high =  $highlowav_price ; print " (I have no preference) < ".$price_low."-".$price_high.">"; break;
}
print ' price option = '.$price; print "<br>";print "<br>";

switch ($storage)
{

case '1' : $storage_low= 0; $storage_high = $medlowav_storage ; print " (I dont want a lot of storage space) < ".$storage_low."-".$storage_high.">"; break;  
case '2' : $storage_low = $medlowav_storage; $storage_high = $highlowav_storage; print " (I would like to have a decent amount of storage space) < ".$storage_low."-".$storage_high.">"; break; 
case '3' : $storage_low = $medlowav_storage; $storage_high = $highlowav_storage; print " (I prefer to pay more for bigger storage) < ".$storage_low."-".$storage_high.">"; break; 
case '4' : $storage_low= 0; $storage_high =  $highlowav_storage ; print " (I have no preference) < ".$storage_low."-".$storage_high.">"; break;
}
print ' storage option = '.$storage; print "<br>";print "<br>";

switch ($processor)
{

case '1' : $processor_low = 0; $processor_high = $medlowav_processor ; print " (I only use the computer for the internet or a word processor ) < ".$processor_low."-".$processor_high.">"; break;  
case '2' : $processor_low = $medlowav_processor; $processor_high = $highlowav_processor; print " (I generally only have a couple of programs open at a time ) < ".$processor_low."-".$processor_high.">"; break; 
case '3' : $processor_low = $medlowav_processor; $processor_high = $highlowav_processor; print " (Yes I generally like to have as many programs as I can active at a time without slowing the computer down) < ".$processor_low."-".$processor_high.">"; break; 
case '4' : $processor_low = 0; $processor_high =  $highlowav_processor ; print " (I have no preference) < ".$processor_low."-".$processor_high.">"; break;
}
print ' processor option = '.$processor; print "<br>";print "<br>";
?>

<HTML>
<BODY> 
<table width="75%" border="1" align="center">
  <th> 
  <td align="center">SELECTED Computers</td>
  <tr>
    <td>Price</td>
<td>Storage</td>
<td>Processor</td>
  </tr>
  
<?PHP
  
while ($row = mysql_fetch_array($result)) 
    {
	If  (($row["price"] = $price_low) && ($row["price"] <= $price_high)) //price
		If (($row["storage"] = $storage_low) && ($row["storage"] <= $storage_high)) //storage
			if (($row["Processor "] = $processor_low) && ($row["Processor"] <= $processor_high))  //Processor 
				 { $row_number++; 
				 }};
?>

		<tr> 

			<td align="center"><?php print $row["Price"]; ?> </td>
			<td align="center"><?php print $row["Storage"]; ?> </td>
			<td align="center"><?php print $row["Processor"]; ?> </td>

		</tr>
</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.