Shadowing Posted January 17, 2012 Share Posted January 17, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/255234-using-css-inside-a-echo-with-a-variable/ Share on other sites More sharing options...
floridaflatlander Posted January 17, 2012 Share Posted January 17, 2012 I always use double quotes in my inline styles, when you play with it make sure you remove your single quotes and don't have a space between $b_x & px style=\"position:absolute; left: $b_xpx; top: $b_ypx;\"></a>"; Quote Link to comment https://forums.phpfreaks.com/topic/255234-using-css-inside-a-echo-with-a-variable/#findComment-1308602 Share on other sites More sharing options...
Psycho Posted January 17, 2012 Share Posted January 17, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/255234-using-css-inside-a-echo-with-a-variable/#findComment-1308603 Share on other sites More sharing options...
scootstah Posted January 17, 2012 Share Posted January 17, 2012 I much prefer this syntax in templates: <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> Quote Link to comment https://forums.phpfreaks.com/topic/255234-using-css-inside-a-echo-with-a-variable/#findComment-1308606 Share on other sites More sharing options...
Shadowing Posted January 17, 2012 Author Share Posted January 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255234-using-css-inside-a-echo-with-a-variable/#findComment-1308620 Share on other sites More sharing options...
floridaflatlander Posted January 17, 2012 Share Posted January 17, 2012 On floridaflatlander example I had the same results. Using those \ is to allow for unusable characters to be used right? The slashs are to escape the double quote to the statement will echo Quote Link to comment https://forums.phpfreaks.com/topic/255234-using-css-inside-a-echo-with-a-variable/#findComment-1308623 Share on other sites More sharing options...
litebearer Posted January 17, 2012 Share Posted January 17, 2012 Scoots' should work with the double quotes, unless there is something inside $address to cause a problem. Quote Link to comment https://forums.phpfreaks.com/topic/255234-using-css-inside-a-echo-with-a-variable/#findComment-1308626 Share on other sites More sharing options...
Shadowing Posted January 17, 2012 Author Share Posted January 17, 2012 $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> Quote Link to comment https://forums.phpfreaks.com/topic/255234-using-css-inside-a-echo-with-a-variable/#findComment-1308631 Share on other sites More sharing options...
Psycho Posted January 17, 2012 Share Posted January 17, 2012 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' " Quote Link to comment https://forums.phpfreaks.com/topic/255234-using-css-inside-a-echo-with-a-variable/#findComment-1308634 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.