tags:

views:

163

answers:

3

hi guys,

Updating:

The php is considering the code beyond the EOM; closing tag.

Here is how my script is structured:

<?php
switch($x){

case "a":
$var = <<< EOM;
...the html...
EOM;
break;

case "b":
...some code...
break;

}
?>

And the script is giving erro just after the EOM;

Old:

I'm using the following code.

$var = <<< EOM

... some html ...

<?php 
if (date("j",strtotime($row["dinicio"]))==$i){echo "selected='selected'";}
?>

EOM;>

but it fails with the following error

Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Apache\htdocs\ancp\adm\adm_functions.php on line 132

removing the $row['dinicio'] solves the problem. but i need this field

Any ideas?

Edit:

For more reference here a more 'complete' segment:

<select id="ddia" name="ddia" class="form-normal">
      <option value="-1">dia</option>
      <?php
       for ($i=1; $i<32; $i++){
        echo "<option value='";
        echo ($i<10)?"0":"";
        echo "$i' ";
        if (date("j",strtotime($row["dinicio"]))==$i){echo "selected='selected'";}
        echo ">$i</option>\n";
       }
      ?>
     </select>
+1  A: 

try

if (date("j",strtotime({$row["dinicio"]}))==$i){echo "selected='selected'";}

and using more spaces :D

mozillalives
and I hope you meant to write <<<EOM not <<< $EOM ...
mozillalives
Yes it does the trick.
Paulo Bueno
Beware that `selected='selected'` is not valid (X)HTML. Escape the quotes using a backslash instead: `selected=\"selected\"`
Paul Lammertsma
A: 

Here's a cleaned up version of your code:

<select id="ddia" name="ddia" class="form-normal">
        <option value="-1">dia</option>
        <?php $j = date('j', strtotime($row["dinicio"]));
        for($i = 1; $i <= 31; $i++): ?>
        <option value="<?php echo str_pad($i, 0, 2, STR_PAD_LEFT); ?>"<?php if($j == $i) echo ' selected="selected"'; ?>>
            <?php echo $i; ?>
        </option>
        <?php endfor; ?>
</select>
Tatu Ulmanen
thx Tatu,But now i got anther problem. The php is going beyond the EOM; ending tag.
Paulo Bueno
+1  A: 

You can't escape into PHP in the middle of a HEREDOC (the <<< EOM ... EOM; block) - it is supposed to be just a string.

if you are simply echoing this straight out to the screen, then do something like:

...
switch($x){

  case "a":
  ?>
  <select id="ddia" name="ddia" class="form-normal">
    <option value="-1">dia</option>
    <?php $j = date('j', strtotime($row["dinicio"])); ?>
    <?php for($i = 1; $i <= 31; $i++): ?>
      <option value="<?php echo str_pad($i, 0, 2, STR_PAD_LEFT); ?>"<?php if($j == $i) echo ' selected="selected"'; ?>>
        <?php echo $i; ?>
      </option>
    <?php endfor; ?>
  </select>
  <?php
  break;
...
HorusKol
Well, you evidently *can* parse PHP in a `heredoc`. But just because you can doesn't mean you *should*.
Paul Lammertsma
fair enough - to be honest, i didn't actually try and run it - it just felt wrong.
HorusKol