Jump to content

Showing mysql php results in html table


dropbop

Recommended Posts

I was wondering how does one go about showing results from SELECT query in columns in a html table.

 

I have a list of products in a table, and would like to show them on the page in 4 columns. I have done many searches on google to try and find the sulution, but the majority of what im finding instead is about displaying a table from phpmyadmin as a table in html.

 

If its a large operation to do this, I would be very happy if someone could poiint me in the direction of a tutorial maybe.

 

Here is the code I have so far to display the products, but for some reason, it only show 1 row instead of all the rows from my table.

 

<?php

$dbhost = "localhost";
$dbuser = "user";
$dbpass = "pass";
$dbname = "dbname";

mysql_connect ($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$result = mysql_query("SELECT * FROM mcproducts");
while($row = mysql_fetch_array($result))
{
  $products_local_id = $row['products_local_id'];
  $productname = $row['product_name'];
  $thumburl = $row['image_from_url'];
  $productlink = $row['product_local_url'];
  $thumbnail = $row['product_image_small'];
  $currencysymbol = $row['product_currency'];
  $price = $row['product_price'];
  $flagicon = $row['product_country_from'];

}

?> 
<html>
<head>
<link href="style/stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>

<div class="displaybox">
                <div class="productimage">
                <a href="<?php echo $productlink; ?><?php echo $products_local_id; ?>"><img src="<?php echo $thumburl; ?><?php echo $thumbnail ?>" width="150" height="150"></a>
                </div>
                <div class="productdescription">
                  <div class="pro_name">
		      <a href="<?php echo $productlink ?><?php echo $products_local_id; ?>"><?php echo $productname; ?></a>
                  </div>
                  <div class="pro_description">
                  </div>
                  <?php
                  if ($flagicon=="Ireland") {
				  $flagicon = "<img src=\"flags/ireland.jpg\">";
				  }
				  
				  elseif ($flagicon=="UK") {
					  $flagicon = "<img src=\"flags/uk.jpg\">";
					  }  else
					  echo "";
  ?>
                  <div class="pro_description"><?php echo $flagicon; ?><?php echo $currencysymbol ?> <?php echo $price ?></div>
                </div>
              </div>
              
</body>
</html>

 

Many thanks,

 

DB

Link to comment
https://forums.phpfreaks.com/topic/245956-showing-mysql-php-results-in-html-table/
Share on other sites

Ah yes!!, well spotted, never even occured to me....  :)  cheers for that.

 

Here is what I have now and with a little help from css, I have it showing as many columns as will fit :)

 

<?php
mysql_connect ($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$output = "";
$result = mysql_query("SELECT * FROM mcproducts ORDER BY RAND() LIMIT 10");
while($row = mysql_fetch_array($result))
{
  $products_local_id = $row['products_local_id'];
  $productname = $row['product_name'];
  $thumburl = $row['image_from_url'];
  $productlink = $row['product_local_url'];
  $thumbnail = $row['product_image_small'];
  $currencysymbol = $row['product_currency'];
  $price = $row['product_price'];
  $flagicon = $row['product_country_from'];
  $output = "
  <div class=\"productimage\">
                <a href=".$productlink."".$products_local_id."><img src=".$thumburl."".$thumbnail." width=\"150\" height=\"150\"></a>
                </div>
                <div class=\"productdescription\">
                  <div class=\"pro_name\">
		      <a href=".$productlink."".$products_local_id.">".$productname."</a>
                  </div>
                  <div class=\"pro_description\">
                  </div>
                  <div class=\"pro_description\">".$flagicon." ".$currencysymbol." ".$price."</div>
                </div>

         ";

?> 
<html>
<head>
<link href="style/stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="displaybox">
                <div class="productimage">
                <a href="<?php echo $productlink; ?><?php echo $products_local_id; ?>"><img src="<?php echo $thumburl; ?><?php echo $thumbnail ?>" width="150" height="150"></a>
                </div>
                <div class="productdescription">
                  <div class="pro_name">
		      <a href="<?php echo $productlink ?><?php echo $products_local_id; ?>"><?php echo $productname; ?></a>
                  </div>
                  <div class="pro_description">
                  </div>
                  <?php
                  if ($flagicon=="Ireland") {
				  $flagicon = "<img src=\"flags/ireland.jpg\">";
				  }
				  
				  elseif ($flagicon=="UK") {
					  $flagicon = "<img src=\"flags/uk.jpg\">";
					  }  else
					  echo "";
  ?>
                  <div class="pro_description"><?php echo $flagicon; ?><?php echo $currencysymbol; ?> <?php echo $price; ?></div>
                </div>
              </div>
              <?php } ?>
</body>
</html>

 

Thanks again AyKay47, that was a quick resonse from you... really appreciated indeed :)

 

DB

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.