Jump to content

isplay product name with Product URL


antonio12

Recommended Posts

Hello MySQL/PHP Experts

 

I this piece of code and I can not seem to figure out why my images are not being displayed. I have the IMAGEURL link stored in my MySQL database. I have the FULL link stored in the database and it still will not display the image. Here is my short code:

 

<?php 
// Make a MySQL Connection 
mysql_connect("IPADDRESS","databaseID","password") or die(mysql_error()); 
mysql_select_db("dbname") or die(mysql_error()); 

// Get all the data from the "example" table 
$result = mysql_query("SELECT * FROM producttable") 
or die(mysql_error()); 

echo "<table border='1'>"; 
echo "<tr> <th>Product ID</th><th>IMAGE PICTURE</th> <th>Product Name</th> </tr>"; 
// keeps getting the next row until there are no more to get 
while($row = mysql_fetch_array( $result )) { 
// Print out the contents of each row into a table 
echo "<tr><td>"; 
echo $row['ProductID']; 
echo "</td><td>"; 
echo "<img src='<?= [imageURL] ?>' />"; 
echo "</td><td>"; 
echo $row['ProductName']; 
echo "</td></tr>"; 
} 

echo "</table>"; 
?> 

 

Can someone help explain to me what I am doing wrong?

 

Thank you in advance!

 

Regards,

 

Antonio

Link to comment
https://forums.phpfreaks.com/topic/135200-isplay-product-name-with-product-url/
Share on other sites

Rhodesa if you are out there - help one more time please!

 

I can not seem to display the URL associated with each product name now. I have tried every which way but no luck!

 

Please help!

 

<?php
// Make a MySQL Connection
mysql_connect("IPADDRESS", "databasename","password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM PRODUCTS") 
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Product ID</th><th>IMAGE PICTURE</th> <th>Product URL</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['ProductID'];
echo "</td><td>";
    echo "<img src='{$row['ImageURL']}' />";  
echo "</td><td>"; 
echo  "<a href='<?= {$row['BuyURL']} ?>'><?'{$row['ProductName']}'></</a>";
echo "</td></tr>"; 
} 

echo "</table>";
?>

 

Thanks again in advance.

 

Regards,

 

Antonio

ok...we need to cover some PHP basics...to print a variable you have a few options...

 

if PHP is NOT started (aka, we are not in PHP tags) you can do the following:

<table>
  <tr>
    <td>The common way:</td>
    <td><?php echo $variable; ?></td>
  </tr>
  <tr>
    <td>The shorthand way:</td>
    <td><?=$variable?></td>
  </tr>
</table>

 

now, in your case, PHP is already started (aka we are in PHP tags):

echo "<table>";
echo "<tr>";
echo "<td>This way, we stop the string, print the variable, the start it again</td>";
echo "<td>".$variable."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>This way, we put the variable inside the string, PHP will convert it to it's value for us:</td>";
echo "<td>$variable</td>";
echo "</tr>";
echo "<tr>";
echo "<td>But, with arrays, we need to put braces around it (you can do this for regular scalar variables too:</td>";
echo "<td>{$row['variable']}</td>";
echo "</tr>";
echo "</table>";

 

you are mixing the two methods. by using echo and <?={$row['BuyURL']}?>...just use one:

echo  "<a href='{$row['BuyURL']}'>{$row['ProductName']}</a>";

 

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.