The following Codeigniter controller outputs a JSON when accessed through url.
Codeigniter controller
function ttoday(){
$this->load->model('dashboard_stats');
$tours_today = $this->dashboard_stats->upcoming_tours();
$data_json = json_encode($tours_today);
echo $data_json;
}
Codeigniter Model
function upcoming_tours()
{
mysql_query("SET time_zone = '+3:00'");
$this->db->order_by('from_start_time','asc');
$this->db->where('DATE(from_start_time) = CURDATE()');
$this->db->where('from_start_time > DATE_SUB(NOW(), INTERVAL 90 MINUTE)');
$query = $this->db->get('tours');
return $query->result();
}
Angularjs Controller
translineClassic.controller('homeController', ['$scope', '$http', '$window', '$rootScope', function($scope, $http, $window, $rootScope){
$http.get('http://122.222.22.2/booking/public_html/admin/api/cities')
.success(function(result){
$scope.cities = result;
});
$http.get('http://122.222.22.2/booking/public_html/admin/api/ttoday')
.success(function(result){
$scope.tours = result;
});
}]);
Encoded Json
[{"tour_id":"4260","route_time":"121473438600","tour_route":"12","from_start_time":"2016-09-09 19:30:00","available_seats":"47","start_price":"800.00","additional":"0","book_online":"0","date_created":"2016-08-30 09:50:05"},{"tour_id":"4412","route_time":"111473438600","tour_route":"11","from_start_time":"2016-09-09 19:30:00","available_seats":"47","start_price":"800.00","additional":"0","book_online":"0","date_created":"2016-08-30 11:34:46"}]
In the database where the routes are stored. We have stored the tour_route id and not the route name. There is a separate table that stores the routes, and I can get the route name by using the following code.
function get_route_name($id){
$this->db->select('route_name');
$this->db->where('route_id', $id);
$query = $this->db->get('routes');
if ($query->num_rows() > 0)
{
$row = $query->row();
return $row->route_name;
}
}
Using the code as shown above, I can be able to use the $http for angularjs to get the data and then bind with the view.
The problem arises when I need to show the route name. but instead I have tour_route, I can be able to do this in php using the get_route_name() function, How can I print route name instead of tour_route? Do I have to use the $http again? Please help me on this. I am new to angularJs and any help will be appreciated.
My View
<article class="result">
<div ng-repeat="tour in tours">
<div class="one-fourth heightfix"><img class="img-responsive" src="assets/images/translinebus.JPEG" alt="" /></div>
<div class="one-half heightfix">
<h3>{{ tour.tour_route }} ?</h3>
<ul>
<li>
<span class="ico people"></span>
<p>Max people</strong> <br/>
Seats Remaining </strong> </p>
</li>
<li>
<span class="ico luggage"></span>
<p>Max <strong>3 suitcases</strong> <br />per vehicle</p>
</li>
<li>
<span class="ico time"></span>
<p>Departure Time<br />
<strong><span class="icon-clock" style="color:red;">
{{ tour.from_start_time }} </span>
</strong></p>
</li>
</ul>
</div>
<div class="one-fourth heightfix">
<div>
<div class="price"><small>KES</small> {{ tour.start_price }}</div>
<span class="meta">per passenger</span>
<button value="{{ tour.tour_id }}" type="submit" class="btn grey large">select</button>
</div>
</div>
<div class="full-width information" ng-show="showDetails">
x
<p>Free Wifi, Charging</p>
</div>
</div>
</article>
Related
I've added a ajax search box in app's home page. It works fine. But when i use the pagination after the new results shows up, pagination leads me to the old results. I'm not sure what I'm not doing right. So looking for expert's help to get a solution for it.
I've these in routes --
Route::get('/', 'BookmarkController#index');
Route::post('/', 'BookmarkController#search');
I've these in their controller -
public function index(Request $request)
{
$tags_list = Tag::orderBy('tag', 'asc')->get();
$bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->paginate(10);
$bookmarks_all = Bookmark::orderBy('created_at','desc')->where('public', '1')->get();
return view('welcome')->with('bookmark', $bookmarks)->with('tags_list', $tags_list)->with('bookmarks_all', $bookmarks_all);
}
public function search(Request $request){
$search_value = $_POST['search'];
$bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->where('title', 'rlike', $search_value)->orwhere('description', 'rlike', $search_value)->orwhere('contents', 'rlike', $search_value)->orwhere('tags', 'rlike', $search_value)->paginate(10);
return view('public_bookmarks')->with('bookmark', $bookmarks);
}
In welcome.blade.php
<div class="container content-container">
<div class="row ">
<div class="col-sm-12">
<div class="page-header">
<div class="row">
<span class="title col-sm-8">Recent Bookmarks</span>
<form id="demo-2" class="search-form col-sm-4" method="post">
{{ csrf_field() }}
<div class="input-group">
<!-- <input id="search" class="form-control" onkeyup="search_data(this.value, 'result');" placeholder="Search" name="search" value="" type="text"> -->
<input id="search" class="form-control" placeholder="Search" name="search" value="" type="text">
<span class="input-group-btn">
<button class="btn btn-primary" id="search-btn" type="button"><i class="fa fa-search"></i></button>
</span>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#search-btn').click(function(){
$.ajax({
url: '/',
type: "post",
data: {'search':$('input[name=search]').val(), '_token': $('input[name=_token]').val()},
success: function(data){
$('#search-results').html(data);
},
error: function (data) {
console.log('Error on Article extracting');
console.log(data);
}
});
});
});
</script>
</form>
</div>
</div>
</div>
<div class="col-sm-9" id="search-results">
#include ('public_bookmarks')
</div>
</div>
In public_bookmars.blade.php
#if (count($bookmark) > 0)
<div class="row card-row">
#foreach ($bookmark as $bookmark_single)
<div class="col-sm-4 col-xs-12 card-parent" data-col="col-sm-4">
<div class="card">
<div class="card-part1">
<div class="img-card">
<img src="{{$bookmark_single->thumbnail}}" />
</div>
<div class="card-content">
<h4 class="card-title">
{{ $bookmark_single->title }}
</h4>
<div class="card-desc">
{{ str_limit($bookmark_single->description, $limit = 50, $end = ' [...]') }}
</div>
</div>
<div class="card-read-more">
<p><?php $tags = $bookmark_single->tags;
$tag_list = explode(',', $tags); ?>
#foreach ($tag_list as $tag)
{{$tag}}
#endforeach
</p>
<p class="card-user">- {{ $bookmark_single->bookmarker }}</p>
<a class="v-link" target="_blank" href="{{ $bookmark_single->url }}">Visit the link</a>
</div>
<button type="button" class="btn btn-success btn-circle btn-lg btn-read-more"><i class="fa fa-chevron-right"></i></button>
</div>
<div class="card-part2 col-xs-0">
{{ print $bookmark_single->contents }}
</div>
</div>
</div>
#endforeach
</div>
{{ $bookmark->links() }}
#endif
You can use flash() method to achieve this. Old Input
In your search() method add $request->flash(). This will flash the current inputs in the session.
And, next time you want retrieve it by $request->old('search');
Here's the complete search() method:
public function search(Request $request){
$search_value = $_POST['search']; //assign the current value of search field
if(!$search_value) //check if current value is not null, means this is new search or previous one
{
$search_value = $request->old('search'); //If search_value is null use the old value
$request->search = $request->old('search'); //add the old value to current request so that it can be flashed
}
$bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->where('title', 'rlike', $search_value)->orwhere('description', 'rlike', $search_value)->orwhere('contents', 'rlike', $search_value)->orwhere('tags', 'rlike', $search_value)->paginate(10);
$request->flash(); //adding this request's search value to the session
return view('public_bookmarks')->with('bookmark', $bookmarks);
}
Update:
public function search(Request $request){
//$search_value = $_POST['search']; change this to use $request
$search_value = $request->search;
$bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->where('title', 'rlike', $search_value)->orwhere('description', 'rlike', $search_value)->orwhere('contents', 'rlike', $search_value)->orwhere('tags', 'rlike', $search_value)->paginate(10);
return view('public_bookmarks')->with('bookmark', $bookmarks)->withInput($request->only('search')); // flashed input to the view
}
Now, in your view you can get the value with old() method:
{{ $bookmark->appends(['search' => old('search')])->links()}}
Update 2:
change your ajax call to use get method and point it to new address:
type: "get",
url: '/search'
Add a route to handle search request as a get method:
Route::get('/search', 'BookmarkController#search');
In your search() method use $request to get the search value:
$search_value = $request->search;
I have an array of fonts which i displaying in the page using blade templates #foreach like this
#foreach($data as $fonts)
<li class="abc">
<div class="am-actions"><span><i class="icon add"></i></span></div>
Grumpy wizards make toxic brew for the evil Queen and Jack.
</div>
<div class="am-font-details">
<div class="am-font-name">{!! $fonts['font_name'] !!}</div>
<div><?php echo $count = count($fonts['variants']); ?> Styles</div>
</div>
<div class="am-font-options">
<h4>SELECT VARIANTS TO INCLUDE:</h4>
<ul>
#foreach($fonts['variants'] as $variants)
<li><label><input type="checkbox">
{{ $variants }}
</label></li>
#endforeach
</ul>
<h4>LANGUAGE/SCRIPT TO INCLUDE:</h4>
<ul>
#foreach($fonts['subsets'] as $subsets)
<li><label><input type="checkbox">{{ $subsets }} </label></li>
#endforeach
</ul>
</div>
</li>
#endforeach
as you can see i use #foreach for variants and languages inside the main loop the variants and langauges is listed with checkbox so the user can selected their choice and add. on the add the selected variants and languages with the main font array details should be passed through ajax please help me :-(
Since this an ajax question this belongs to Jquery section.
Anyway you can't use ul tag inside li tag. so I made some changes to your html code. I hope this what you need(change php variables appropriately)
/**
* Blade File
**/
#foreach($data as $fonts)
<div class="abc">
<div class="am-actions">
<span><i class="icon add"></i></span>
</div>
Grumpy wizards make toxic brew for the evil Queen and Jack.
</div>
<div class="am-font-details">
<div class="am-font-name">{!! $fonts['font_name'] !!}</div>
<div><?php echo $count = count($fonts['variants']); ?> Styles</div>
</div>
<div class="am-font-options">
<h4>SELECT VARIANTS TO INCLUDE:</h4>
<ul>
#foreach($fonts['variants'] as $variant)
<li><label><input type="checkbox" name="variant" value="{{$variant->id}}">{{ $variant->name }}</label></li>
#endforeach
</ul>
<h4>LANGUAGE/SCRIPT TO INCLUDE:</h4>
<ul id="subsets">
#foreach($fonts['subsets'] as $subsets)
<li><label><input type="checkbox" name"subset">{{ $subsets }} </label></li>
#endforeach
</ul>
</div>
</div>
#endforeach
/**
* Jquery
**/
$(document).on('change', 'input[name=varient], input[name=subset]', function(){
if($('input[name=varient]:checked').length > 0 && $('input[name=subset]:checked').length > 0){
var variants = [];
var subsets = [];
$('input[name=varient]:checked').each(function(index, element){
varients.push($(element).val());
});
$('input[name=subset]:checked').each(function(index, element){
subsets.push($(element).val());
});
$.ajax('<route-to-controller-method>', {
type : 'POST',
data : {
'varients' : variants,
'subsets' : subsets
}
});
}
});
My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
Although its no good Practice Accessing the DB in Blade (better do this in the controller and pass the data) you can do:
<div class="price"><strong>Products</strong>
<span>
{{ BasicModel::where('sub_id', $row->sub_id)->count() }}
</span>
</div>
Its not tested, but have a look at the Eloquent docs, the count() method is explained there.
Update: I am not shure if laravel will find the class BasicModel (I never would access Models directly in blade, as stated do this in the controller and pass the data.) So maybe you need to write it with the full Namespace most likely {{ \App\BasicModel::where() }}.
My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
I think you are closing the PHP tag in the wrong place. Change to this:
<?php print_r($count);
$data = $this->BasicController->count {{ ($row->sub_id) }}
echo $data['count'];//here is the rightplace to close your PHP ?>
</span></div>
All PHP code needs to stay between the <?php and ?>
Looking to your image, apparently, it's printing the $count and then it's printing the plain text , so you have 80$data = ...
If you counted your rows in controller by the code below
$count = DB::table('products')->where('sub_id', $id)->count();
then why wrote so many code in html for counting?
Remove all code form counting form your html and just use this {{ $count }}
My search function is not returning the results I expect.
How can I output the query object to see what is returned by the db query?
public function search()
{
//Save requests to variables
$price = Helper::explodeString(Request::get('price'));
$texts = Helper::explodeString(Request::get('texts'));
$minutes = Helper::explodeString(Request::get('minutes'));
$data = Helper::explodeString(Request::get('data'));
$contract = Request::get('contract');
$phone_option = Request::get('phone-option');
//get all selected carriers
$carriers = array();
foreach(Carrier::all() as $carrier){
if(Request::has($carrier->name)){
$carriers[] = $carrier->id;
}
}
// build query
$query = Plan::whereBetween('price', $price)
->whereBetween('text', $texts)
->whereBetween('minutes', $minutes)
->whereBetween('data', $data)
->whereIn('carrier', $carriers)
->where('contract', $contract)
->where('phone_inlcuded', $phone_option);
return view('planresults')->with('plans', $query);
}
I am then trying to output the object into my view as follows
#foreach($plans as $plan)
<div class="panel price">
<div class="panel-heading arrow_box text-center">
<h3>{{$plan->Carrier->name}}</h3>
<h3>{{$plan->name}}</h3>
</div>
<div class="panel-body text-center">
<p class="lead" style="font-size:40px"><strong>${{$plan->price}} / month</strong></p>
</div>
<ul class="list-group list-group-flush text-center">
<li class="list-group-item">{{$plan->Carrier->name}}</li>
<li class="list-group-item">{{$plan->contract}} Month term contract</li>
#if($plan->phone_included == 0)
<li class="list-group-item">No phone included</li>
#else
<li class="list-group-item">Phone option included</li>
#endif
</ul>
<div class="panel-footer">
<a class="btn btn-lg btn-block btn-success" href="{{$plan->url}}">BUY NOW!</a>
</div>
</div>
<!-- /PRICE ITEM -->
#endforeach
No results are being displayed in my view so I assume my object is empty... I'm very confused and have been stuck on this for ages now
You never execute the query in $query. Read up: http://laravel.com/docs/5.0/queries
You'll need to specify one of the following:
$query->pluck()
$query->get()
$query->first()