0

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>"
4
  • 9
    Your approach is fundamentally flawed, as any PHP code inside the string will not be executed (or could be only using eval() which is bad practice). You should change your approach so that you don't need to do this. Commented Jun 24, 2010 at 11:52
  • Now that I had a closer look at it, I agree with Pekka. It's fundamentally flawed. Commented Jun 24, 2010 at 12:07
  • Eh, I don't know. This is more of a subquestion of stackoverflow.com/questions/3108949/… Request to close? Commented Jun 24, 2010 at 12:13
  • it would help if you would clearify if you want PHP inside the string to be evaluated before assigning it. But if you want the question closed we can flag it to a moderator - if you want. Commented Jun 24, 2010 at 12:20

4 Answers 4

3

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!

Sign up to request clarification or add additional context in comments.

4 Comments

Keeping the inline styles is far from being optimal either, as it adds code bloat and hampers accessibility. Initializing $sched with &nbsp; when you are only adding it when you overwrite it anyway is odd too. And in fact, the entire loop is not needed.
@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 :)
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 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 :)
3

the semicolon is missing ;)

1 Comment

+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
0

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>";
} ......................

?>

2 Comments

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.