laravel pagination on post - php

hi :) im parsing my database in a table with pagination !!
this is my controller
$theme = Theme::all();
$questions = questionList::paginate(10);
$the = "";
return View::make('home.home')
->with('user',Auth::user())
->with('theme', $theme)
->with('the' , $the)
->with('questions',$questions);
and my view i have {{ $questions->links(); }} under my table and it's working fine !!
the thing is that i have a list of themes to sort data in the table so when i click on divertisement i get divertisement data.
the problem is that when i paginate it return to the get request and give me all the data !! what is the problem thx :)

To add a filter/sort you also need to add that in a where clause in your query and also you need to append the query string in your pagination links. For example:
public function showPosts()
{
$questions = app('questionList');
if($filter = Input::get('filter')) {
$questions = $questions->where('theme', $filter);
}
$questions = $questions->paginate(10);
if(isset($filter)) {
$questions->appends('filter', $filter);
}
return View::make(...)->with(...);
}
In your view you need to create links to this method (Probably using a route name or url) with filter query string. So, for example:
Divertisement
SomethingElse

Related

Laravel edit pagination next and previous url for filtering

I am trying to create infinite scroll in laravel for that I am using default pagination and it is working fine but I want a pagination to use filtering.
public function infinite_scroll(Request $request)
{
$key = $request->input('key');
$group_name = $request->input('groupname');
$wachat = Wechat::where('key', '=', $key)->where('groupName', '=', $group_name)->orderBy('id', 'DESC')->paginate(2);
$this->response['values'] = $wachat;
$this->response['key'] = $key;
return response()->json(
$this->response
);
}
I am using this code and it is giving me this url in next url:
next_page_url: "http://localhost:8888/kc/kyo-webservice/public/api/v1/wechatinfinite?page=2"
But I want a filtering based on key and groupname for example when I pass a param groupname and key it should give me values.
When I am trying to get next page url it is not working I want my result for pagination based on my filter it should give me next page url like this:
next_page_url: "http://localhost:8888/kc/kyo-webservice/public/api/v1/wechatinfinite??key=smg1np1f77&groupname=group&page=2"
And it should give me result based on my filters.
used appends() pagination method here
Appending To Pagination Links
You may append to the query string of pagination links using the
appends method. For example, to append sort=votes to each pagination
link, you should make the following call to appends:
$wachat->appends(['key'=> $key,'groupname' => $group_name]);
in your controller do like that
public function infinite_scroll(Request $request)
{
$key = $request->input('key');
$group_name = $request->input('groupname');
$wachat = Wechat::where('key', '=', $key)->where('groupName', '=', $group_name)->orderBy('id', 'DESC')->paginate(2);
$wachat->appends(['key'=> $key,'groupname' => $group_name]);
$this->response['values'] = $wachat;
$this->response['key'] = $key;
return response()->json(
$this->response
);
}

Only last row getting displayed in view in Codeigniter

I'm trying to fetch certain values from and then pass it to another model in the same control.
However I'm only able to display the last row in the view.
I have shared my code below and I'm not sure where I'm going wrong.
Controller:
public function test($id){
$mapping_details = $this->queue_model->get_mapping_details($id);
foreach ($mapping_details as $value) {
$data['agent_details'] = array($this->agent_model->get_agent_details($value['user_id']));
}
$this->load->view('app/admin_console/agent_queue_mapping_view', $data);
}
Model:
public function get_agent_details($id) {
$query = "select * from user_table where id = ".$id." and company_id = ".$this->session->userdata('user_comp_id');
$res = $this->db->query($query);
return $res->result_array();
}
Welcome to StackOverflow. The problem is the iteration in your controller. You are iterating through the $mapping_details results and per every iteration you are re-assigning the value to $data['agent_details'] , thus losing the last stored information. What you need to do is push to an array, like this:
foreach ($mapping_details as $value) {
$data['agent_details'][] = $this->agent_model->get_agent_details($value['user_id']);
}
However, wouldn't it be best if you created a query that uses JOIN to get the related information from the database? This will be a more efficient way of creating your query, and will stop you from iterating and calling that get_agent_details() over and over again. Think of speed. To do this, you would create a model method that looks something like this (this is just an example):
public function get_mapping_details_with_users($id){
$this->db->select('*');
$this->db->from('mapping_details_table as m');
$this->db->join('user_table as u', 'u.id=m.user_id');
$this->db->where('m.id', $id);
$this->db->where('u.company_id', $this->session->userdata('user_comp_id'));
return $this->db->get()->result();
}
Then your controller will only need to get that model result and send it to the view:
public function test($id){
$data['details_w_users'] = $this->queue_model->get_mapping_details_with_users($id);
$this->load->view('app/admin_console/agent_queue_mapping_view', $data);
}
Hope this helps. :)

Codeigniter - selecting a database row based on one field

I'm building a codeigniter site and I have a database table of books - one record for each book with title, author, etc etc fields. I know how to get the db table contents and pass an object to the view and then do a foreach loop to get the values. I could print a table of all the data. However I then get a bit muddled. The page has areas for each book and the data for a book will be one row of the data array. So what I want to do is, if the author is 'this' value, find the correct row in which the author appears and then get the other fields. How can I do a foreach loop that finds the one row with the author's name?
I can't think how to do it - I seem to go round and round in circles.
Help!
Edit: Code:
OK so here's the controller method:
public function index()
{
$books = $this->Books_model->getbooks();
$data = array(
'body_id'=>'home',
'main'=>'home_view',
'books'=>$books
);
$this->load->view('templates/template_main_view', $data);
}
and here's the model method:
function getbooks(){
$query = $this->db->get('books');
return $query->result();
}
so I end up with the variable $books (which is of course an object, not an array) in the view and a var_dump() shows that it has all the data. So far so good. I can then use a foreach loop to get values.
Now I want to extract a single row/record conditional on the fact that it has a given value for 'author' and assign each field of that row to a variable that I can use them in the view. And then I want to repeat that for the other rows. I can't seem to work out how to do that.
Afternote:
I found a way of doing this but not sure if it's the best or neatest:
I do this:
if(isset($books)){
foreach($books as $row){
if($row->author == 'authorname'){
$title = $row->title;
}
}
}
it works but seems a bit clumsy/overkill??
How about creating a specific function for getting the book/s that matches the author name?
For example:
In your controller you can do something like this:
public function index()
{
$data['books'] = $this->Books_model->get_each_book();
$data = array(
'body_id'=>'home',
'main'=>'home_view',
'books'=>$books
);
$this->load->view('templates/template_main_view', $data);
}
The in your model, do something like this:
function get_each_book(){
$this->db->where('author','authorname');
$query = $this->db->get('books');
return $query->result_array();
}
This way, you are gonna filter the results in the model already.
Then in your view, access the variable 'books' through a foreach loop:
Example:
foreach($books as $row)
{
$title = $row['title'];
echo $title;
}

Laravel - pagination links not appearing

For some reason {{ $items->links() }} doesn't do anything.
Direct cause of this problem is that $items->getLastPage() returns 0. Same $items->getTotal().
I can change page parameter in the url and it works fine - it goes to correct page.
however $items->getCurrentPage() returns 1 on each page.
Pagination works perfectly for me with other models just this one gives me problems.
Also it seems to be working fine when I create the paginator manually but I want to be able access some methods from the model so I want to use Eloquent models and not raw array.
Edited:
Code:
$records = EventLog::byObject($model, $id)->paginate(10);
static public function byObject($model, $id)
{
$records = DB::select([query], [params]);
return self::getHistory($records, $model);
}
static public function getHistory($records, $model = '')
{
$ids = array();
foreach ($records as $record) {
array_push($ids, (int)$record->id);
}
$history = array();
if (count($ids)) {
$history = EventLog::whereRaw('id IN (' . implode(',', array_fill(0, count($ids), '?')) . ')', $ids)
->with('creator')
->with(array('eventfields' => function($query)
{
$query->whereRaw('field NOT LIKE \'%_id\'');
}))
->orderBy('event_logs.created_at')
->orderByRaw('(CASE WHEN eventable_type=? THEN 0 ELSE 1 END)', array($model))
->orderByRaw('(CASE WHEN parent_type=? THEN 0 ELSE 1 END)', array($model));
}
return $history;
}
The returned $history has the right type, is displayed correctly but the pagination is not working for some reason.
I am trying to display links using
{{ $records->links(); }}
Thanks
I've same problem here, laravel 4.1 and a query scope with fulltext search. In my case the problem was the orderByRaw (a normal orderby doesn't broke pagination links).
So my "poor" solution is to keep orderByRaw off when I need a pagination and use:
->orderBy(DB::raw('my raw orderby'));
that works

CodeIgniter getting data from database

In my CodeIgniter project I'm getting the list of projects and successfully output them on a page. However, the data in one of the columns on that page should be retrieved from a different table in DB using the project ID. Could anybody help me to figure out how that can be done? Basically I need to make another query to that other table specifying the project id but don't actually know how to do that with CodeIgniter.
UPDATE
In the model I'm getting the list of projects with the following function:
function get_projects_list($page, $limit){
$sql = sprintf("SELECT * FROM Project WHERE deleted != 1 LIMIT %d, %d", ($page-1)*$limit, $limit);
$query = $this->db->query($sql);
return $query->result();
}
And in the controller I call the following function:
$projects_list = $this->Project_management_model->get_projects_list($curPage, self::$LIMIT_PER_PAGE);
$data['projects_list'] = $projects_list;
$data['cur_page'] = $curPage;
$data['page_count'] = $pageCount;
$this->load->view('project_management_view', $data);
And in the view I simply run on the $data with foreach and list the results in a table. In that table there's a column where I need to show a result from another table based on the ID of the project of that very row.
Thanks for helping.
You didn't mention whether you are using ActiveRecord or not. I am assuming that you are. I'll also guess that maybe what you need to do is use a JOIN.
If you were using straight SQL, you would do this using some SQL that might look something like this:
SELECT a.appointment_time, u.user_real_name FROM appointment a, site_user u WHERE u.site_user_id = a.user_id;
That would pull the user's name from the user table based on the user id in the appointment table and put it with the appointment time in the query results.
Using ActiveRecord, you would do something like this:
$this->db->select('appointment_time,user_real_name')->from('appointment')->join('site_user', 'site_user_id=appointment_user_id');
But why don't you tell us a little bit more about your question. Specifically, do you want this column in the other table to be related to the rows from the first table? If so, my JOIN suggestion is what you need.
I've actually found a way to do that with a custom helper. Creating a new helper and loading it in the controller gives an option to use the function from that helper in the view.
Thanks.
public function get data()
{
$this->db->flush_cache();
$query = $this->db->get('project_table');
$result = $query->result();
$data = array();
for ($i = 0;$i < count($result);$i++)
{
$data[$i] = $result[$i];
$data[$i]['project_data'] = temp($result[$i]->id);
}
return data;
}
private function temp($id = 0)
{
$this->db->flush_cache();
$this->where('id',$id);
$query = $this->db->get('project_table2');
$result = $query->result();
if (count($result) != 0)
return $result[0]->data;
}
you can do it by some thing like that,or you can use sub-query by query function of database.

Categories