4

我正在使用在 XAMPP 服务器上运行的 CakePHP 和 PHP 5.3.5 我不断收到错误消息语法错误,意外的 T_CONSTANT_ENCAPSED_STRING 第 38 行

第 38 行是“已发布”,

编码

<div id="center_content">
<h2>Post Listings</h2>
<p>Here is a list of existing posts</p>
<div>
</div>
<?php
if (isset($posts) && is_array($posts))
{
?>
<table>
<tr>
<td>
<b>ID</b>
</td>
<td>
<b>title</b>
</td>
<td>
<b>content</b>
</td>
<td>
<b>Last Modified</b>
</td>
<td>
<b>published<b>
</td>
<td colspan="2"><b>&nbsp;&nbsp;Action</b></td>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id'];?></td>
<td><?php echo $post['Post']['title'];?></td>
<td><?php echo $post['Post']['content'];?></td>
<td><?php echo $post['Post']['modified'];?></td>
<td>
<?php echo $html->link(ife(
$post['Post']['published'] == 1', 
'Published',
'Unpublished),
'/posts/'.ife($post['Post']['published'] == 1',
'disabled','enable').'/'.$post['Post']['id']
 );
?>
</td>
<td>
<?php echo $html->link(
'Edit',
'/posts/edit'.$post['Post']['id']);?>
</td>
<td>
<?php echo $html->link(
'Delete',
'/posts/delete/'.$post['Post']['id']);?>
</td>
</tr>

<? endforeach; ?>
<?php
if (sizeof($posts) == 0) {
?>
<tr style= "background-color:#cccccc;">
<td colspan="6">
<span style="font-size: 17px;">
No post found.
</span>
</td>
</tr>
<?php
}
?>
</table>
<br/>
<?php
}
?>
</div>

就是这样,注意我正在使用 CakePHP MVC 框架使用 PHP 5.3.5 运行应用程序

4

4 回答 4

6

改变它从

 <?php echo $html->link(ife(
'$post['Post']['published'] == 1', 
'Published',
'Unpublished'),
'/posts/'.ife('$post'['Post']['published'] == 1',
'disabled','enable').'/'.$post['Post']['id']
);
?>

<?php echo $html->link(ife(
$post['Post']['published'] == 1', 
'Published',
'Unpublished),
'/posts/'.ife($post['Post']['published'] == 1',
'disabled','enable).'/'.$post['Post']['id']
);
?>

您只需要在 $post 之前删除单引号

于 2013-09-20T22:23:02.747 回答
1

尝试使它更具可读性:

$isPublished = ($post['Post']['published'] == 1) ? true : false;

echo $html->link(
  ife($isPublished, 'Published','Unpublished'),
  '/posts/' . ife($isPublished, 'disabled', 'enable') . '/' . $post['Post']['id']
);
于 2013-09-20T22:48:37.867 回答
1
 $post['Post']['published'] == '1', 
于 2013-09-20T23:45:53.190 回答
0

这对我有用......你有一个老式的 php 标签,并且在错误的地方有单引号。

我不确定“ife”是什么,很可能是 if 语句出错了?

<div id="center_content">
<h2>Post Listings</h2>
<p>Here is a list of existing posts</p>
<div>
</div>
<?php
if (isset($posts) && is_array($posts))
{
?>
<table>
<tr>
<td>
<b>ID</b>
</td>
<td>
<b>title</b>
</td>
<td>
<b>content</b>
</td>
<td>
<b>Last Modified</b>
</td>
<td>
<b>published<b>
</td>
<td colspan="2"><b>&nbsp;&nbsp;Action</b></td>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id'];?></td>
<td><?php echo $post['Post']['title'];?></td>
<td><?php echo $post['Post']['content'];?></td>
<td><?php echo $post['Post']['modified'];?></td>
<td>
<?php echo $html->link(
    ($post['Post']['published'] == 1 ? 'Published' : 'Unpublished'),
    '/posts/' . 
    ($post['Post']['published'] == 1 ? 'disabled' : 'enabled') .
    '/' . $post['Post']['id'] 
    );
?>
</td>
<td>
<?php echo $html->link(
'Edit',
'/posts/edit'.$post['Post']['id']);?>
</td>
<td>
<?php echo $html->link(
'Delete',
'/posts/delete/'.$post['Post']['id']);?>
</td>
</tr>

<?php endforeach; ?>
<?php
if (sizeof($posts) == 0) {
?>
<tr style= "background-color:#cccccc;">
<td colspan="6">
<span style="font-size: 17px;">
No post found.
</span>
</td>
</tr>
<?php
}
?>
</table>
<br/>
<?php
}
?>
</div>
于 2015-03-05T06:04:23.740 回答