4

Am using CakePHP running on XAMPP Server with PHP 5.3.5 i keep getting the error message syntax error, unexpected T_CONSTANT_ENCAPSED_STRING Line 38

Line 38 is 'Published',

The Code

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

Thats all, Note am running the app with PHP 5.3.5 using CakePHP MVC Framework

3
  • And what about the line or two before?
    – Mark Baker
    Sep 20, 2013 at 22:21
  • The thing with syntax errors is that they usually occur a line or two BEFORE the line given in the error message. That said, I don't know what that comma is there for, but I can't think of a reason for it to follow a logical operation.
    – Sammitch
    Sep 20, 2013 at 22:24
  • @MarkBaker seems like you had accidentally removed the syntax error when indenting the code. :P And @ OP, doesn't your IDE/code editor's syntax highlighting look a bit weird with those out of place quotes? Sep 20, 2013 at 22:30

4 Answers 4

6

Change it from

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

to

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

You just needed to remove the single quote right before $post

5
  • Thanks it worked but now i get the following error message.Error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING File: C:\xampp\htdocs\cakephp\app\View\Posts\index.ctp Line: 38 Sep 20, 2013 at 22:47
  • I updated my answer again. I've never used CakePHP. So let me know if that's right. Sep 20, 2013 at 23:01
  • Oops! It didn't work, it completely commented out all the other codes below the one in question. Sep 20, 2013 at 23:07
  • It shouldn't have. There are an even number of quotes in there. Sep 20, 2013 at 23:13
  • The entire question is now updated..Thanks for any help you are able to give. Sep 21, 2013 at 0:02
1

Try and make it a bit more readable:

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

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

This works for me... You had an old style php tag and had single quotes in the wrong spots.

I'm not sure what "ife" is, most likely an if statement gone wrong ?

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.