php - Concatenate HTML in controller
Get the solution ↓↓↓Solution:
The error is because$s
is not defined. You either have to change
foreach($storeArray as $store){
to
foreach($storeArray as $s){
Or change every instance of$s['example_key']
to$store['example_key']
But you are still going to run into the problem described in @Bartek's comment.
This whole thing could be made easier by using$this->load->view
which will "echo" the view file for you. Writing HTML that drops into and out of the PHP processor is easier to write and read. Plus it's a lot more versatile.
Here's the controller that loads a view.
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);
}
The "view": /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;
In case you're wondering, it is more efficient to drop into and out of PHP "mode" than to concatenate a gigantic string. There is zero performance penalty for switching between processing PHP and outputting HTML directly.
If you're not familiar with the syntax,<?= ...
is the same as<?php echo ...
. If it seems I was switching between the two syntax styles at random that is true. There's no reason for one style over the other except one involves less typing.
Answer
Solution:
<h2>".$s['storeName']."</h2>".if($this->session->userdata('userType') == '2'):.
The error comes from if() part. You can't use if() inside echo. Instead you can save result of your if condition to variable and display it in echo or you can put your echo inside the if() else block.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: integrity constraint violation: 1452 cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.