Jump to content

Img src construction problem


KevinM1

Recommended Posts

I'm trying to dynamically create an image's src attribute.  I have the following echo statement:
[code]
<?php

echo "<img src='../images/store/{$tablename}_banner.jpg' alt='' /><a href='viewcart.php'><img src='../images/store/storefront_02.jpg' alt='View Cart' /></a>\n\n<div>";

?>
[/code]

The problem is that $tablename is not inserted into the image's src attribute, leaving me with the script trying to display /images/store/_banner.jpg, which is definitely not what I want.  Please help.
Link to comment
https://forums.phpfreaks.com/topic/33615-img-src-construction-problem/
Share on other sites

$tablename is simply created through a $_GET assignment:
[code]
<?php

$tablename = $_GET['cat'];

?>
[/code]

My full script is:
[code]
<?php

#viewcat.php script

session_start();
ob_start();

include('../php_config/config.php');
include('../dbconnect.php');
include('../templates/sub_header.inc');

if(isset($_GET['cat'])){
   $tableName = $_GET['cat'];
}

else{
   $_SESSION['ip'] = urlencode(serialize($ip));
   $_SESSION['myCart'] = urlencode(serialize($myCart));
   header("Location: http://www.thinkingmachinestore.com/storefront.php");
   exit();
}

$query = "SELECT * FROM $tableName WHERE availability='y'";
$result = mysql_query($query);

echo "<div style='margin-left: auto; margin-right: auto; text-align: center;'><img src='../images/store/{$tablename}_banner.jpg' alt='' /><a href='viewcart.php'><img src='../images/store/storefront_02.jpg' alt='View Cart' /></a>\n<br /><br />\n";

$count = 0;

while($row = mysql_fetch_assoc($result)){
   $id = $row["$tableName" . "_id"];
   $pic = $row["pic_url"];
   echo "<a href='viewitem.php?cat=$tableName&id=$id'><img src='$pic' alt='' /></a>";
   $count++;

   if($count == 2){
      echo "<br />\n";
      $count = 0;
   }
}

echo "</div>";

include('../templates/sub_footer.inc');

?>
[/code]

EDIT: When I remove the brackets from $tablename, my src name actually shrinks to /images/store/.jpg, so that's not the problem.

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.