Codeigniter pagination only showing last page selected - codeigniter

My url is: http://[::1]/cashback/index.php/home/brand_offers/3
in which 3 denotes the brand id
My controller:
public function brand_offers($id=null) //getting id of brand from view all offers link in pages
{
$this->load->library('pagination');
$config=['base_url'=> base_url('index.php/home/brand_offers'.$id),'per_page'=>2,'total_rows'=>$this->home_offers->total_rows($id)];
$this->pagination->initialize($config);
$brand_offers=$this->home_offers->brand_offers($id,$config['per_page'],$this->uri->segment(4));
$brand_desc=$this->home_offers->brand_desc($id);
$this->load->view('brand_offers',['offers'=>$brand_offers,'desc'=>$brand_desc]);
}
My Model:
public function brand_offers($id,$limit,$offset)// get offers according to brand id
{
$this->db->trans_start();
$this->db->select('offers.id as oid,offers.offer_type,offers.discount,offers.coupon_code,offers.offer_title,offers.offer_url,offers.offer_desc,brands.brand_logo,brands.bid,brands.brand_desc,brands.brand_banner,brands.brand_url')
->from('offers')
->join('brands','offers.brand_id=brands.bid')
->where('offers.status','0')
->where('brands.brand_status','0')
->where('offers.brand_id',$id)
->order_by('offers.id','DESC')
->limit($limit,$offset);
$q=$this->db->get();
$this->db->trans_complete();
return $q->result();
}
And in view i am printing the pagination using
$this->pagination->create_links()
I am getting the links but the last link is selected automatically and if i select the previous link it still show last page selected and last page data

Related

retrieve data by clicking next and previous in codeigniter

I have created a page using following link 1 in codeigniter.I have displayed the abstract in one line and author in second line using echo get_abstract($row->id=1).For second record i have to change id=2 and so on. Is there any way to create next and previous button in view so that when i click on next id would automatically get incremented and display record. Similarly for previous also.Right now I am using following view.php
<?php
if($fetch_data->num_rows() > 0)
{
$row = $fetch_data->row();
echo get_Author($row->DocumentID=1);
}?>
<?php
if($fetch_data->num_rows() > 0)
{
$row = $fetch_data->row();
echo get_Abstract($row->DocumentID=1);
}?>
You can see that i am using fixed id (DocumentID) to display records of 1st row of mysql.I want to change it dynamically by clicking next, previous in view rather than manually assigning.
The model and controller are defined in above link.I will be grateful to you for your help.
I have solved this problem by adding following model class and adding pagination in controller.
function fetch_data($limit, $offset=0)
{
$this->db->limit($limit,$offset);
$query = $this->db->get('prlaps');
return $query;

Laravel 5.6 Like Button

I'm using this package here in Laravel 5.6 to add likes system in my project.
I have updated the models as per their documentation. However, I'm confused on how to use this package.
I have added tried the following which adds the logged in user to the particular article likes list when he visits the link.
public function show(ArticleCategory $articlecategory, $slug)
{
$categories = ArticleCategory::all();
$article = Article::where('slug', $slug)->first();
$user = User::first();
$user->addFavorite($article);
return view('articles.show', compact('article', 'categories'));
}
And in my user dashboard, I'm able to pull up all the articles which are liked by the user with
$user = Auth::user();
$favoritearticles = $user->favorite(Article::class);
But I'm looking for a functionality where I have a button on the article page where when a logged user clicks on it, he is added to the likes list. I haven't tried this before so stuck at this point.
I replaced
$user->addFavorite($article);
with
$user->toggleFavorite($article);
but that just toggles the favourite list. I mean when I visit the link once, the logged in user is added to the likes list. When I visit the link for the second time, the logged in user is removed from the likes list. The cycle is repeated.
Could anyone explain to me how to achieve the like functionality with a button?
you're almost there,
You have to add a button and on click you will trigger an AJAX request to the server to perform what you want without refreshing the page, here is an example:
First you'll add a button and give it an ID or class:
<button class="like">Like</button>
Then the moment you click on it, you'll call the url which you need to replace with the route to your function,
Then you have to declare a method like so:
public function like($slug)
{
$article = Article::where('slug', $slug)->first();
$user = \Auth::user(); //to get authenticated user...
$user->toggleFavorite($article); // toggle so if u already like it u remove it from the liked table
return response()->json(['status': 1])
}
And of course add the route to your routes.php:
Router::get('like/{slug}',"ArticleController#like");
then add the function (jQuery is used here) to hook the AJAX call
$('.like').on('click', function(){
$.ajax({
type: "GET",
url: 'wwww.example.com/articles/slug',
data: {slug: 'the slug'},
success: function(data){
alert('its done')
},
});
})
Create a form in you article page with a button
<form action="{{url('favorite/{$post->id}')}}" method="post">
#if($post->isFavorited())
<button type="submit">Remove from favorite</button>
#else
<button type="submit">Add to favorite</button>
#endif
</form>
create the favorite route and controller
Router::post('favorite/{id}',"ArticleController#toggleFavorite");
public function toggleFavorite($id) {
$article = ArticleCategory::find($id);//get the article based on the id
Auth::user()->toggleFavorite($article);//add/remove the user from the favorite list
return Redirect::to('article/{$id}');//redirect back (optionally with a message)
}

send a mail to particular user while clicking on the button from view page in codeigniter

i have one view page called vendor view candidates page..in that page i display all the candidates details from database and i have user_id in that view page..and i gave button called release information..so when user click on the button the page will redirect to edit_candidates page with that particular candidate_id..and they will allow to insert email and phone number..
After they insert email and mobile number and then submit..i want to send mail to particular user_id..
This is my button:(edit candidates page)
<td>Release Contact Info</td>
Controller:
public function edit_candidates($id)
{
$data['editdata']=$this->CandidateModel->candidate($id);
if($this->input->post())
{
$this->CandidateModel->update_candidate($this->input->post(),$id);
redirect(base_url('index.php/Candidate/vendor_view_candidates'));
}
$this->load->view('Candidates/edit_candidate',$data);
}
Can anyone help me...i tried a lot..but i didn't get any idea on how to do this..
Thanks in advance..
Use the following code to fix your issue
public function edit_candidates($id)
{
$data['editdata']=$this->CandidateModel->candidate($id);
if($this->input->post('update_form'))
{
$userEmail = $this->CandidateModel->get_userinfo($data['editdata']->user_id);
//Pass this $userEmail into mail function it will send email
$this->CandidateModel->update_candidate($this->input->post(),$id);
redirect('index.php/Candidate/vendor_view_candidates','refresh');
}
else
{
$this->load->view('Candidates/edit_candidate',$data);
}
}
Let me know if it not fix your issue

How to solve the problems with empty return of model in view

I have a form to update an article.in this form I have tow select boxes one for section and another for sub section.An article should have a section but sub section is not necessary.In my update form if a section has subsections it should bring it.My problem is that in my update form if a section does not have any subsection it does not show the continuation of my form because in model it returns false.I tried to return null or an empty array but it could not solve my problem.
Model:
function get_subsection($sec_id,$subsec_name){
$this->db->selec("*");
$this->db->where('sec_id',$sec_id);
$this->db->where('subsec_name !=',$subsec_name);
$query=$this->db->get('sub_section');
if($query->num_rows() > 0)
{
return $query;
}
else
return false;
}
if this function returns false in update form it does not show the continuation of form and the edit submit button.I removed the else condition but it can not solve the problem.
Controller
function edit($id){
$data['rec']=$this->amodel->edit_art($id);//get the record to update
foreach($data['rec'] as $a)
{
$sections['items']=$this->amodel->section('$a->sec_id');//select all sections without that which selected by user
$subsetion['sub']=$this->amodel->subsection($a->sec_id,$a->subsec_name);//select all subsections of a section which selected by user
}
$this->load->view('edit_art',array_merge($data,$sections,$subsection));
}
the problem is with $subsetion which can has no record in database.
please guide me what should I do to show all form element even the subsection be empty.
If i understand ur problem then you can try this
Model:
function get_subsection($sec_id,$subsec_name){
$this->db->selec("*");
$this->db->where('sec_id',$sec_id);
$this->db->where('subsec_name !=',$subsec_name);
$query=$this->db->get('sub_section');
return $query->result();
Controller:
function edit($id){
$data['rec']=$this->amodel->edit_art($id);//get the record to update
foreach($data['rec'] as $a)
{
$data['items']=$this->amodel->section('$a->sec_id');//select all sections without that which selected by user
$data['sub']=$this->amodel->subsection($a->sec_id,$a->subsec_name);//select all subsections of a section which selected by user
}
$this->load->view('edit_art',$data);
}
View :
<?php echo form_dropdown('items',$items,"",'class=""'); ?>
<?php echo form_dropdown('sub',$sub,"",'class=""'); ?>
Let me know is it ok or not. if not ok then post your view code.
Try the following:
function edit($id){
$data['rec']=$this->amodel->edit_art($id);//get the record to update
foreach($data['rec'] as $a){
if($this->amodel->count_sections($a->sec_id) > 0)
$sections['items']=$this->amodel->section('$a->sec_id');//select all sections without that which selected by user
else
$sections['items'] = NULL;
if($this->amodel->count_subsections($a->sec_id,$a->subsec_name) > 0)
$subsetion['sub']=$this->amodel->subsection($a->sec_id,$a->subsec_name);//select all subsections of a section which selected by user
else
$subsetion['sub']= NULL;
}
$this->load->view('edit_art',array_merge($data,$sections,$subsection));
}
Count the number of available sections and subsections and try to read the records if they are avalable!
Write the required functions (count_sections and count_subsections) in the model!

{Magento} Product page and Add To Cart

I am building a static page that contains several products. I took the static HTML that was generated by one of my product pages and added all the other products to this page. Each product has a radio button and the customer can only select one of them. The qty will always be 1.
How do I submit the product_addtocart_form?
I modified the form submit function like this:
var productAddToCartForm = new VarienForm("product_addtocart_form");
productAddToCartForm.submit = function(){
if(this.validator.validate()) {
var product_id = jQuery("input[name='product']:checked").val();
this.form.action = "/store/checkout/cart/add/product/"+product_id+"/qty/1";
this.form.submit();
}
}.bind(productAddToCartForm);
But it doesn't always work. If I modify the action to this, which is the same as my product page but changing the product_id:
this.form.action = "/store/checkout/cart/add/uenc/aHR0cDovL3N0YWdpbmcuY2ljLnNjaWMuY29tL3N0b3JlL3B1YmxpY2F0aW9ucy8yNS1tb3N0LWlubm92YXRpdmUtYWdlbnRzLWluLWFtZXJpY2EuaHRtbD9fX19TSUQ9VQ,,/"+product_id+"/qty/1";
It also works inconsistenty.
How do I do this??????
Just post the form as an action="get" to /checkout/cart/add
Name your radio field product and another hidden field named qty. Pass the Magento product ID to product and the quantity to qty

Resources