Codeigniter - Inserting Ads after each x rows - codeigniter

I want to insert advertisements after each X rows returned from database.
My Controller looks like this:
class Home extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->library('parser');
$this->load->helper('url');
$this->load->helper('download');
$this->load->model('Categorii');
$this->load->model('Comentarii');
}
public function index($pages = ""){
$c_ar = $this->Categorii->iaCategorii();
$sql = (!empty($pages)) ? "SELECT * FROM bancuri WHERE categorie = '" . $c_ar[$pages] . "'" : "SELECT * FROM bancuri";
$dbQ = $this->db->query($sql);
$renderData = array("bancuri" => $dbQ->result_array(), "ad" => "<img src='http://placehold.it/350x150'>"); //Se stocheaza datele intr-o matrice
$this->parser->parse('pages/bancuri',$renderData);
}
And view looks like this:
<div class="col-lg-8 col-sm-12 col-xs-12">
{bancuri}
{ad}
<div class="corgi_feed_well">
<div class="individual_feed_item">
<div class="feed_item">
<div class="feed_body">
<div class="row">
<!--
<audio controls>
<source src="<?php echo $this->config->item('base_url'); ?>resources/mp3/{mp3}" type="audio/mpeg">
</audio>
-->
</div>
<div class="row">
<div class="feed_profile_pic">
<span class="label label-info">Rating: {rating}</span>
<div class="clearfix"> </div>
<button type="button" class="btn btn-success">Like</button>
</div>
<div class="feed_text">
<p class="well">{banc}</p>
</div>
</div>
</div>
<div class="comment_area">
<p>Adauga un comentariu</p>
<form>
<textarea rows="3" class="span6"></textarea><br/>
<input type="submit" class="btn" />
</form>
</div>
<hr class="feed_hr" />
<div class="bottom_meta">
<div class="row">
<div class="bottom_left">
<div class="share_wrapper">
<div class="share"><i class="icon-heart"></i></div>
<div class="share_hidden">
<ul class="hover_heart">
<span class="internal_heart"><i class="icon-heart"></i> Trimite bancul pe</span>
<div class="social_links">
<li><span><i class="icon-twitter"></i> Twitter</span></li>
<li><span><i class="icon-facebook"></i> Facebook</span></li>
<li><span><i class="icon-pinterest"></i> Pinterest</span></li>
</div>
</ul>
</div>
</div>
</div>
<div class="bottom_left">
Descarca <span class="label label-primary">{descarcari} descarcari</span>
{comentarii} comentarii<br>
</div>
<div class="bottom_right">
{autor} <span>|</span> Adauga comentariu <span>|</span> {data}
</div>
</div>
</div>
</div>
</div>
</div>
{/bancuri}
The problem is that at the moment, it displays the ad (simple image atm) for every database row.

Regardless of the code you posted, In short, you just add a counter and use modulo.
so something like
$arr = range('a','z');
$n = 0;
foreach ($arr as $item)
{
if($n % 4 ==0) // 4 equals to the interval between ads
echo 'print banner';
$n++ ;
}
this will print an ad, or whatever you want, every 4 lines, play around with the numbers as you see fit and add a zero check if you don't like ads to appear in the first line.

Related

Property [Total_Sayur] does not exist on this collection instance

how to display or call value in array in laravel blade view ?
I already have the data that appears in the image below:
enter image description here
I still don't understand how to call the data in the :
<h3>#currency($laporan->Total_Sayur)</h3>
I'm tired of trying it in various ways and it always appears another error message. Please help me
MyController
public function index()
{
$users = User::count();
$kelompok = Kelompok::count();
$anggota = Anggota::count();
$laporan = Anggota::select('kecamatan', Produk::raw('avg(total_sayur) as Total_Sayur, avg(total_buah) as Total_Buah,avg(total_ikan) as Total_Ikan, avg(total_ternak) as Total_Ternak'))
->leftjoin('produk', 'produk.anggota_id', '=', 'anggota.id')
->where('kecamatan', '=', 'Ngajum')
->GroupBy('kecamatan')
->get();
// return $laporan;
return view('Dashboard.index', compact('users', 'kelompok', 'anggota', 'laporan'));
}
View
<div class="row">
<div class="col-lg-3 col-6">
<!-- small box -->
<div class="small-box bg-info">
<div class="inner">
<h3>#currency($laporan->Total_Sayur)</h3>
<p>TOTAl KEMANFAATAN</p>
</div>
<div class="icon">
<i class="ion ion-pricetagg"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>
</div>
Your solution start from here....
<div class="row">
<div class="col-lg-3 col-6">
#foreach($laporan as $item)
<!-- small box -->
<div class="small-box bg-info">
<div class="inner">
<h3>#currency($item->Total_Sayur)</h3>
<p>TOTAl KEMANFAATAN</p>
</div>
<div class="icon">
<i class="ion ion-pricetagg"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
#endforeach
</div>
</div>

Dynamic Carousel In Laravel not displaying proper data

<div class="container mt-4 mb-4">
<div class="row">
<div class="col-lg-12">
<div id="blogCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
#foreach($reviews as $items)
<div class="carousel-item #if($loop->first) active #endif">
<div class="row">
#foreach($reviews as $review)
<div class="col-lg-4 ">
<a>
<div class="home1-img mt-3">
<?php
for($i=1;$i<6;$i++){
$check = '';
if($review->number_of_stars >= $i){
$check = 'checked';
}
echo '<span class="fa fa-star '.$check.'"></span>';
}
?>
<div class="home1-text mt-1" style="margin-bottom:30px;">
<p>{!! Illuminate\Support\Str::limit($review->customer_review,100) !!}</p>
<h5 class="text-color font-weight-bold">{{ $review->customer_name }}</h5>
</div>
</div>
</a>
</div>
#endforeach
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
I am getting all the records in all sides but I want 3 records in 1st slide then 3 records in 2nd slide and so on. I have tried many times but I am not able to fix it.

remove duplicate for only title laravel

In laravel 5.7 in my web controller
public function index(Request $request){
$plan = Plan::All();
return view('web.plan.index', compact('plan'));
}
In my view file
#foreach($plan as $singlePlan)
#if(Carbon\Carbon::parse($singlePlan->date)->format('m')==10)
<div class="row brow bg-gray ">
<div class="col-md-12">
<div class="row-title">
<strong>12.31</strong>
</div>
</div>
</div>
<div class="m-departures">
<div class="row brow last-row" >
<div class="col-md-2 col-sm-2 col hidden-xs">
<div class="td center">Dec 21-22</div>
</div>
<div class="col-md-5 col-sm-5 col">
<div class="td">Fest</div>
</div>
<div class="col-xs-6 col visible-xs">
<div class="td">2 day</div>
</div>
<div class="col-md-1 col-sm-2 col hidden-xs">
<div class="td">2 day</div>
</div>
<div class="col-md-2 col-sm-3 col-xs-6 col">
<div class="td">
<span class="orange schedule-status">
4
</span>
</div>
</div>
<div class="col-md-2 col-xs-12 col tour-link hidden-sm hidden-xs">
<div class="td center"><a href="#" >view</a></div>
</div>
</div>
</div>
#endif
#endforeach
and my web looks like this
but I want it to look like this
How can I remove the duplicate title?
It would be better to group your data by the month before passing data to blade template. By using collection method mapToGroups
https://laravel.com/docs/8.x/collections#method-maptogroups
$planGroups = Plan::get()->mapToGroups(function($plan, $key) {
$planDate = \Carbon\Carbon::parse($plan->date);
return ["{$planDate->month} {$planDate->year}" => $plan];
})
->all();
You will get several groups of plans grouped by month, e.g.
[
'11 2020' => [plan, plan, ...],
'12 2020' => [plan, plan, plan, ...]
]
Then in your blade template you can loop through the months then loop through the plans.
#foreach ($planGroups as $month => $plans)
// show month heading
#foreach ($plans as $plan)
// show each plan
#endforeach
#endforeach

Laravel - How to Pass a variable to controller and to url name of route

I need to pass variable to controller of details of Boat and show it in the url.
This is the blade view where I download a list of boats available:
#foreach($Boats as $boat)
<article class="box">
<figure class="col-sm-4">
<a title="" href="{{asset('frontend')}}/ajax/cruise-slideshow-popup.html" class="hover-effect popup-gallery"><img width="270" height="160" alt="" src="{{ json_decode($boat->images, true)['url']}}"></a>
</figure>
<div class="details col-sm-8">
<div class="clearfix">
<h4 class="box-title pull-left">{{$boat->name}}<small>{{$boat->id}}</small></h4>
<span class="price pull-right"><small>from</small>$239</span>
</div>
<div class="character clearfix">
<div class="col-xs-3 cruise-logo">
<img src="http://placehold.it/110x25" alt="" />
</div>
<div class="col-xs-4 date">
<i class="soap-icon-clock yellow-color"></i>
<div>
<span class="skin-color">Date</span><br>{{$boat->year}}
</div>
</div>
<div class="col-xs-5 departure">
<i class="soap-icon-departure yellow-color"></i>
<div>
<span class="skin-color">Departure</span><br>{{$boat->homeBase}}
</div>
</div>
</div>
<div class="clearfix">
<div class="review pull-left">
<div class="five-stars-container">
<span class="five-stars" style="width: 60%;"></span>
</div>
<span>270 reviews</span>
</div>
select cruise
</div>
</div>
</article>
#endforeach
This is route route.php file to translate url:
return [
"login" => "login",
"register" => "register",
"listBoats" => "boats",
"contact" => "contact",
"detailedBoat" => "DetailBoat/{id}",
];
web.php :
Route::get(LaravelLocalization::transRoute('routes.detailedBoat'),('DetailedBoat'), function (\App\Boat $id) {
return $id;
});
DetailsBoat Controller:
public function __invoke($id)
{
echo $id;
return view('frontendViews.detailedBoat');
}
Result should be
http://localhost:8000/en/boats/{id}
The strange thing is that routes work perfectly based on app.php 'locale' setting, if i have 'en' i get http://localhost:8000/en/DetailBoat/1` and http://booking:8888/it/DettaglioBarca/%7Bid%7D switching language;
viceversa if have ['it'] on app.php i'm getting http://booking:8888/it/DettaglioBarca/1 and http://booking:8888/en/DetailBoat/%7Bid%7D in english.
Thank you

POST data from input within loop

I have a lot of products from a database. I display them with a loop. When a user clicks "more", I want to display its details. The problem is, for example: I have four products A, B, C, and D. When a user clicks A's details, it displays D's details or randomly. How can I fix this? This is my code:
<form action="details" method="post" enctype="multipart/form-data">
#foreach($images as $image)
<div class="card" >
<div class="card-inside1" >
<div class="center">
<img class="card-img-top img-fluid" src="<?php echo asset('images').'/'.$image->image1 ?>" alt="Card image" >
</div>
<div class="card-body">
<input type="text" name="imagename" value="{{$image->product_name}}"></input>
<button type="submit" value="Submit" name="more" class="btn btn-primary ">More</button>
</div>
</div>
</div>
#endforeach
</form>
route
Route::post("details",'UserController#detail');
controller
public function detail(Request $req)
{
$productname = $req->input('imagename');
return $productname;
}
There is no need of using a form to accomplish what you want.
#foreach($images as $image)
<div class="card" >
<div class="card-inside1" >
<div class="center">
<img clas="card-img-top img-fluid" src="<?php echo asset('images').'/'.$image->image1 ?>" alt="Card image" >
</div>
<div class="card-body">
<input type="text" name="imagename" value="{{$image->product_name}}"></input>
More
</div>
</div>
</div>
#endforeach
Route:
Route::get("details/{imagename}",'UserController#detail');
Controller:
public function detail(Request $req)
{
$productname = $req->imagename;
return $productname;
}
UPDATE
The reason why is taking the last product is because you are sending all products through the form. and it takes the last one that sends, not the one you clicked.
view
#foreach($images as $image)
{{ $product->name }}
#endforeach
web.php
Route::get("details/{id}",'ProductController#show')->name('product.show');
ProductController.php
public function show($id)
{
$product = Product::find($id);
return $product
}

Resources