biggieuk Posted March 20, 2009 Share Posted March 20, 2009 Hi all, Im having a little trouble finding out how to parse a variable within an echo. Here is my code: echo "<img src='../images/bullets/bullet_black.png' title='".$desc1."' width='10' height='10' />"; Im trying to echo out $desc1 which is a value from my database. Thanks for help with this. Dan Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/ Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 $desc1 is the title of the image correct. are you sure your using a while loop or loop on the code. $row['desc1'] Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-789880 Share on other sites More sharing options...
Mchl Posted March 20, 2009 Share Posted March 20, 2009 This line looks good. Show us the code you use to get data from database. Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-789884 Share on other sites More sharing options...
Malevolence Posted March 21, 2009 Share Posted March 21, 2009 echo "<img src='../images/bullets/bullet_black.png' title='".$desc1."' width='10' height='10' />"; is better off being: <img src="../images/bullets/bullet_black.png" title="<?=$desc1?>" width="10" height="10" /> if the img tag is inside other html stuff... seeing as php has to do less work! If quick echo tags is disabled, you could just do <?php echo $desc1 ?> where <?=$desc1?> is. Also, mysql (which is what I assume you are using requires: a connection and a selection to a database, a query to a table and then some kind of data handling function with that query such as, mysql_get_assoc will create an associative array where you can then call up the 'row' data by field name(s).. You probably knew that last bit but covering everythign. PS: you should get into the habit of using mysqli (mysql improved), seeing as its safer, more secure (by far) and hey, it's newer (so long as its supported, which it should be >_>) Look at the php manuals. - hope I'm not cramming too much in this post :s Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-789984 Share on other sites More sharing options...
bluejay002 Posted March 21, 2009 Share Posted March 21, 2009 yup... please post your the code prior to that one. Looking at that, it should be clean and Malevolence is also a good suggestion since I use that way also. Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-789991 Share on other sites More sharing options...
biggieuk Posted March 21, 2009 Author Share Posted March 21, 2009 Thanks for your replies. Sorry for not being a bit more specific. I have $desc1 = $imgtitle['desc1']; Is what i used for the variable and has been tested and its outputting fine. I am using the echo method as its actually setting these image properties within a function. I could change this if needs be though. Not too sure why this isn't working then. I'll try another way in the morning if i cant find a solution. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-790003 Share on other sites More sharing options...
bluejay002 Posted March 21, 2009 Share Posted March 21, 2009 That sure is weird. From how I see it, it should work fine given it really has a value. Does the tested word you said means it displays a value when used on echo? Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-790010 Share on other sites More sharing options...
DeanWhitehouse Posted March 21, 2009 Share Posted March 21, 2009 @Malevolence Except it is considered bad practice to use PHP short tags Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-790247 Share on other sites More sharing options...
biggieuk Posted March 21, 2009 Author Share Posted March 21, 2009 I think its maybe because i am using a function to get the values. Here is the full code in between my head tags: for ( $counter = 1; $counter <= 10; $counter += 1) { mysql_select_db($database_hwbltaconf, $hwbltaconf); $query_colour = "SELECT * FROM colours WHERE colourid = ".$counter; $colour = mysql_query($query_colour, $hwbltaconf) or die(mysql_error()); $row_colour = mysql_fetch_assoc($colour); $var1 = 'desc'; $var2 = $counter; ${$var1.$var2} = $row_colour['colourdesc']; } function checkMarker($rsval){ if($rsval == "green"){ }else if($rsval == "yellow"){ echo "<img src='../images/bullets/bullet_yellow.png' title='".$desc10."' width='10' height='10' />"; }else if($rsval == "red"){ echo "<img src='../images/bullets/bullet_red.png' title='".desc8."' width='10' height='10' />"; }else if($rsval == "black"){ echo "<img src='../images/bullets/bullet_black.png' title='".desc1."' width='10' height='10' />"; }else if($rsval == "blue"){ echo "<img src='../images/bullets/bullet_blue.png' title='".desc2."' width='10' height='10' />"; }else if($rsval == "orange"){ echo "<img src='../images/bullets/bullet_orange.png' title='".desc5."' width='10' height='10' />"; }else if($rsval == "pink"){ echo "<img src='../images/bullets/bullet_pink.png' title='".desc6."' width='10' height='10' />"; }else if($rsval == "purple"){ echo "<img src='../images/bullets/bullet_purple.png' title='".desc7."' width='10' height='10' />"; }else if($rsval == "bronze"){ echo "<img src='../images/bullets/bullet_bronze.png' title='".desc3."' width='10' height='10' />"; }else if($rsval == "white"){ echo "<img src='../images/bullets/bullet_white.png' title='".desc9."' width='10' height='10' />"; }else{ echo "<img src='../images/bullets/bullet_white.png' title='".desc9."' width='10' height='10' />"; } } And this is the code used to call the function in the body of the doc: <td valign="middle"><? checkMarker($row_sessiondetails['marker1']); ?></td> <td valign="middle"><? checkMarker($row_sessiondetails['marker2']); ?></td> <td valign="middle"><? checkMarker($row_sessiondetails['marker3']); ?></td> This returns a different coloured icon depending on which value is passed in with the function but the title doesn't work on hover. I found a different method using a CSS popup: echo "<a href=\"#\" class=\"tt\"><img src=\"../images/bullets/bullet_green.png\" width=\"10\" height=\"10\" /><span class=\"tooltip\"><span class=\"top\"></span><span class=\"middle\">".$desc4."</span><span class=\"bottom\"></span></span></a>"; which doesn't seem to work either.. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-790256 Share on other sites More sharing options...
redarrow Posted March 21, 2009 Share Posted March 21, 2009 if($rsval == "green"){ where green mate. Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-790258 Share on other sites More sharing options...
redarrow Posted March 21, 2009 Share Posted March 21, 2009 clened the function up, also you had no $<<<< in front off variables. <?php function checkMarker($rsval){ SWITCH($rsval){ CASE "yellow": echo "<img src='../images/bullets/bullet_yellow.png' title='".$desc10."' width='10' height='10' />"; BREAK; CASE "red": echo "<img src='../images/bullets/bullet_red.png' title='".$desc8."' width='10' height='10' />"; BREAK; CASE "black": echo "<img src='../images/bullets/bullet_red.png' title='".$desc8."' width='10' height='10' />"; BREAK; CASE "blue": echo "<img src='../images/bullets/bullet_blue.png' title='".$desc2."' width='10' height='10' />"; BREAK; CASE "orange": echo "<img src='../images/bullets/bullet_orange.png' title='".$desc5."' width='10' height='10' />"; BREAK; CASE "pink": echo "<img src='../images/bullets/bullet_pink.png' title='".$desc6."' width='10' height='10' />"; BREAK; CASE "purple": echo "<img src='../images/bullets/bullet_purple.png' title='".$desc7."' width='10' height='10' />"; BREAK; CASE "bronz": echo "<img src='../images/bullets/bullet_bronze.png' title='".$desc3."' width='10' height='10' />"; BREAK; CASE "white": echo "<img src='../images/bullets/bullet_white.png' title='".$desc9."' width='10' height='10' />"; BREAK; CASE " ": echo "<img src='../images/bullets/bullet_white.png' title='".$desc9."' width='10' height='10' />"; DEFAULT; } } ?> added the php to the code short tags are bad programming practice. <td valign="middle"><?php checkMarker($row_sessiondetails['marker1']); ?></td> <td valign="middle"><?php checkMarker($row_sessiondetails['marker2']); ?></td> <td valign="middle"><?php checkMarker($row_sessiondetails['marker3']); ?></td> Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-790260 Share on other sites More sharing options...
biggieuk Posted March 30, 2009 Author Share Posted March 30, 2009 Thanks for your reply. I have tried everything but still can't get it to work. ??? Should function be in between my HEAD or BODY tags? I'm currently using this in the HEAD: for ( $counter = 1; $counter <= 10; $counter += 1) { mysql_select_db($database_hwbltaconf, $hwbltaconf); $query_colour = "SELECT * FROM colours WHERE colourid = ".$counter; $colour = mysql_query($query_colour, $hwbltaconf) or die(mysql_error()); $row_colour = mysql_fetch_assoc($colour); $var1 = 'desc'; $var2 = $counter; ${$var1.$var2} = $row_colour['colourdesc']; } function checkMarker($rsval){ SWITCH($rsval){ CASE "yellow": echo "<img src='../images/bullets/bullet_yellow.png' title='".$desc10."' width='10' height='10' />"; BREAK; CASE "red": echo "<img src='../images/bullets/bullet_red.png' title='".$desc8."' width='10' height='10' />"; BREAK; CASE "black": echo "<img src='../images/bullets/bullet_black.png' title='".$desc1."' width='10' height='10' />"; BREAK; CASE "blue": echo "<img src='../images/bullets/bullet_blue.png' title='".$desc2."' width='10' height='10' />"; BREAK; CASE "orange": echo "<img src='../images/bullets/bullet_orange.png' title='".$desc5."' width='10' height='10' />"; BREAK; CASE "pink": echo "<img src='../images/bullets/bullet_pink.png' title='".$desc6."' width='10' height='10' />"; BREAK; CASE "purple": echo "<img src='../images/bullets/bullet_purple.png' title='".$desc7."' width='10' height='10' />"; BREAK; CASE "bronze": echo "<img src='../images/bullets/bullet_bronze.png' title='".$desc3."' width='10' height='10' />"; BREAK; CASE "white": echo "<img src='../images/bullets/bullet_white.png' title='".$desc9."' width='10' height='10' />"; BREAK; CASE "green": echo "<img src='../images/bullets/bullet_green.png' title='".$desc4."' width='10' height='10' />"; BREAK; CASE " ": echo "<img src='../images/bullets/bullet_white.png' title='".$desc9."' width='10' height='10' />"; DEFAULT; } } The $desc... variables are all echo'ing out okay separately from the function. Here is the code in the body which should lookup the colour from the database and show the tooltip in the title: <td valign="middle"><?php checkMarker($row_sessiondetails['marker1']); ?></td> <td valign="middle"><?php checkMarker($row_sessiondetails['marker2']); ?></td> <td valign="middle"><?php checkMarker($row_sessiondetails['marker3']); ?></td> Thanks for help with this as im struggling Quote Link to comment https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/#findComment-797173 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.