Jump to content

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