if else error message always shows else statement message - php

I think this is a simple problem. But, I don't know how to solve this problem. I have posted my codes below. Here, In this below code if else statement always shows No Sarees. I got the result from mysql table but it always shows with No Sarees text. I have checked this using var_dump($categories) it returns array(0){} . How can I solve this problem?
<?php if ($categories) { ?>
<!--BOF Refine Search Result-->
<div class="refine-search-result">
<?php if (count($categories) <= 5) { ?>
<?php foreach ($categories as $category) { ?>
<div class="refine-block">
<p><?php echo $category['name']; ?></p>
</div>
<?php } ?>
<?php } else { ?>
<?php for ($i = 0; $i < count($categories);) { ?>
<?php $j = $i + ceil(count($categories) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($categories[$i])) { ?>
<div class="refine-block">
<p><?php echo $categories[$i]['name']; ?></p>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
<div class="clear"></div>
</div><!--EOF Refine Search Result-->
<?php } else {
?>
<div class="refine-search-result">
<div class="refine-block">
<p>No Sarees</p>
</div>
<div class="clear"></div>
</div>
<?php } ?>

If your $categories is empty array then in if($categories) your array is converted to boolean value. If it's empty array (and you say it is) then it's get converted to false.
Check this:
$categories = array(); //empty array
var_dump((boolean)$categories); //this will show what variable will look like after converting to boolean value
if ($categories){
echo '$categories IS NOT empty';
}else{
echo '$categories IS empty';
}

Related

ACF / PHP Repeater count

I have a repeater using Advanced Custom fields, which when a count reaches a total (of 3) based on an iterative count, I would like to output a div, then reset the counter.
Here's the code, at the moment it's not outputting as it should and I don't know enough about math to get it to work.
Appreciate your assistance :-)
<?php if(have_rows('flexible_row')) : ?>
<div class="row">
<?php if(empty($count)){ $count=1;} while(have_rows('flexible_row')) : the_row(); ?>
<?php if(get_sub_field('column_width') == "One column") { $count = $count+1; ?>
<div class="col-sm-4">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $count; ?>
<hr/>
</div>
<?php } if(get_sub_field('column_width') == "Two columns") { $count = $count+2; ?>
<div class="col-sm-8">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $counter; ?>
<hr/>
</div>
<?php } else { $count = $count+3; ?>
<div class="col-sm-12">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $counter; ?>
<hr/>
</div>
<?php } if ($count == 3) { ?></div><div class="row"><?php $count = 0; } ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
Think I'd got my logic mixed up a bit — I wasn't using the else clauses correctly. Here's the final working code in case anyone stumbles across this in the future:
<?php if(have_rows('flexible_row')) : ?>
<div class="row">
<?php if(empty($count)){ $count=0;} while(have_rows('flexible_row')) : the_row(); ?>
<?php if(get_sub_field('column_width') == "One Column") { $count = $count+1; ?>
<div class="col-sm-4">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } elseif(get_sub_field('column_width') == "Two Columns") { $count = $count+2; ?>
<div class="col-sm-8">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } else { $count = $count+3; ?>
<div class="col-sm-12">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } if ($count == 3) { ?></div><div class="row"><?php $count = 0; } ?>
<?php endwhile; ?>
</div>
You could of course use switch statements too..

Put else statement that is inside a while loop into one div

I have a while loop and inside the while loop there is an if-else statement. All I want to do is put all the contents of the else statements into one div. It's in Wordpress.
I can't seem to get the logic behind it..
<?php /* The loop */
$i = 1;
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
static $count = 0;
if ($count == 5) { break; }
else {
?>
<div class="content_container">
<?php
if($i == 1) {
?><span style="width:370px;" class="main active"><?php the_post_thumbnail('full');?></span><?php
} else {
?><span class="child nonactive"><?php the_post_thumbnail('full'); ?></span><?php
}
$i++;
$count++;
?>
</div>
<?php
}
?>
<?php endwhile; ?>
<div class="content_container">
<span <?php if($i == 1) { ?> style="width:370px;" class="main active" <?php } else {?> class="child nonactive" ><?php the_post_thumbnail('full');?></span>
<?php }
$i++;
$count++;
?>
</div>

Find last row in a foreach query

How could I find the last post and NOT put <hr> below it?
<?php foreach($db->query("SELECT * FROM news") as $row): ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<hr>
<?php endforeach ?>
Set a counter equal to 1 which increments each round, and check if it is equal to the count() of the $query array returned. If it is, you are on the last round!
<?php
$counter = 1;
$total = count($db->query("SELECT * FROM news"));
?>
<?php foreach($db->query("SELECT * FROM news") as $row): ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<?php if($counter != $total){
?>
<hr>
<?php
}
?>
<?php $counter++; ?>
<?php endforeach ?>
A solution would be to build an array and then use join :
<?php
$arr=array();
foreach($db->query("SELECT * FROM news") as $row):
$arr[] = '<div class="NewsHeadline"><div class="NewsDate">'.$date
.' - </div><div class="NewsTitle">'.$title'
.</div></div><div class="NewsBody">'.$body.'</div>';
endforeach
echo join("<hr>",$arr);
?>
You can convert to regular for loop and then compare $i to the size of your query result:
<?php
$news = $db->query("SELECT * FROM news");
for($i=0; $i<sizeof($news);$i++){ ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<?php if(($i+1) < sizeof($news)) echo '<hr>'; ?>
<?php } ?>
If you get a count of the rows prior to the loop, you can check the count and avoid adding the HR.
$rows = $db->query("SELECT * FROM news");
$row_count = $rows.count();
$iter = 0;
foreach($rows as $row)
{
//...
if ($iter < $row_count)
{
// render hr
}
$iter++;
}
You could also go with a pure CSS approach:
div.containerClass:last-child hr { display:none; }
where "containerClass" would be whatever element your posted code sits inside of.

Tabular Input Yii

I have to input several strings into table in db. Its tabular input yii. Cannot understand, how to do it this way, if i have such controller:
public function actionCreate()
{
$model1=new Section;
$model2=new SectionI18n;
if(isset($_POST['SectionI18n'])){
foreach ($model2 as $i => $m1) {
foreach($_POST['SectionI18n'] as $i => $m1)
{
$m1=new SectionI18n;
$m1->attributes=$_POST['SectionI18n'][$i];
$my[]=$m1->attributes;
}
}
and some layout:
<?php foreach ($model2 as $i=>$m1):?>
<?php ?>
<div class="row">
<?php echo $form->labelEx($model2,'Название'); ?>
<?php echo $form->textField($model2,'[$i]title',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model2,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model2,'Описание'); ?>
<?php echo $form->textArea($model2,'[$i]description',array('size'=>60,'maxlength'=>200)); ?>
<?php echo $form->error($model2,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model2,'Язык'); ?>
<?php echo $form->dropDownList($model2,'[$i]lang',array('ru'=>'ru','en'=>'en','ua'=>'ua')); ?>
<?php echo $form->error($model2,'lang'); ?>
</div>
<?php ?>
<?php endforeach;?>
Help please
It would be something similar to this:
foreach ( $_POST[Model Name] as $i => $y ){
$var name[$i] = new Model Name;
$var name[$i]->title = $_POST[Model Name][$i][title]
...
}
var_dump the post ($_POST), object makes it easier to see.

How can I show four images per row and rows can be of any no with php

Dear Friends I have a Div of Images.
<div class="img_team_container">
<div class="img_team_subcontain">
<div class="img_team"></div>
</div>
</div>
My question is that How can I show four images per row and rows can be of any no with php.
Assuming you have a array $images of images:
<?php $i = 0; foreach($images as $image): ?>
<?php if($i === 0): ?>
<div class="row">
<?php endif; ?>
<?php echo sprintf('<img src="%s" />', $image['src']); ?>
<?php if($i === 4): $i = 0; ?>
</div>
<?php else: $i++; endif; ?>
<?php endforeach; ?>
well i dont see an image tag in your code but use modulo arithmetics.
<?
$perRow=4;
for($i=0;$i < count($myimages); $i++) {
echo '<img src="'.$myimages[$i].'"/>';
if(($i+1)%$perRow === 0) {
// we reched the end of the row, lets break
echo '<br/>';
}
}
?>

Categories