CNGHL Posted November 1, 2015 Share Posted November 1, 2015 HI There, I am trying to separate two variables in a image string my code looks like this ECHO "<td width='100'><div align='center'><img src=\"http://www.brickbybricks.ca/php/images/".$row['Color_Name'].$row['PartID'].".gif\"></td>"; I cannot seem to figure out what to put between the two variables to make the img src work, the way it is right now I get the link back looking like this. <img src="http://www.brickbybricks.ca/php/images/Black4733.gif"> So I need a slash in between black at 4733 Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/298973-separating-two-variables-in-an-image-string/ Share on other sites More sharing options...
ginerjm Posted November 1, 2015 Share Posted November 1, 2015 You didn't escape the " at the end of the src= string. Try using single quotes so you don't have to escape the doubles. echo "<td width='100'><div align='center'><img src='http://www.brickbybricks.ca/php/images/" . $row['Color_Name'].$row['PartID'] . ".gif'></td>"; See how the entire string is in double quotes but the attributes inside of the tags are in single ones? Quote Link to comment https://forums.phpfreaks.com/topic/298973-separating-two-variables-in-an-image-string/#findComment-1525042 Share on other sites More sharing options...
CNGHL Posted November 1, 2015 Author Share Posted November 1, 2015 Thanks, I fixed the string, but how do I separate the two variable in the img src. Quote Link to comment https://forums.phpfreaks.com/topic/298973-separating-two-variables-in-an-image-string/#findComment-1525045 Share on other sites More sharing options...
Solution benanamen Posted November 1, 2015 Solution Share Posted November 1, 2015 (edited) echo "<td width=\"100\"><div align=\"center\"><img src=\"http://www.brickbybricks.ca/php/images/{$row['Color_Name']}/{$row['PartID']}.gif\"></td>"; See how the entire string is in double quotes but the attributes inside of the tags are in single ones? The only thing about that is you have no choice but to escape and concatenate the variables instead of doing {$row['Color_Name']} (Curly Syntax, my preference). Edited November 1, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298973-separating-two-variables-in-an-image-string/#findComment-1525050 Share on other sites More sharing options...
CNGHL Posted November 1, 2015 Author Share Posted November 1, 2015 Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/298973-separating-two-variables-in-an-image-string/#findComment-1525051 Share on other sites More sharing options...
Jacques1 Posted November 1, 2015 Share Posted November 1, 2015 (edited) Dumping raw variables into HTML markup is insecure and can lead to very nasty bugs. For example, if your color happens to contain a special character, you'll quickly run into syntax conflicts. And if an attacker is able to control the variables (e. g. by manipulating your database), this code can be used to perform a cross-site scripting attack. Always URL-encode and HTML-escape variables before inserting them into links: <?php function html_escape($raw_input, $encoding) { return htmlspecialchars($raw_input, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, $encoding); } $image_url = 'http://www.brickbybricks.ca/php/images/'.rawurlencode($row['Color_Name']).'/'.rawurlencode($row['PartID']).'.gif'; echo '<td width="100"><div align="center"><img src="'.html_escape($image_url, 'UTF-8').'"></td>'; Note that using style attributes like width or align is heavily outdated. You should be using CSS. It's also bad practice to mix HTML markup into PHP code. Keep them separate: <?php // Your PHP code goes here ?> <!-- Your HTML markup goes here --> Edited November 1, 2015 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/298973-separating-two-variables-in-an-image-string/#findComment-1525053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.