Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/150403-variable-within-a-img-tag/
Share on other sites

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

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

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?

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>

  • 2 weeks later...

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  :(

 

 

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.