简体   繁体   中英

How can I put PHP and HTML into a PHP array?

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>"

分号丢失;)

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!

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.

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM