If statement inside echoed html not showing inside the html - php

I want to shorten a string when it's longer than a certain length for example 20.
But when I add the if statement to my echoed html it outputs the result outside of the echoed html. Any idea why this happens?
My code:
foreach($contentcr as $content)
{
$contentje .= '<li class="job_listing">
<a href="http://www.website.nl/_extern/website/content.php?alias='.$content['alias'].'">
<div class="location job_img">
<img src="images/samples/person1.jpg" alt="" class="company_logo">
</div>
<div class="location boldfont">
'.$content['title'].'
</div>
<div class="location">
'.$content['fulltext'].'';
if (strlen($content['fulltext']) >= 20){
echo 'blala';
}
$contentje .='</div>
<div class="rating location">
<div class="rating-stars">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
</div>
</div>
<div class="location">
'.$content['metakey'].'
</div>
<div class="location">
<button type="submit" class="btn btn-primary buttonblock" onClick="location.href="http://www.website.nl/_extern/website/content.php?page="'.$content['alias'].'">Nu huren</button>
</div>
</a>
</li>'; // Here
}
This is a table like structure, but the output of the if statement is echoed outside the html markup.

You are echoing the string inside if statement rather than appending that to your $contentje variable. See if statement below:
foreach($contentcr as $content)
{
$contentje .= '<li class="job_listing">
<a href="http://www.website.nl/_extern/website/content.php?alias='.$content['alias'].'">
<div class="location job_img">
<img src="images/samples/person1.jpg" alt="" class="company_logo">
</div>
<div class="location boldfont">
'.$content['title'].'
</div>
<div class="location">
'.$content['fulltext'].'';
if (strlen($content['fulltext']) >= 20){
$contentje .= 'blala';
}
$contentje .='</div>
<div class="rating location">
<div class="rating-stars">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
</div>
</div>
<div class="location"> '.$content['metakey'].'</div>
<div class="location">
<button type="submit" class="btn btn-primary buttonblock" onClick="location.href="http://www.website.nl/_extern/website/content.php?page="'.$content['alias'].'">Nu huren</button>
</div>
</a></li>'; // Here
}

Related

Displaying product in a row with three products laravel

I wrote the following code to display some products on a page but all that I want is to display only 3 items in a row.
My code for displaying all products (i have to display more than 15 products):
<div class="products">
<div class="row">
#foreach($products as $item)
<div class="col-md-4 col-sm-6 col-xs-12 ">
<div class="blog-item item swin-transition">
<div class="block-img">
<img style="width: 269.844px;height: 254.984px " src="{{url('images/product/'.$item['image'])}}" alt="" class="img img-responsive">
<div class="group-btn">
<i class="icons fa fa-link"></i>
<i class="fa fa-shopping-basket"></i>
</div>
</div>
<div class="block-content">
<h6 class="title" style="font-size: 15px">
{{$item['name_'.$lang]}}
</h6>
<div class="product-info">
<ul class="list-inline">
<li class="author">
<span>{{Lang::get('frontend.price')}}:</span>
<span class="text">{{number_format($item['online_price'])}} {{Lang::get('frontend.unit')}}</span>
</li>
<li class="rating">
<a href="javascript:void(0)" tabindex="0">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
php `array_chunk()` function will serve your purpose [http://php.net/manual/en/function.array-chunk.php][1]
<div class="products">
#foreach(array_chunk($products,3,true) as $chunk)
<div class="row">
#foreach($chunk as $item)
<div class="col-md-4 col-sm-6 col-xs-12 ">
<div class="blog-item item swin-transition">
<div class="block-img">
<img style="width: 269.844px;height: 254.984px " src="{{url('images/product/'.$item['image'])}}" alt="" class="img img-responsive">
<div class="group-btn">
<i class="icons fa fa-link"></i>
<i class="fa fa-shopping-basket"></i>
</div>
</div>
<div class="block-content">
<h6 class="title" style="font-size: 15px">{{$item['name_'.$lang]}}</h6>
<div class="product-info">
<ul class="list-inline">
<li class="author"><span>{{Lang::get('frontend.price')}}:</span><span class="text">{{number_format($item['online_price'])}} {{Lang::get('frontend.unit')}}</span></li>
<li class="rating"><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i></li>
</ul>
</div>
</div>
</div>
</div>
#endforeach
</div>
#endforeach;
</div>
[1]: http://php.net/manual/en/function.array-chunk.php

PHP Data Looping in girds

I have been trying to loop Data from a Rest , and i am able to do it without grid but with grid it just doesn't work . Here is the Source code where i am trying to loop .
THE CODE WITH ERROR
<?php
$json = file_get_contents('https://newsapi.org/v2/everything?domains=wsj.com&apiKey=5d');
$news= ( json_decode($json, true));
$ii = 0;
foreach ($news["articles"] as $headling["articles"]) {?>
<div class="row"><?php
foreach ( $headling["articles"] as $i => $newsdata){?>
<?php if($ii==0){
?>
<div class="col-md-6">
<div class="card">
<div class="header">
<h4 class="title">Email Statistics</h4>
<p class="category">Last Campaign Performance</p>
</div>
<div class="content">
<img src="<?php echo $newsdata['urlToImage'];?>" style="width:100%;" /> <div class="footer">
<div class="legend">
<i class="fa fa-circle text-info"></i> Open
<i class="fa fa-circle text-danger"></i> Bounce
<i class="fa fa-circle text-warning"></i> Unsubscribe
</div>
<hr>
<div class="stats">
<i class="fa fa-clock-o"></i> Campaign sent 2 days ago
</div>
</div>
</div>
</div>
</div>
<?php
$ii =1 ;
}else{
?>
<div class="col-md-6">
<div class="card">
<div class="header">
<h4 class="title">Users Behavior</h4>
<p class="category">24 Hours performance</p>
</div>
<div class="content">
<img src="<?php echo $newsdata['urlToImage'];?>" style="width:100%;" /> <div class="footer">
<div class="footer">
<div class="legend">
<i class="fa fa-circle text-info"></i> Open
<i class="fa fa-circle text-danger"></i> Click
<i class="fa fa-circle text-warning"></i> Click Second Time
</div>
<hr>
<div class="stats">
<i class="fa fa-history"></i> Updated 3 minutes ago
</div>
</div>
</div>
</div>
</div>
<?php
$ii = 0;
}
}
?>
</div>
<?php
}
?>
THE PROBLEM ?
It is not giving me any Error, I have been try to do it with a inner loop.But it just doesn't work Here is the Screenshot. Well basically what i am thinking is its not looping the rows .
Here it is
EDIT
The loop inside a loop was just useless.
You forgot a </div> inside the else { ... } part. See the pretty formatted code:
<div class="col-md-6">
<div class="card">
<div class="header">
<h4 class="title">Users Behavior</h4>
<p class="category">24 Hours performance</p>
</div>
<div class="content">
<img src="<?php echo $newsdata['urlToImage'];?>" style="width:100%;" />
<div class="footer">
<div class="footer">
<div class="legend">
<i class="fa fa-circle text-info"></i> Open
<i class="fa fa-circle text-danger"></i> Click
<i class="fa fa-circle text-warning"></i> Click Second Time
</div>
<hr>
<div class="stats">
<i class="fa fa-history"></i> Updated 3 minutes ago
</div>
</div>
</div>
</div>
</div>

why this is Invalid argument supplied for foreach()?

I don't understand why, the error says :
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\twitter\core\ajax\popuptweets.php on line 134 , and I don't see any error or maybe I'm blind !!
popuptweets.php code
<?php
include '../init.php';
if (isset($_POST['showpopup']) && !empty($_POST['showpopup'])) {
$tweetID = $_POST['showpopup'];
$user_id = $_SESSION['user_id'];
$tweet = $getFromT->getPopupTweet($tweetID);
$user = $getFromU->userData($user_id);
$likes = $getFromT->likes($user_id, $tweetID);
$retweet = $getFromT->checkRetweet($tweetID, $user_id);
$comments = $getFromT->comments($tweetID);
?>
<div class="tweet-show-popup-wrap">
<input type="checkbox" id="tweet-show-popup-wrap">
<div class="wrap4">
<label for="tweet-show-popup-wrap">
<div class="tweet-show-popup-box-cut">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
</label>
<div class="tweet-show-popup-box">
<div class="tweet-show-popup-inner">
<div class="tweet-show-popup-head">
<div class="tweet-show-popup-head-left">
<div class="tweet-show-popup-img">
<img src="<?php echo BASE_URL.$tweet->profileImage;?>"/>
</div>
<div class="tweet-show-popup-name">
<div class="t-s-p-n">
<a href="<?php echo BASE_URL.$tweet->username;?>">
<?php echo $tweet->screenName;?>
</a>
</div>
<div class="t-s-p-n-b">
<a href="<?php echo BASE_URL.$tweet->username;?>">
#<?php echo $tweet->username;?>
</a>
</div>
</div>
</div>
<div class="tweet-show-popup-head-right">
<button class="f-btn"><i class="fa fa-user-plus"></i> Follow </button>
</div>
</div>
<div class="tweet-show-popup-tweet-wrap">
<div class="tweet-show-popup-tweet">
<?php echo $getFromT->getTweetLinks($tweet->status);?>
</div>
<div class="tweet-show-popup-tweet-ifram">
<?php if(!empty($tweet->tweetImage)){?>
<img src="<?php echo BASE_URL.$tweet->tweetImage;?>"/>
<?php }?>
</div>
</div>
<div class="tweet-show-popup-footer-wrap">
<div class="tweet-show-popup-retweet-like">
<div class="tweet-show-popup-retweet-left">
<div class="tweet-retweet-count-wrap">
<div class="tweet-retweet-count-head">
RETWEET
</div>
<div class="tweet-retweet-count-body">
<?php echo $tweet->retweetCount;?>
</div>
</div>
<div class="tweet-like-count-wrap">
<div class="tweet-like-count-head">
LIKES
</div>
<div class="tweet-like-count-body">
<?php echo $tweet->likesCount;?>
</div>
</div>
</div>
<div class="tweet-show-popup-retweet-right">
</div>
</div>
<div class="tweet-show-popup-time">
<span><?php echo $tweet->postedOn;?></span>
</div>
<div class="tweet-show-popup-footer-menu">
<ul>
<?php if($getFromU->loggedIn() === true){
echo '<li><i class="fa fa-share" aria-hidden="true"></i></a></button></li>
<li>'.(($tweet->tweetID === $retweet['retweetID']) ? '<button class="retweeted" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><a href="#"><i class="fa fa-retweet" aria-hidden="true"></i><span class="retweetsCount">'.$tweet->retweetCount.'</span></button>' : '<button class="retweet" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><a href="#"><i class="fa fa-retweet" aria-hidden="true"></i><span class="retweetsCount">'.(($tweet->retweetCount > 0) ? $tweet->retweetCount : '').'</span></button>').'</li>
<li>'.(($likes['likeOn'] === $tweet->tweetID) ? '<button class="unlike-btn" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><i class="fa fa-heart-o" aria-hidden="true"></i><span class="likesCounter">'.$tweet->likesCount.'</span></button>' : '<button class="like-btn" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><i class="fa fa-heart" aria-hidden="true"></i><span class="likesCounter">'.(($tweet->likesCount > 0) ? $tweet->likesCount : '').'</span></button>').'</li>
<li>
<i class="fa fa-ellipsis-h" aria-hidden="true"></i>
<ul>
<li><label class="deleteTweet">Delete Tweet</label></li>
</ul>
</li>';
}else{
?>
<li><button type="buttton"><i class="fa fa-share" aria-hidden="true"></i></button></li>
<li><button type="button"><i class="fa fa-retweet" aria-hidden="true"></i><span class="retweetsCount">RETWEET-COUNT</span></button></li>
<li><button type="button"><i class="fa fa-heart" aria-hidden="true"></i><span class="likesCount">LIKES-COUNT</span></button></button></li>
<?php }?>
</ul>
</div>
</div>
</div><!--tweet-show-popup-inner end-->
<?php if($getFromU->loggedIn() === true){?>
<div class="tweet-show-popup-footer-input-wrap">
<div class="tweet-show-popup-footer-input-inner">
<div class="tweet-show-popup-footer-input-left">
<img src="<?php echo BASE_URL.$user->profileImage?>"/>
</div>
<div class="tweet-show-popup-footer-input-right">
<input id="commentField" type="text" name="comment" placeholder="Reply to #<?php echo $tweet->username;?>">
</div>
</div>
<div class="tweet-footer">
<div class="t-fo-left">
<ul>
<li>
<label for="t-show-file"><i class="fa fa-camera" aria-hidden="true"></i></label>
<input type="file" id="t-show-file">
</li>
<li class="error-li">
</li>
</ul>
</div>
<div class="t-fo-right">
<span id="count">140</span>
<input type="submit" id="postComment">
</div>
</div>
</div><!--tweet-show-popup-footer-input-wrap end-->
<?php }?>
<div class="tweet-show-popup-comment-wrap">
<div id="comments">
<?php
foreach ($comments as $comment) {
echo '<div class="tweet-show-popup-comment-box">
<div class="tweet-show-popup-comment-inner">
<div class="tweet-show-popup-comment-head">
<div class="tweet-show-popup-comment-head-left">
<div class="tweet-show-popup-comment-img">
<img src="'.BASE_URL.$comment->profileImage.'">
</div>
</div>
<div class="tweet-show-popup-comment-head-right">
<div class="tweet-show-popup-comment-name-box">
<div class="tweet-show-popup-comment-name-box-name">
'.$comment->screenName.'
</div>
<div class="tweet-show-popup-comment-name-box-tname">
#'.$comment->username.' - '.$comment->commentAt.'
</div>
</div>
<div class="tweet-show-popup-comment-right-tweet">
<p>#'.$tweet->username.' '.$comment->comment.'</p>
</div>
<div class="tweet-show-popup-footer-menu">
<ul>
<li><button><i class="fa fa-share" aria-hidden="true"></i></button></li>
<li><i class="fa fa-heart-o" aria-hidden="true"></i></li>
<li>
<i class="fa fa-ellipsis-h" aria-hidden="true"></i>
<ul>
<li><label class="deleteTweet">Delete Tweet</label></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
<!--TWEET SHOW POPUP COMMENT inner END-->
</div>
';
}
?>
</div>
</div>
<!--tweet-show-popup-box ends-->
</div>
</div>
<?php
}
?>
and the error on foreach that's means on this code
foreach ($comments as $comment) {
echo '<div class="tweet-show-popup-comment-box">
<div class="tweet-show-popup-comment-inner">
<div class="tweet-show-popup-comment-head">
<div class="tweet-show-popup-comment-head-left">
<div class="tweet-show-popup-comment-img">
<img src="'.BASE_URL.$comment->profileImage.'">
</div>
</div>
<div class="tweet-show-popup-comment-head-right">
<div class="tweet-show-popup-comment-name-box">
<div class="tweet-show-popup-comment-name-box-name">
'.$comment->screenName.'
</div>
<div class="tweet-show-popup-comment-name-box-tname">
#'.$comment->username.' - '.$comment->commentAt.'
</div>
</div>
<div class="tweet-show-popup-comment-right-tweet">
<p>#'.$tweet->username.' '.$comment->comment.'</p>
</div>
<div class="tweet-show-popup-footer-menu">
<ul>
<li><button><i class="fa fa-share" aria-hidden="true"></i></button></li>
<li><i class="fa fa-heart-o" aria-hidden="true"></i></li>
<li>
<i class="fa fa-ellipsis-h" aria-hidden="true"></i>
<ul>
<li><label class="deleteTweet">Delete Tweet</label></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
<!--TWEET SHOW POPUP COMMENT inner END-->
</div>
';
}
where is the error here ?
Arguments for foreach must be an array, you need to var_dump($comments) to see whether it's an array or not. In your case, returning result may be null so it returns warning for foreach. To ensure the foreach working, you may use this:
foreach ((array)$comments as $comment) {
}
I compared your code with mine and found a difference in this line: Do not worry, the error is there.
<li>
'.(($tweet->tweetID ===$retweet['retweetID']) ?
'<button class="retweeted" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><a href="#"><i class="fa fa-retweet" aria-hidden="true"></i><span class="retweetsCount">'.$tweet->retweetCount.'</span></button>'
:
'<button class="retweet" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><a href="#"><i class="fa fa-retweet" aria-hidden="true"></i><span class="retweetsCount">'.(($tweet->retweetCount > 0) ? $tweet->retweetCount : '') .'</span></button>' ).'<button class="retweet" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><a href="#"><i class="fa fa-retweet" aria-hidden="true"></i><span class="retweetsCount"></span></button>
</li>

Laravel paginator doesn't work properly

The laravel paginator does work, but not good in my case.
So I have a forum with comments and the paginator must display when there are more than 10 comments on that page.
So I have a topic that has more than 10 comments and then it works fine on that page, but when I go to another page, he does take the pagination of the most comments. That is not what I want.
Here are some screens and code:
Here are there more than 10 comments, so it is good that it displays the paginator. The strange thing here is that it only shows 2 comments and not 10.
Here is another topic with just 8 comments, so the pagination shouldn't display anything:
But as you can see, it does show the paginator....
My code look like this:
Controller:
public function thread($title)
{
$thread = Thread::where('title', '=', str_replace('Thread-', '',str_replace('-', ' ', $title)))->first();
$usercolor = DB::table('threads')
->join('user', 'threads.uid', '=', 'user.id')
->join('role', 'user.role_id', '=', 'role.id')
->where('threads.uid', '=', $thread->uid)
->first();
$usertitle = DB::table('threads')
->join('user', 'threads.uid', '=', 'user.id')
->where('threads.uid', '=', $thread->uid)
->first();
$comments = Comment::orderBy('posted_at', 'asc')
->where('deleted_at', '=', '0000-00-00 00:00:00')
->with('user.role')
->paginate(10);
if (!$thread) {
return die('bestaat niet!');
}else{
return View::make('thread')->with('threads', $thread)
->with('comments', $comments)
->with('usercolor', $usercolor)
->with('usertitle', $usertitle);
}
}
View:
#include('globs.header')
<div class="col-sm-offset-1 col-sm-10">
<div class="clearfix"> </div>
#if(Request::get('page', 1) == 1)
<div class="panel panel-default" id="a9">
<div class="panel-heading">
<div class="panel-title">{{ $threads->title }}
</div>
</div>
<div class="panel-body thread-row">
<div class="row thread-row">
<div class="col-md-2 col-sm-3 text-center userblock">
<div class="clerafix"> </div>
<img class="img-thumbnail avatar" src="./public/img/avatar.jpg" alt="Avatar" height="55px;" width="55px">
<div class="push_bottom_5" style="color:{{ $usercolor->colour }};"><strong>{{ $threads->username }}</strong></div>
<div class="push_bottom_5"><small>{{ $usertitle->usertitle }}</small></div>
<div class="label label-default" style="background-color:{{ $usercolor->colour }};">{{ $usercolor->name }}</div>
<div class="push_bottom_5">
#if($usercolor->id == '1')
<i class="fa fa-star"></i>
#elseif($usercolor->id == '2')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($usercolor->id == '3')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($usercolor->id == '4')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($usercolor->id == '5')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($usercolor->id >= '6')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#endif
</div>
<div class="text-muted text-left"><small>Total Posts: 66</small></div>
<div class="text-muted text-left"><small>Joined 05-11-2014</small></div>
<div class="clerafix"> </div>
</div>
<div class="col-md-5 col-sm-6 col-xs-8">
<div class="clerafix"> </div>
<div class="text-muted"><span><i class="fa fa-calendar"></i> </span><small>
<span>Posted:</span> {{ date("d-m-Y", strtotime($threads->date_posted)) }}</small></div>
</div>
<div class="col-md-10 col-sm-9 col-xs-12">
<div class="clerafix"> </div>
<div class="content_body"><p>{{ Helper::Filter(BBCode::parse($threads->message)) }}</p>
</div>
<div class="clerafix"> </div>
</div>
</div>
</div>
<div class="panel-footer clearfix">
<div class="pull-right">
<!-- acties -->
<button class="btn btn-primary btn-sm"><i class="fa fa-edit"></i> Aanpassen</button>
<button class="btn btn-primary btn-sm"><i class="fa fa-quote-left"></i> Quote</button>
<button class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> Verwijder</button>
<button class="btn btn-warning btn-sm"><i class="fa fa-exclamation"></i> Rapporteer</button>
</div>
</div>
</div>
<!-- end of the topic -->
</div>
#endif
<!-- begin of comments -->
#foreach($comments as $comment)
#if($comment->tid == $threads->tid)
<div class="col-sm-offset-1 col-sm-10">
<div class="clearfix"> </div>
<div class="panel panel-default" id="a9">
<div class="panel-body thread-row">
<div class="row thread-row">
<div class="col-md-2 col-sm-3 text-center userblock">
<div class="clerafix"> </div>
<img class="img-thumbnail avatar" src="./public/img/avatar.jpg" alt="Avatar" height="55px;" width="55px">
<div class="push_bottom_5" style="color:{{ $comment->user->role->colour }};"><strong>{{ $comment->username }}</strong></div>
<div class="push_bottom_5"><small>{{ $comment->user->usertitle }}</small></div>
<div class="label label-default" style="background-color:{{ $comment->user->role->colour }};">{{ $comment->user->role->name }}</div>
<div class="push_bottom_5">
#if($comment->user->role->id == '1')
<i class="fa fa-star"></i>
#elseif($comment->user->role->id == '2')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($comment->user->role->id == '3')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($comment->user->role->id == '4')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($comment->user->role->id == '5')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($comment->user->role->id >= '6')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#endif
</div>
<div class="text-muted text-left"><small>Total Posts: 66</small></div>
<div class="text-muted text-left"><small>Joined 05-11-2014</small></div>
<div class="clerafix"> </div>
</div>
<div class="col-md-5 col-sm-6 col-xs-8">
<div class="clerafix"> </div>
<div class="text-muted"><span class="hidden-md "><i class="fa fa-calendar"></i>
</span><small><span>Posted:</span> {{ date("d-m-Y", strtotime($comment->posted_at)) }}</small></div>
</div>
<div class="col-md-10 col-sm-9 col-xs-12">
<div class="clerafix"> </div>
<div class="content_body"><p>{{ Helper::Filter(BBCode::parse($comment->comment)) }}</p>
</div>
<div class="clerafix"> </div>
</div>
</div>
</div>
<div class="panel-footer clearfix">
<div class="pull-right">
<!-- acties -->
<button class="btn btn-primary btn-sm"><i class="fa fa-edit"></i> Aanpassen</button>
<button class="btn btn-primary btn-sm"><i class="fa fa-quote-left"></i> Quote</button>
<button class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> Verwijder</button>
<button class="btn btn-warning btn-sm"><i class="fa fa-exclamation"></i> Rapporteer</button>
</div>
</div>
</div>
</div>
#endif
#endforeach
<!-- end of comments -->
#if(Auth::check())
<div class="col-sm-offset-1 col-sm-10">
<div class="clearfix"> </div>
<div class="panel panel-default" id="a9">
<div class="panel-heading"><h4>Snelle reactie plaatsen</h4></div>
<div class="panel-body">
{{ Form::open(array('url' => 'SubmitComment')) }}
<input type="hidden" value="{{ $threads->tid }}" name="tid">
{{ Honeypot::generate('spamprt', 'time') }}
<textarea name="comment" class="form-control" col="250" rows="5" placeholder="Typ uw bericht..."></textarea>
<div class="clearfix"> </div>
<button class="btn btn-success pull-left" type="submit" name="action">
Reageer
</button>
{{ Form::close() }}
</div>
</div>
</div>
#endif
<div class="col-md-4 col-md-offset-5">{{ $comments->links() }}</div>
</div>
</div>
#include('globs.footer')
I hope you guys will understand my problem and are be able to find a perfect solution with me.
It looks like you always retrieve all comments. I can also see this in your template, where you wrap your code in #if($comment->tid == $threads->tid) within your foreach.
This means that later in your template, where you retrieve the pagination links, it will retrieve the links for all comments, but not all comments are shown in the page, since you filter them manually in the loop.
Query the comments like
$comments = Comment::orderBy('posted_at', 'asc')
->where('deleted_at', '=', '0000-00-00 00:00:00')
->where('tid', $threads->tid) // it's about this line!
->with('user.role')
->paginate(10);
Note:
Above should provide a solution for your problem, but as an extra, you could also look into relationships. As an addition, this answer on SO "Laravel 4.1: How to paginate eloquent eager relationship?" (Since you tagged your post with Laravel-4) has some nice arguments on how to approach this efficiently.

Get child items recursively with PHP

I have a table in MySQL that holds Menu Items, and each has a parent, that can be either 0 (Top Menu) or the ID of another Item.
I'm working on a controller within CodeIgniter that was started by another programmer, and here's the brilliant way he found to sweep through the items of each item, recursively.
// I know there's a better way to do this, but as time is short we have to run
$data['aplicacoes_list'] = '';
$aplicacoes = $this->aplicacoes_model->get_aplicacoes_no_parent();
foreach($aplicacoes as $apl){
$data['aplicacoes_list'] .= '<li class="dd-item" data-id="3">
<div class="dd-handle">'.$apl['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>';
$sub_aplicacoes = $this->aplicacoes_model->get_aplicacoes_by_parent_id($apl['id']);
foreach($sub_aplicacoes as $sub_apl){
$data['aplicacoes_list'] .= '<ol class="dd-list">
<li class="dd-item" data-id="4">
<div class="dd-handle">'.$sub_apl['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>';
$sub_aplicacoes2 = $this->aplicacoes_model->get_aplicacoes_by_parent_id($sub_apl['id']);
foreach($sub_aplicacoes2 as $sub_apl2){
$data['aplicacoes_list'] .= '<ol class="dd-list">
<li class="dd-item" data-id="4">
<div class="dd-handle">'.$sub_apl2['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>';
}
}
}
So, as you can see, the list goes on until $sub_aplicacoes7, which is just moronic.
Can you guys think of a better way to accomplish this?
Here's a screenshot of the database, as it is.
Here's a recursive method for you:
private function recursive_aplicacoes( $parent = 0 )
{
$sub_aplicacoes = $this->aplicacoes_model->get_aplicacoes_by_parent_id($parent);
if(!count($sub_aplicacoes)) return '';
$s = '<ol class="dd-list">';
foreach($sub_aplicacoes as $sub_apl)
{
$s .= '<li class="dd-item" data-id="4">
<div class="dd-handle">'.$sub_apl['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>';
$s .= $this->recursive_aplicacoes($sub_apl['id']);
}
$s .= '</ol>';
return $s;
}
Put it in same class.
Here's how to use:
$data['aplicacoes_list'] = $this->recursive_aplicacoes();
Should work :)
You could move the code for generating a submenu into it's own function and use recursion. While this will solve your problem may I recommend that, once you've built the menu for a user that you cache it until s/he logs out as your menu system can quickly become quite complex and slow to load the more embedded menus you add (you end up with an recursive N + 1 problem).
$data['aplicacoes_list'] = '';
$aplicacoes = $this->aplicacoes_model->get_aplicacoes_no_parent();
foreach($aplicacoes as $apl){
$data['aplicacoes_list'] .= '<li class="dd-item" data-id="3">
<div class="dd-handle">'.$apl['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>' . build_submenu($this->aplicacoes_model, $apl['id']);
}
function build_submenu($model, $id) {
$aplicacoes = $model->get_aplicacoes_by_parent_id($id);
if (empty($aplicacoes)) {
return '';
}
$submenu = '';
foreach($aplicacoes as $sub){
$submenu .= '<ol class="dd-list">
<li class="dd-item" data-id="4">
<div class="dd-handle">'.$sub['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>' . build_submenu($model, $sub['id']);
}
return $submenu;
}
You may need to fiddle with this to get the correct HTML tags/CSS that you need.

Categories