我正在处理搜索数据,并希望从控制器发送HTML以使用AJAX进行查看。我收到语法错误。这是我的代码。
function search()
{
$params['searchKeys'] = $this->input->post('query');
$params['userID'] = $this->session->userdata('id');
$storeArray = $this->Store_model->searchStore($params);
foreach($storeArray as $store){
echo "<div class='col-lg-3 col-md-4 col-sm-6 col-xs-12'>
<div class='store-block'>
<img class='img-responsive' src=".getImageURL($s['image'], array( 'alt' => '' )).">
<div class='overlay'>
<h2>".$s['storeName']."</h2>".if($this->session->userdata('userType') == '2'):.
.if($s['isBlocked'] == '1'):.
"<a class='info'>Your store is blocked!</a>
".else:."
<a class='info' href=".site_url('Store/preview/'.encode($s['storeID'])).">Preview</a>
<a class='info' href=".site_url('Slot/index/'.encode($s['storeID'])).">Slots</a>
<a class='info' href=".site_url('Booking/index/'.encode($s['storeID'])).">Bookings</a>
<a class='info' href=".site_url('Store/edit/'.encode($s['storeID'])).">Edit</a>
<a class='info' href='#'data-toggle='modal' data-target='#confirm-".$s['storeID'].">Delete</a>
".endif;."
".elseif($this->session->userdata('userType') == '1'):."
<a class='info' href=".site_url('Store/preview/'.encode($s['storeID'])).">Preview</a>
".if($s['isBlocked'] == '0'):.
"<a class='info' href='#' data-toggle='modal' data-target='#confirm-block-".$s['storeID'].">Block</a>
".else:."
<a class='info' href='#' data-toggle='modal' data-target='#confirm-unblock-".$s['storeID'].">Un block</a>
".endif.
.endif."
</div>
</div>
</div>
</div>
";
}
exit;
}
我在这条线上出现错误。
<h2>".$s['storeName']."</h2>".if($this->session->userdata('userType') == '2'):.
答案 0 :(得分:1)
该错误是因为未定义$s
。您要么必须更改
foreach($storeArray as $store){
到
foreach($storeArray as $s){
或将$s['example_key']
的每个实例更改为$store['example_key']
但是您仍然会遇到@Bartek评论中描述的问题。
通过使用$this->load->view
可以简化整个过程,该操作将为您“回显”视图文件。编写可插入和退出PHP处理器的HTML更易于编写和阅读。此外,它还具有更多用途。
这里是加载视图的控制器。
public function search()
{
$params['searchKeys'] = $this->input->post('query');
$params['userID'] = $this->session->userdata('id');
$viewdata['storeArray'] = $this->Store_model->searchStore($params);
$this->load->view('store_search_view', $viewdata);
}
“视图”:/application/views/store_search_view.php
<?php
foreach ($storeArray as $s) :
$storeID = $s['storeID'];
$enc_storeID = encode($storeID);
?>
<div class='col-lg-3 col-md-4 col-sm-6 col-xs-12'>
<div class='store-block'>
<img class='img-responsive' src=".getImageURL($s['image'], array( 'alt' => '' )).">
<div class='overlay'>
<h2><?php echo $s['storeName']; ?></h2>"
<?php
if($this->session->userdata('userType') == '2'):
if($s['isBlocked'] == '1'):
?>
<a class='info'>Your store is blocked!</a>
<?php
else:
?>
<a class='info' href='<?php echo site_url('Store/preview/'.$enc_storeID); ?>'>Preview</a>
<a class='info' href='<?php echo site_url('Slot/index/'.$enc_storeID); ?>'>Slots</a>
<a class='info' href='<?php echo site_url('Booking/index/'.$enc_storeID); ?>'>Bookings</a>
<a class='info' href='<?php echo site_url('Store/edit/'.$enc_storeID); ?>'>Edit</a>
<a class='info' href='#' data-toggle='modal' data-target='#confirm-<?= $storeID; ?>'>Delete</a>
<?php
endif;
elseif($this->session->userdata('userType') == '1'):
?>
<a class='info' href='<?= site_url('Store/preview/'.$enc_storeID); ?>'>Preview</a>
<?php if($s['isBlocked'] == '0'): ?>
<a class='info' href='#' data-toggle='modal' data-target='#confirm-block-<?= $storeID; ?>'>Block</a>
<?php else: ?>
<a class='info' href='#' data-toggle='modal' data-target='#confirm-unblock-<?= $storeID; ?>'>Un block</a>
<?php
endif;
endif;
?>
</div>
</div>
</div>
<?php
endforeach;
如果您想知道,插入和退出PHP“模式”比连接一个巨大的字符串更有效。在处理PHP和直接输出HTML之间切换时,性能损失为零。
如果您不熟悉语法,则<?= ...
与<?php echo ...
相同。如果看起来我是在两种语法样式之间随机切换,那是真的。除了一种类型涉及较少的键入外,没有理由将一种类型置于另一种之上。
答案 1 :(得分:0)
<h2>".$s['storeName']."</h2>".if($this->session->userdata('userType') == '2'):.
错误来自if()部分。您不能在echo中使用if()。 相反,您可以将if条件的结果保存到变量中并在echo中显示,也可以将echo放入if()else块中。