tags:

views:

77

answers:

5

Guys,

This is a quick but simple question. I am having a bad day and don't know how to do this:

What I need to do is this...

I am checking for PMD in the url if so echo this:

<?php 
        if ( isset($_GET['pmd']) ) { 
          echo"<div class=\"response\"><p style=\"width:350px; height:200px; vertical-align:middle;\"><strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! Your name will now appear on our virtual wall.<br><br>You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p></div>";
        }else { etc...

What I need to include within that is an image like such that is firing off a tracking pixel.

<img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" />

What I would like to have is:

<?php 
        if ( isset($_GET['pmd']) ) { 
          echo"<div class=\"response\"><p style=\"width:350px; height:200px; vertical-align:middle;\"><strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! Your name will now appear on our virtual wall.<br><br>You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p>
          <br />
          <img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" />
          </div>";
        }else {

What do I need to do to get that mt_rand() function to fire?

Thanks,

Matt

+1  A: 

Something like this:

 <?php 
            if ( isset($_GET['pmd']) ) { 
              echo "<div class=\"response\"><p style=\"width:350px; height:200px; vertical-align:middle;\"><strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! Your name will now appear on our virtual wall.<br><br>You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p>
              <br />
              <img src=\"url.com?cid=12345&aid=1234&oid=".mt_rand()."&quantity=1\" height=\"1\" width=\"1\" />
              </div>";
            }else {
Pavel Morshenyuk
add a space after echo.
Emyr
+1  A: 

Your problem is that you are opening a PHP tag (<?php) inside another PHP tag (you need to close with ?> first.

One way to do it is to do something like this (I simplified your example for readability):

<?php

echo '<img src="foo.jpg?oid=', mt_rand(), '">';

?>

Some points to consider:

  • Usually you use urlencode when you add parameters to an url but mt_rand returns integers so we don't need to in this case.
  • In a echo it's generally better to use it with , instead of concatening consts with . then echo the resulting string. This way, less memory is used and less operations are done (but that's out of the scope of this question).
AlexV
+8  A: 

You could use this syntax instead:

<?php if ( isset($_GET['pmd']) ): ?>
    <div class="response"><p style="width:350px; height:200px; vertical-align:middle;"><strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! Your name will now appear on our virtual wall.<br><br>You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p>
      <br />
      <img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" />
    </div>
<?php else: ?>
    etc
<?php endif; ?>
Dan Breen
Thank you for your reply
Matthew
+1  A: 

the same way you did it - by opening and closing PHP tags

<?php if ( isset($_GET['pmd']) ) { ?>
<div class=\"response\"><p style=\"width:350px; height:200px; vertical-align:middle;\">
<strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem!
Your name will now appear on our virtual wall.<br><br>
You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p>
<br />
<img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" />
          </div>
<?php        }else { ?>

don't forget to get rid of all these ugly slashes

Col. Shrapnel
+1  A: 

You shouldnt add large blocks of static HTML with echo. Try:

<?php 
        if ( isset($_GET['pmd']) ) {
?>
          <div class="response"><p style="width:350px; height:200px; vertical-align:middle;">
          <strong>Thank you for acting as a &quot;watchman on the walls&quot; of Jerusalem! Your name will now appear on our virtual wall.<br><br>You can also catch up on the latest news from Israel by visiting our news feed below.</strong></p>
          <br />
          <img src="url.com?cid=12345&aid=1234&oid=<?php echo mt_rand(); ?>&quantity=1" height="1" width="1" />
          </div>
<?php
        }else {//...
        }
?>
digitalFresh