So I have a site where there are multiple categories, I use foreach to pull the listings from the database. I am trying to display a Nothing Returned in the view if the database doesnt have anything to return.
The View
<?php foreach ($posts as $post): { ?>
<?php if(empty($post['title'])){
echo "Nothing found";
}else{?>
<html>
<body>
<?php echo $post['title'];?>
<?php echo $post['style'];
</body>
</html>
<?php } ?>
<?php }endforeach; ?>
So it seems to work opposite if I put if(!empty())
I am still a little fresh with CodeIgniter so sorry if this is a basic question.
Do something like so:
if (empty($posts)) {
echo "Nothing returned.";
} else {
foreach($posts as $post) {
// Display each post
}
}
First try to check if we have anything in $posts then try to itarate it with foreach.
empty() returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
So, if you want to execute statement echo "Nothing found"; then you must have to use !empty() or alternatively you can use isset() function too. isset() will return True if value is setted to variable.
see details
Try to use isset() function with not(!) condition
If you are using colon : then don't use curly brackets {} with foreach loop
if(isset($posts) && count($posts) > 0){
foreach ($posts as $post){
if(!isset($post['title'])){
echo "Nothing found";
}else{?>
<html>
<body>
<?= $post['title'] ?>
<?= $post['style'] ?>
</body>
</html>
<?php } } }else{ echo 'No record found';}?>
Just ran your code - you're missing a closing php tag at the end of your 'style' line:
<?php echo $post['style']; ?>
Alternatively, you could compress things a little as follows:
echo "<html><body>";
if(empty($posts)){
echo "Nothing found";
}else{
foreach ($posts as $post){
echo $post['title'];
echo $post['style'];
}
}
echo "</body></html>";
<?php if(empty($posts) {
echo "Nothing returned.";
} else{
foreach ($posts as $post) { ?>
<?php if(empty($post['title'])){
echo "Nothing found";
}else{?>
<html>
<body>
<?php echo $post['title'];?>
<?php echo $post['style'];
</body>
</html>
<?php } ?>
<?php } } endforeach; ?>
Related
Here am checking an array values exist or not with another table and if exist it should produce the output.
Here is my array value which looks like this
the code i written is here
<?php if(array_search($v_employee->user_id,array_column($salary_info, 'user_id'))) {?>
<?php echo $v_employee->fullname; ?>
<?php } else { ?>
<?php echo $v_employee->fullname; ?>
<?php } ?>
it doesn't produce any result.please help me to solve.thanks in advance
array_column can't read object
try this one
<?php
$salary_info_arr=json_decode(json_encode(array_filter($salary_info)),true); //changed
if(array_search($v_employee->user_id,array_column($salary_info_arr, 'user_id'))) {?>
<?php echo $v_employee->fullname; ?>
<?php } else { ?>
<?php echo $v_employee->fullname; ?>
<?php } ?>
<?php } ?>
This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>
I am using the following structure for my foreach how would I use an if statement within the same format?
<?php foreach($property as $section):?>
if(!empty($section))
{
echo <p>data here </p>;
}else{
html
}
<?php endforeach;?>
<?php foreach($property as $section):
if(!empty($section)):
echo "<p>data here </p>";
endif;
endforeach;?>
OR
<?php foreach($property as $section):
if(!empty($section)):
echo "<p>data here </p>";
else:
echo "html";
endif;
endforeach;?>
See: Alternative Syntax
Not sure what you mean, but you don't have valid php.
foreach($property as $section) {
if(!empty($section))
{
echo '<p>data here </p>';
}else{
echo 'html';
}
}
I think you are looking for this.
<?php foreach($property as $section): ?>
<?php
if(!empty($section)) :
echo <p>data here </p>;
else :
html
endif;
endforeach; ?>
I think best solution for this is:
<?php
foreach($property as $section) {
if(!empty($section))
{
echo '<p>data here </p>';
}
else
{
?>
<div id="dosome">really crazy html stuff here</div>
<?php
}
}
?>
This is an example containing }elseif{ also:
<?php foreach($property as $section):?>
<?php if(!empty($section)): ?>
<p>data here </p>
<?php elseif ([another condition]): ?>
...
<?php else: ?>
html
<?php endif: ?>
<?php endforeach;?>
The entire documentation is here: http://php.net/manual/en/control-structures.alternative-syntax.php
SHORTHAND :
<?php foreach($property as $section):?>
<?php if(!empty($section)):?>
<p>data here </p>;
<?php else: ?>
html
<?php endif;?>
<?php endforeach;?>
OTHER:
<?php foreach($property as $section)
{
if(!empty($section))
{
echo '<p>data here </p>';
}else{
echo 'html';
}
}
?>
I'm developing a website using Codeigniter. The structure site is simple, I have dynamic menu and content retrieving DB in Model. Then assign a method to call both of them in Controller.
I have that weird error message keeps coming back saying the array is not defined.
I can get tabPhotos, it contains datas.
View:
<div id="container">
<div id="photos">
<?php echo "Count: ". count($tabPhotos); ?>
<ul>
<?php if (!is_array($tabPhotos) || empty($tabPhotos)) :?>
<?php return null; ?>
<?php foreach ($tabPhotos as $item) : ?>
<?php echo "Model: " . $item->url; ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div>
Controller:
public function loadMenu($file) {
$data['tabMenuItems'] = $this->qdphoto_model->getAllMenuItems();
$this->load->view($file, $data);
}
public function loadCategoryPage($file, $category='Book 1') {
$data['tabPhotos'] = $this->qdphoto_model->getAllPicturesByCategory($category, 'url, model');
$this->load->view($file, $data);
}
Am I reading this correctly? if $tabPhotos IS NOT an array OR it is and it happens to be EMPTY then return null and continue to run the foreach loop with a mysterious endif following?
<?php if (!is_array($tabPhotos) || empty($tabPhotos)) :?>
<?php return null; ?>
<?php foreach ($tabPhotos as $item) : ?>
<?php echo "Model: " . $item->url; ?>
<?php endforeach; ?>
<?php endif; ?>
Shouldn't it read this:
<?php if (is_array($tabPhotos) && !empty($tabPhotos)) :?>
<?php foreach ($tabPhotos as $item) : ?>
<?php echo "Model: " . $item->url; ?>
<?php endforeach; ?>
<?php endif; ?>
no need for that return null statement.
Also, I believe you can return views as strings. If you want to load more than one view you can certainly load any fragment of your view as a string...
$string = $this->load->view($file, $data, true);
Please note that you should review this:
http://codeigniter.com/user_guide/general/views.html
See bottom. It says how to return as string, except that I am used to slightly different syntax thanks to custom controllers and such. Perhaps this example I provided is not accurate.
<marquee behavior="alternate" scrolldelay="1" scrollamount="2">
<?php do { ?>
<?php echo $row_Recordset1['Name']; ?>:
<?php echo $row_Recordset1['Text']; ?>
•
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</marquee>
<?php mysql_free_result($Recordset1); ?>
Print a friendly message to the user instead of NULL:
<?php echo (NULL === $row_Recordset1['Text']) ? "No value" : $row_Recordset1['Text']; ?>
As xil3 illustrates, you can also use this pattern (from the docs):
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
The way you have it written right now, $row_Recordset1 will be null the first time it goes into the loop.
I've rewritten it for you:
<marquee behavior="alternate" scrolldelay="1" scrollamount="2">
<?php while($row_Recordset1 = mysql_fetch_assoc($Recordset1)) { ?>
<?php echo (($row_Recordset1['Name'] != null) ? $row_Recordset1['Name'] : 'n/a'); ?>:
<?php echo (($row_Recordset1['Text'] != null) ? $row_Recordset1['Text'] : 'n/a'); ?>
•
<?php } ?>
</marquee>
<?php mysql_free_result($Recordset1); ?>