jimmyjump75 Posted February 6, 2013 Share Posted February 6, 2013 Hi, I am trying to run an img src from inside a PHP script that renders a graph for me. This works well for me run from inside HTML like this.. <img src="graph.php?LOC=<?php print $LOC; ?>" /> Basically I am passing it the php variable $LOC because the graph php file graphs the data based on this variable. The issue I am having is that I only want the graph being rendered if a certain condition is met in the original php file. Therefore I am trying to embed this in the php file so I can use if - else to decide whether it gets rendered or not.. When I put it at the end of my html it's fine but then it runs no matter what.. I tried embedding like this in PHP.. echo "<img src=\""graph.php?LOC=$LOC"\"/>"; I am obviously a novice.. I understand it's already on PHP so not sure I am approaching this correctly. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/274076-help-with-img-src-inside-php-script/ Share on other sites More sharing options...
Jessica Posted February 6, 2013 Share Posted February 6, 2013 You have too many " in there. Quote Link to comment https://forums.phpfreaks.com/topic/274076-help-with-img-src-inside-php-script/#findComment-1410355 Share on other sites More sharing options...
gizmola Posted February 6, 2013 Share Posted February 6, 2013 You have a couple of options. One is to use the heredoc: $link = <<<HERE <img src="graph.php?LOC=$LOC"> HERE; echo $link; Heredocs are great for having large blocks of markup with php variables sprinkled in, but it's not a great fit for this particular problem, especially given your desire for an if-then block. Then there's the use of echo. As Jess pointed out already, you seem a bit confused in the process of escaping the double quotes, and I would not advocate their use when you are trying to output markup that includes double quotes. It's hard to read and update. Just use concatenation: echo '<img src="graph.php?LOC=' . $LOC . '">'; Mixing logic in with markup can make for some really hard to maintain code, and it's far better to seperate your markup into some sort of template/view file, seperate from your logic. The simplest way to do that is to put your markup in a php file and include/require it, at the point you would otherwise be returning it (in other words, after the variables required are setup. So the way I'd advise to handle this would be to have your markup in a php script, and use PHP's ability to drop in and out of HTML, as in your first example. Just mix in alternative syntax for the control structures you need: <h2>Graph may be coming<h2> <p>If there's a graph available you'll see it below</p> <?php if ($LOC): ?> <div class="graph"> <img src="graph.php?LOC=<?= $LOC ?>"> </div> <?php else: ?> <div class="oops"> Sorry the graph is not available for this criteria. </div> <?php endif; ?> Quote Link to comment https://forums.phpfreaks.com/topic/274076-help-with-img-src-inside-php-script/#findComment-1410369 Share on other sites More sharing options...
jimmyjump75 Posted February 6, 2013 Author Share Posted February 6, 2013 Hi, Thank you Web Mason and Thank you Gizmola! I am new to LAMP and i do read as much as I can but I could not find how to do this.. I was googling run img src from within PHP. Ultimately Gizmola got me going in the right direction. I used this. <?php if (CA == $LOC): ?> <div class="graph"> <img src="graph.php?SITE=<?php print $LOC ?>"> </div> <?php endif; ?> Just out of curiosity.. how do you go about testing and running your php scripts quickly? Doing it via webpage is slow. I have yesterday discovered using php script.php from the command line. Is this the preferred way? Thanks again. I don't know if these forums are points based but if they are let me know how and I will assign. Quote Link to comment https://forums.phpfreaks.com/topic/274076-help-with-img-src-inside-php-script/#findComment-1410483 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.