views:

120

answers:

4

Hi, how can I make this not return an error of:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

.

$day[$i++] = "<tr><?php if(isset($schedule['00:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['00:00'] ?></td><?php } if(isset($schedule['02:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['02:00'] ?></td><?php } if(isset($schedule['03:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['03:00'] ?></td><?php } if(isset($schedule['04:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['04:00'] ?></td><?php } if(isset($schedule['05:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['05:00'] ?></td><?php } if(isset($schedule['06:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['06:00'] ?></td><?php } if(isset($schedule['07:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['07:00'] ?></td><?php } if(isset($schedule['08:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['08:00'] ?></td><?php } if(isset($schedule['09:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['09:00'] ?></td><?php } if(isset($schedule['10:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['10:00'] ?></td><?php } if(isset($schedule['11:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['11:00'] ?></td><?php } if(isset($schedule['12:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['12:00'] ?></td><?php } if(isset($schedule['13:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['13:00'] ?></td><?php } if(isset($schedule['14:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['14:00'] ?></td><?php } if(isset($schedule['15:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['15:00'] ?></td><?php } if(isset($schedule['16:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['16:00'] ?></td><?php } if(isset($schedule['17:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['18:00'] ?></td><?php } if(isset($schedule['19:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['19:00'] ?></td><?php } if(isset($schedule['20:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['20:00'] ?></td><?php } if(isset($schedule['21:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['21:00'] ?></td><?php } if(isset($schedule['22:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['22:00'] ?></td><?php } if(isset($schedule['23:00'])) { ?><td style=\"width:32px\"><?php echo $schedule['23:00'] ?></td><?php } ?></tr>"
+3  A: 

the semicolon is missing ;)

Oliver
+1. I'd suggest you use single quotes to enclose strings if you don't want the variables or escape characters to be interpreted. Its also a little faster, so generally a good practice to prefer single quotes over double quotes
Gunjan
It still throws an error.
Sam
A: 

Edit: removed the NOWDOC example after having read the linked question explaining the UseCase.

You can easily solve your problem like this:

if(count($schedule) > 0) {
    $rows = '<tr></th>' 
          . implode('</td><td>', $schedule) 
          . '</td></tr>';
}

For your table headers, you can use a similar approach:

if(count($schedule) > 0) {
    $head = '<tr><th>' 
          . implode('</th><th>', array_keys($schedule)) 
          . '</td></tr>';
}

Since you are querying the database only for those columns with a value and do not want the full 24 hours to be displayed, there is no need to check if the db rows contain anything in PHP again. They do. Otherwise they wouldn't have been returned from the query. And since you only want to wrap the content in table cells and headers, all you have to do is implode them with the markup for that.

In additon, do not add inline styles as they make your code less accessible. Some users override styles with user stylesheets and inline styles can disrupt that. Just add a CSS stylesheet or <style> with td { width:32px } instead. Makes the page weigh less too.

Gordon
+3  A: 

You've done it wrong twice:

  1. You're trying to use PHP code as a data. While you should use code result instead
  2. Your PHP code is far from being optimal.

Here you go:

$str = '';
for ($h=0;$h<24;$h++) {
  $sched = "&nbsp;";
  $hour  = str_pad($h, 2, 0, STR_PAD_LEFT);
  if (isset($schedule["$hour:00"])) $sched = $schedule["$hour:00"]; 
  $str  .= "<td style=\"width:32px\">$sched</td>";
}
$day[$i++] = $str;

Feel the power of programming!

Col. Shrapnel
THANKS, 24 `if` s were hurting my brain
kemp
+1 for elegance !
FreekOne
Keeping the inline styles is far from being optimal either, as it adds code bloat and hampers accessibility. Initializing $sched with   when you are only adding it when you overwrite it anyway is odd too. And in fact, the entire loop is not needed.
Gordon
@Gordon nice points. I've thought already to adapt this code to template use. Though overwriting is just replacement for `else` statement. Or you can call it variable definition :) And your shoot for no loop is excellent :)
Col. Shrapnel
Agreed. I misread the code. I am not used to `if` one liners (I consider it a coding style violation). However, in this case you are creating 24 cells, when the OP did ask for only those with a value.
Gordon
@Gordon yeah, intentionally. I don't know the whole case, but resulting table would look messy otherwise. That's obviously a schedule, and it must have straight columns, I believe :)
Col. Shrapnel
A: 

Use like this,

<?php
$day[$i++] = "<tr>";
if(isset($schedule['00:00'])) { 
   $day[$i++] .= "<td style=\"width:32px\">".$schedule['00:00']."</td>";
}
if(isset($schedule['02:00'])) {
   $day[$i++] .= "<td style=\"width:32px\">".$schedule['02:00']."</td>";
} if(isset($schedule['03:00'])) { 
    $day[$i++] .= "<td style=\"width:32px\">".$schedule['03:00']."</td>";
} ......................

?>
Karthik
Seeng noone using loop in this topic makes me pain
Col. Shrapnel
He is asking about writing the code without error and not a flow wise suggestion.
Karthik