Laravel is giving me Undefined variable Error Exception - laravel

I have this code:
<? $counter = 1 ?>
<div id="container_blog_all">
#foreach ($posts as $post)
<div class="blog_block" style="#if($counter % 3 == 0) margin-right:0px #endif">
<img class="blog_block_cover" src="{{ URL::to('uploads/blog/cover/'.$post->cover) }}">
<div class="blog_block_date">{{ $post->date }}</div>
<div class="blog_block_sep"></div>
<div class="blog_block_title">{{ $post->title }}</div>
</div>
<? $counter++; ?>
#endforeach
</div>
And I am getting this error, any reasons why??
I can see the the variable counter is defined, so why am I getting this error?

You're missing a semicolon:
<? $counter = 1 ?>
should be
<? $counter = 1; ?>

Looks like laravel doesn't like php short tags, it is better you use <?php $counter = 1 ?> rather than <? $counter = 1 ?>.That should resolve the error also.

Related

How do I get total in a foreach loop

I have a div that displays items , quantities and total prices for each item using a foreach loop. I now want to get the total amount and dont seem to get it.
This is my blade file
<div class="order-summary">
<div class="order-col">
<div><strong>PRODUCT</strong></div>
<div><strong>TOTAL</strong></div>
</div>
<div class="order-products">
#if(session('cart'))
#foreach(session('cart') as $id => $details)
<?php
$total = 0;
$total += $details['price'] * $details['quantity'] ;
$total_amount += $details['price'] * $details['quantity'];
?>
<div class="order-col">
<div>
<h4 class="nomargin">{{$details['quantity'] }}x {{ $details['name'] }}</h4>
</div>
<div>KSh {{ $total }}</div>
</div>
#endforeach
#endif
</div>
<hr>
<div class="order-col">
<div>Shiping</div>
<div><strong>KSh 300</strong></div>
</div>
<hr>
<div class="order-col">
<div><strong>TOTAL</strong></div>
<?php $total_amount += $total?>
<div><strong class="order-total">KSh {{ $total_amount }} </strong></div>
</div>
</div>
I have also tried <div><strong class="order-total">KSh <?php echo $total_amount?> </strong></div> but still does not work.
I get an error of undefined variable total_amount. How do I go about it?
The variables $total and $total_amount must be outside of the foreach
here I have rewritten your code
<div class="order-summary">
<div class="order-col">
<div><strong>PRODUCT</strong></div>
<div><strong>TOTAL</strong></div>
</div>
#php $total = 0; $total_amount = 0; #endphp
<div class="order-products">
#if(session('cart'))
#foreach(session('cart') as $id => $details)
#php
$total += $details['price'] * $details['quantity'] ;
$total_amount += $details['price'] * $details['quantity'];
#endphp
<div class="order-col">
<div>
<h4 class="nomargin">{{$details['quantity'] }}x {{ $details['name'] }}</h4>
</div>
<div>KSh {{ $total }}</div>
</div>
#endforeach
#endif
</div>
<hr>
<div class="order-col">
<div>Shiping</div>
<div><strong>KSh 300</strong></div>
</div>
<hr>
<div class="order-col">
<div><strong>TOTAL</strong></div>
#php $total_amount += $total; #endphp
<div><strong class="order-total">KSh {{ $total_amount }} </strong></div>
</div>
</div>

Check duplicate data in CodeIgniter result

I have a small forum and want to show the last 10 reply's.
My problem:
I get some duplicate entries from the database because there have some replies in one post. How I can disable all duplicate posts and show only one from all posts. I try to say, if more then 1 replys with the same forum_post_id > show only the last.
My View:
<?php foreach ($forum_reply as $item): ?>
<?php $standing = $item->forum_post_id ?>
<div class="col-lg-12 robie" onClick="location.href='<?= site_url('viewtopic/index/' . $item->forum_post_id) ?>'">
<div class="catcenter55 row">
<div class="col-md-3 deta grew">
<?php echo $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->post_head; ?>
</div>
<div class="col-md-2 deta">
<?php $topcat = $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->forum_cat_id ;?>
<?php echo $this->db->where('forum_cats2_id', $topcat)->get('forum_cats2')->row()->forum_name; ?>
</div>
<div class="col-md-2 deta">
<?php echo $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->views; ?>
</div>
<div class="col-md-2 deta">
<div class="likebg2">+<?php echo $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->likes; ?></div>
</div>
<div class="col-md-3 deta">
<?php foreach ($this->db->order_by('timestamp', 'DESC')->get('forum_reply')->result() as $stan): ?>
<?php if($standing == $stan->forum_post_id): ?>
<?php echo $this->db->where('user_id', $stan->user_id)->get('users')->row()->username; ?> | <?php echo $this->db->order_by('timestamp', 'DESC')->where('forum_reply_id', $stan->forum_reply_id)->get('forum_reply')->row()->timestamp; ?>
<?php break; ?>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endforeach; ?>
My Controller
public function index() {
$data['forum_reply'] = $this->db->get('forum_reply')->result();
}
My result
Managed to get what I think you are trying to achieve by using a group by i.e.
SELECT * FROM posts GROUP BY forumId;
However this makes things more complicated when ordering the posts, this is a bit hacky but have a look at this:
SELECT *
FROM (
SELECT *
FROM posts
ORDER BY date DESC
LIMIT 18446744073709551615
) AS sub
GROUP BY sub.forumId
ORDER BY id DESC
For reference regarding the sub query look at this previous question and this post
Thank you Jake,
I was change
$data['forum_reply'] = $this->db->get('forum_reply')->result();
to
$data['forum_reply'] = $this->db->group_by('forum_post_id')->order_by('timestamp', 'DESC')->get('forum_reply')->result();
and it´s working.

Translate to Blade+Laravel a while loop

This code works, however in my learning of Laravel, I want to know if using Blade+Laravel syntax, can be better implemented
<?php
$i = 1;
while ($i <= (5 - $post->images->count())) {
echo '<div class="col"> </div>';
$i++;
}
?>
Thanks
https://laravel.com/docs/5.5/blade#loops
I would suggest using a for loop instead of a while loop in this case:
#for ($i = 1; $i <= (5 - $post->images->count()); $i++)
<div class="col"> </div>
#endfor
I'm not sure is the best way to do, but works.
<?php $z = 0; ?>
#while ($z < 3)
{{ "test".$z }}
<?php $z++ ?>
#endwhile
#php
$i = 0;
#endphp
#while (++$i <= (5 - $post->images->count()))
<div class="col">
</div>
#endwhile
#for ($i = 0; $i <= (5 - $post->images->count()); $i++)
<div class="col"> </div>
#endfor
Yes, there is. Templating is made just for that, you can see how similar things are done with the docs : laravel blade : loops
#for ($i = 0; $i < $post->images->count()); $i++)
<div class="col"> </div>
#endfor
The better solution would be to use #foreach
#foreach( $image as $post->images )
<div class="col"> </div>
#endforeach
In this specific case beacause you are looping count() you should use foreach, however you may also be interested #forelse:
#forelse($image as $img)
<div class="col"> {{ $img }}</div>
#empty
<div class="col"> NO IMAGES </div>
#endforesle

codeigniter invalid argument supplied for foreach() on join clause

i try all of away that i know and suggestion on internet.
but all time return false.
please help me...
thanks
my error message is:
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/index.php
Line Number: 10
model.php
public function getBanner()
{
$this->db->select('album.id, album.cat_id , album.poster, album.slug, album.modify, category.id, category.name, category.slug');
$this->db->from('album,category');
$this->db->where('album.cat_id', 'category.id');
$query = $this->db->get();
if($query->num_rows() != 0)
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
controllers.php
$data['banner'] = $this->Fornt_model->getBanner();
showbanner.php
<section class="banner">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php $item = 1; foreach($banner as $latest_poster): $item_class = ($item == 1) ? 'item active' : 'item'; ?>
<div class="<?php echo $item_class; ?>">
<img src="<?php echo base_url('images/poster/'.$latest_poster['poster']); ?>" class="img-responsive img-thumbnail">
<div class="carousel-caption caption">
<a href="<?php echo site_url('category/bollywood/'.$latest_poster['slug']); ?>" class="box">
<b class="h3"><?php echo $latest_poster['name']; ?></b>
<small class="p">Exclusive AT*<?php echo substr($latest_poster['modify'], 0, 10); ?></small>
</a>
</div>
</div>
<?php $item++; endforeach; ?>
</div>
</div>
</div>
</div>
<div class="clearfix" style="padding: 10px 0;"></div>
<div class="row hidden-xs">
<?php $itemto = 0; foreach($banner as $latest_poster): $item_class_active = ($itemto == 0) ? 'active' : ''; ?>
<div class="col-sm-2 pointer" data-target="#myCarousel" data-slide-to="<?php echo $itemto; ?>"><img src="<?php echo base_url('images/poster/'.$latest_poster['poster']); ?>" class="img-responsive img-thumbnail" /></div>
<?php $itemto++; endforeach; ?>
</div>
</div>
I think something wrong with your query.. try
if($query->num_rows() != 0)
{
$result = $query->result_array();
print_r($result);
}
make sure your array data is available..
If there is no rows, front_model->getBanner() will return the result as false. Due to this you are getting an error. Use following code
public function getBanner()
{
$this->db->select('album.id, album.cat_id , album.poster, album.slug, album.modify, category.id, category.name, category.slug');
$this->db->from('album,category');
$this->db->where('album.cat_id', 'category.id');
$query = $this->db->get();
return $query->result_array();
}

model->validate displays errors for text field which has value in yii

I have an application where user has to fill a lengthy form, so I split the form into three and created steps - Step 1, Step 2 etc. So it has to be convenient for users. Now I face a problem, after filling out the details, a text field shows an error. When using model->get Errors() the error is 'address cannot be blank'. When I use $_POST the arrays displays value for the field address. I started yii framework last week and I do not know where I have gone wrong. Any help appreciated.
The view code i use forloop to reduce my code -
<fieldset>
<?php if(!Yii::app()->user->hasFlash('success')):
Yii::app()->user->setFlash('warning','All Fields are mandatory!');
endif; ?>
<?php echo CHtml::errorSummary($model, null, null, array(
'class' => 'alert alert-danger col-lg-offset-2',
)); ?>
<?php $rrr = -1;
foreach($model->attributeLabels() as $a){ $rrr++;
if($rrr<27 && $rrr>=12){
?>
<div class="form-group">
<?php echo CHtml::activeLabel($model, $a, array('class' => 'col-lg-3 control-label')); ?>
<div class="col-lg-7">
<?php echo CHtml::activeTextField($model,array_keys($model->attributeLabels())[$rrr],array('value'=>'a','size'=>255,'maxlength'=>255,'class'=>'form-control formtype1')); ?>
</div>
</div>
<?php
}}
?>
<?php $rrr = -1;
foreach($model2->attributeLabels() as $a){ $rrr++;
if($rrr>1 && $rrr<6){
?>
<div class="form-group">
<?php echo CHtml::activeLabel($model2, $a, array('class' => 'col-lg-3 control-label')); ?>
<div class="col-lg-7">
<?php echo CHtml::activeTextField($model2,array_keys($model2->attributeLabels())[$rrr],array('value'=>'a','size'=>255,'maxlength'=>255,'class'=>'form-control formtype1')); ?>
</div>
</div>
<?php
}}
?>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-sm btn-primary" type="submit"><?php echo Yii::t("user", "Submit") ?></button>
</div>
</div>
</fieldset>
</form>

Resources