Jump to content

Help with img src inside PHP script.


jimmyjump75

Recommended Posts

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

Link to comment
Share on other sites

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; ?>

 

 

 

Link to comment
Share on other sites

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.

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.