Jump to content

Using CSS inside a echo with a variable


Shadowing

Recommended Posts

Im having a problem with getting the quotes correct with this. I can either get the variable to work in the href and img src or in the css. Anyone have any ideas on this. Cuase im really stuck.

 

This allows the $address and $cos to echo in but not the css variables

 

<td><?php echo "<a href='planet_profile.php?planet=$address'>
<img src='images/star.jpg' id='$cos' 
style='position:absolute;' left:'$b_x px;' top:'$b_y px;'></a>"; ?></td>

 

and this allows only the css variables to work

 

 

 
<td><?php echo '<a href="planet_profile.php?planet="' . $address . '">
<img src="images/star.jpg" id="' . $cos . '" 
style="position:absolute; left:' . $b_x . 'px; top:' . $b_y . 'px;"></a>'; ?></td>

Link to comment
Share on other sites

Yeah, it can be confusing when echoing content that includes tags with quotes (or even quoted content within those quoted content). The first thing to do is to generate the content without PHP so that it works. Then, determine how you will generate the content with PHP.

 

I assume this is what you want your link to look like

<td><a href='planet_profile.php?planet=ADDRESS_VALUE'>
<img src='images/star.jpg' id='ID_VALUE' style='position:absolute; left:X_VALUE px; top:Y_VALUE;'></a></td>

 

The problem is that you were trying to put each value in the style parameter within single quote. All the values go within one set of quotes

<td><?php echo "<a href='planet_profile.php?planet={$address}'>
<img src='images/star.jpg' id='{$cos}' 
style='position:absolute; left:{$b_x} px; top:{$b_y} px;'></a>"; ?></td>

Link to comment
Share on other sites

Thats interesting Psycho. Ive only seen one other example before in using brackets like that. I couldnt get your example to work. It ignored the CSS but the link and img variables worked. Im guessing cause after style it needs something else there. Thats what was really confusing me is the css requires a quote to be switched.

 

On floridaflatlander example I had the same results. Using those \ is to allow for unusable characters to be used right?

 

Scootstah's example works but had to get rid of the quotes after planet= to make it work and whats wierd is my Jedit program is showing it shouldnt work but it works with no problems. :) this program is out of date though.

 

 

 

Link to comment
Share on other sites

$address is just a number

 

 

 <a href="planet_profile.php?planet="<?php echo $address; ?>"> 

 

i had to get rid of the quote right after planet= but keep the other two double quotes because their is no matching pair.

 

so i was thinking maybe i just need to add a set of quotes to match the one I deleted.

 

notice how <td> is blue and </td> is black

<td>
<a href="planet_profile.php?planet="<?php echo $address; ?>">  
<img src="images/star.jpg" id="<?php echo $cos; ?>" 
style="position:absolute; left:<?php echo $b_x; ?>px; top:<?php echo $b_y; ?>px;"></a>
</td>

 

if I get rid of that 1 double quote this turns black

<?php echo $b_x; ?>px; top:<?php echo $b_y; ?>px;

 

it shows up correctly though in this forum just doesnt in my jeditor "the one below is the one that works"

<td><a href="planet_profile.php?planet=<?php echo $address; ?>">  
<img src="images/star.jpg" id="<?php echo $cos; ?>" 
style="position:absolute; left:<?php echo $b_x; ?>px; top:<?php echo $b_y; ?>px;"></a></td>

Link to comment
Share on other sites

As you see there are many ways to skin a cat so to speak. The method you use is up to you but the method I would use would depend on "how" that line is used.

 

If that line of code was used in a loop to generate many results I would prefere to have a single echo statement with the variables included within double quotes. Or, even better, I would concatenate the results into one big variable - then output that variable in the HTML. However, if that line is only used once, then I would prefer scootstah's method. I think it is much cleaner to separate your logic (PHP Code) from the output (HTML code). I either put all the logic in a separate file or at the top of the script to generate variables for the dynamic output. Then, in the HTML code I would only have echo statements for that dynamic content.

 

A couple other notes:

 

1. The backslash, as floridaflatlander alluded to, is to 'escape' the quote mark. This tells the PHP engine that you want that quote mark to be treated as a literal quote mark character and not the closing quotemark for the string.

2. The brackets I used help the PHP parser identify the variables you want interpreted. For example, if the variable $b_x contained the value '5' and you used this line:

echo " style='top:$b_xpx' "

The result would fail and produce this

echo " style='top:' "

because the PHP parser was trying to interpret the variable $b_xpx - which did not exist. You can overcome this by enclosing variables within double quoted text with curly braces such as this

echo " style='top:{$b_x}px' "

This would generate the correct output

echo " style='top:5px' "

Link to comment
Share on other sites

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.