0

I'm using the server to write the html of a lot of my page to process some pagination. I'm getting a syntax error on the line just below the comment insert as shown below. I'm guessing it is because I'm trying to place echo within a echo? I've tried a lot of things here and am having no luck figuring out what I'm doing wrong. Any help appreciated greatly!

<?php

PDO STUFF HERE. . . 
if() {
foreach() {
echo ' <input name="flyer" type="file" id="'.$row['ad_link'].'" tabindex="9" /></td>
//following line showing syntax error in my text editor
  <input name="transaction" type="radio" tabindex="11" value="0"' . if($transaction == '0') echo '$chkvalue'; . '/><label for="listings">Listings</label>
//following line showing syntax error in my text editor
  <input style="margin-left:20px;" name="transaction" type="radio" tabindex="12" value="1"' . if($transaction == '1') echo $chkvalue; . '/> ' 
          }
     }

?>
rhill45
  • 559
  • 10
  • 35

3 Answers3

1

if and echo are language constructs and as such cannot be used inside a string. You'll have to change the flow of the logic to fix this.

Easiest fix is to just use a bunch of semicolons.

This is not allowed:

echo 'abc' . if( $a == 1 ) { echo '3'; };

But this is fine, because it breaks everything up into steps:

echo 'abc';
if( $a == 1 ) { echo '3'; }
random_user_name
  • 25,694
  • 7
  • 76
  • 115
Erik
  • 3,598
  • 14
  • 29
1

Try something like this:

echo '<input name="flyer" type="file" id="'.$row['ad_link'].'" tabindex="9" /></td>
<input name="transaction" type="radio" tabindex="11" value="0"'.($transaction == '0') ? $chkvalue:"".'/><label for="listings">Listings</label>
<input style="margin-left:20px;" name="transaction" type="radio" tabindex="12" value="1"' .($transaction == '1') ? $chkvalue:"".'/>';

Using the ? operator, you can check a condition and echo something out if it equates, or nothing if it doesn't.

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
0

You cannot concatinate an entire if statement. It cannot be converted to a string.

You can change it to an expression with a ternary operator though:

echo 'first part of your string ' . ($transaction == '0'?'$chkvalue':'') . ' rest of your string';

Even better (I think), you can put the HTML outside of PHP tags. You can briefly open PHP tags again to execute and if statement to conditionally echo a value, or you can use the short <?= XYZ ?> tag which is short for <?php echo XYZ ?>.

This way of working is very convenient when working with larger chunks of HTML, because most syntax highlighters will actually highlight the HTML, and you won't need to escape anything. If you 'prepare' your variables before and insert them using the short <?= tags, your HTML will look like a clean template-like piece of code with only those variables in it.

<?php
foreach( ... ) 
{?>
 <input name="flyer" type="file" id="<?=$row['ad_link']?>" tabindex="9" /></td>
 <input name="transaction" type="radio" tabindex="11" 
     value="0" <? if($transaction == '0') echo $chkvalue; /* Actual statement */ ?>/>
 <label for="listings">Listings</label>
 <input style="margin-left:20px;" name="transaction" type="radio" tabindex="12" 
     value="1" <?= ($transaction == '1'? $chkvalue : '') /* Just output an expression */ ?>/>
<?}
Community
  • 1
  • 1
GolezTrol
  • 114,394
  • 18
  • 182
  • 210