Please can someone help, I'm new to PHP and I need to know how to display the data from mysql database using PHP in a format like so

ProductA(image) - ProductB(image) - ProductC(image)
ProdAName - ProdBName - ProdCName
ProdAPrice - ProdBPrice - ProdCPrice

I've managed to get the Product images displaying ok using this code

<?php
// Connect to Database
$con = mysql_connect("localhost","user","passwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Select the Database
mysql_select_db("dbname", $con);

// Select the data from table_name
$result = mysql_query("SELECT product_name, network_deep_link, network_image_url, search_price FROM wp_products ORDER BY search_price");

// Display the table
echo "<table width='400' border='0' cellspacing='0' cellpadding='1'>";

// Put results into table
 $counter = 0;
while ( $row = mysql_fetch_assoc( $result ) )
{
  echo "<a href=\"" .$row['network_deep_link'] . "\">
  <img src=\"" . $row['network_image_url'] . "\" border=0 alt=\"" . $row["network_image_url"] . "\" title=\"" . $row["product_name"] . "\">
  </a>";
    $counter++; # increment count every iteration
  if ( $counter % 3 == 0 )
  {
      echo "<br>";
  }
}

// Disconnect from Database
mysql_close($con);
?>

But what I also need to do is add ProdAName and ProdAPrice on the next lines like so I'm now using tables but have hit a problem when trying to put the results in <br>. If I put text inbetween the <br>text<br> it works, but not if I put .$row[product_name]

// Put results into table
while($row = mysql_fetch_array($result))
  {
echo "<tr>";
echo "<td><a href=\"" .$row['network_deep_link'] . "\">
<img src=\"" . $row['network_image_url'] . "\" border=0 alt=\"" . $row["network_image_url"] . "\" title=\"" . $row["product_name"] . "\"></a>";
echo "<br>.$row['product_name']<br>";
echo "<br>.$row['search_price']<br>";
echo "</td>";
  }
echo "</table>";

Then I need to loop it like the first code example using the counter

Any help appreciated

Thanks
Gary

Recommended Answers

All 9 Replies

are you trying to show multiple things on one page or more then one ?

i think that would do it

<?php


 $i = 0;

		$query = "SELECT * FROM products WHERE (`products`.`some` = '".$something."')"; 
 $result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_BOTH)){
$product[$i] = $row[0];
$image[$i] = $row[2];
$price[$i] = $row[3];
$i = $i + 1;
//echo $ij;
}
$i = $i - 1;




echo "<table bgcolor='#A5A5A5' bordercolor='#A5A5A5' bordercolordark='#787878' bordercolorlight='#878787'>"
echo "<tr>";
echo "<td>PRODUCT</td>";
echo "<td>IMAGE</td>";
echo "<td>PRICE</td>";
echo "</tr>";
for ( $counter = 0 ; $counter <= $i; $counter += 1) { 
 echo "<tr>";
echo	"<td><?PHP echo $product[$counter]; ?></td>";
echo	"<td><?PHP echo $image[$counter]; ?></td>";
echo	"<td><?PHP echo $price[$counter]; ?></td>";
echo "</tr>"
 } 
echo "</table>";

?>

Hi Hitmanof44th,

Thanks for your reply

I'm trying to show the productimage with the product name underneath and the price underneath this, then repeat this from left to right, like so:

1 2 3
4 5 6

Thanks

are you trying to show multiple things on one page or more then one ?

Hi Hitmanof44th,

Tried this and change the code to the fields from mysql database etc, but it didn't work. Also I think if my understanding is correct, this will only display product then image then price in row 1 then the next product image and price in the following row and so on. Correct me if I'm wrong.

Your help is appreciated

Thanks
Gary

i think that would do it

<?php


 $i = 0;

		$query = "SELECT * FROM products WHERE (`products`.`some` = '".$something."')"; 
 $result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_BOTH)){
$product[$i] = $row[0];
$image[$i] = $row[2];
$price[$i] = $row[3];
$i = $i + 1;
//echo $ij;
}
$i = $i - 1;




echo "<table bgcolor='#A5A5A5' bordercolor='#A5A5A5' bordercolordark='#787878' bordercolorlight='#878787'>"
echo "<tr>";
echo "<td>PRODUCT</td>";
echo "<td>IMAGE</td>";
echo "<td>PRICE</td>";
echo "</tr>";
for ( $counter = 0 ; $counter <= $i; $counter += 1) { 
 echo "<tr>";
echo	"<td><?PHP echo $product[$counter]; ?></td>";
echo	"<td><?PHP echo $image[$counter]; ?></td>";
echo	"<td><?PHP echo $price[$counter]; ?></td>";
echo "</tr>"
 } 
echo "</table>";

?>

ok i see what your trying to do give me a sec i'll change it around

this would make your tables

and did you get an error on the query ?

i set $a on how many down and $b on how many across

<?PHP
echo "<table>";
 for ( $counter1 = 1 ; $counter1 <= $a; $counter1 += 1) {
 
echo "<tr>";
 	for ( $counter = 1 ; $counter <= $b; $counter += 1) {
echo 	"<td>$image[$counter]</br>$name[$counter]</br>$price[$counter]</td>";
 	}
echo "</tr>";
}
echo "</table>";

?>

Hi again hitman,

Thanks for the quick response. I have tried this and it just gives a blank page.

Here is the amended code as I tried with the code you gave and also notice 2 lines appeared the same where the for loop statements are

<?php

// Connect to Database
$con = mysql_connect("localhost","user","passwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Select the Database
mysql_select_db("dbname", $con);

// Select the data from table_name
$result = mysql_query("SELECT product_name, network_deep_link, network_image_url, search_price FROM wp_products ORDER BY search_price");

// Display the table
echo "<table border='1' cellspacing='16' cellpadding='1'>";

// Put results into table
while($row = mysql_fetch_array($result))

echo "<tr>";
for ( $counter = 1 ; $counter <= $b; $counter += 1) {
echo 	"<td>$network_deep_link[$counter]</br>$produce_name[$counter]</br>$search_price[$counter]</td>";
}
echo "</tr>";
}
echo "</table>";


?>

Any ideas?

this would make your tables

and did you get an error on the query ?

i set $a on how many down and $b on how many across

<?PHP
echo "<table>";
 for ( $counter1 = 1 ; $counter1 <= $a; $counter1 += 1) {
 
echo "<tr>";
 	for ( $counter = 1 ; $counter <= $b; $counter += 1) {
echo 	"<td>$image[$counter]</br>$name[$counter]</br>$price[$counter]</td>";
 	}
echo "</tr>";
}
echo "</table>";

?>

you can give that a whirl

you just have to look at the structure of your database and fill in the rows the stuff is on

$query = "SELECT * FROM wp_products ORDER BY search_price"; 
 $result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_BOTH)){

//now just put in which row the item is on / in the database
$network_deep_link[$i] = $row[0];
$produce_name[$i] = $row[1];
$search_price[$i] = $row[2];
$i = $i + 1;
} 
$i= $i - 1;

Thanks for your help so far, but this doesn't acheive what I need to do either.

My database consists of several fields (columns) of which I am trying to pull the data in the rows from those specific fields. I need to return those specific fields from all rows.

If I put this in the code, it will display the products left to right in 3's. But when I use tables it doesn't work.

Also when I put in another line for the .$row['search_price] to be display under each picture it doesn't align to the correct image.

// Put results into page
 $counter = 0;
while ( $row = mysql_fetch_assoc( $result ) )
{
  echo "<a href="" .$row['network_deep_link'] . "">
  <img src="" . $row['network_image_url'] . "" border=0 alt="" . $row["network_image_url"] . "" title="" . $row["product_name"] . "">
  </a>";
    $counter++; # increment count every iteration
  if ( $counter % 3 == 0 )
  {
      echo "<br />";
  }
}

you can give that a whirl
you just have to look at the structure of your database and fill in the rows the stuff is on

> 
> 
>     $query = "SELECT * FROM wp_products ORDER BY search_price"; 
>      $result = mysql_query($query) or die(mysql_error());
>     while($row = mysql_fetch_array($result, MYSQL_BOTH)){
>     
>     //now just put in which row the item is on / in the database
>     $network_deep_link[$i] = $row[0];
>     $produce_name[$i] = $row[1];
>     $search_price[$i] = $row[2];
>     $i = $i + 1;
>     } 
>     $i= $i - 1;
> 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.