How to open specific page of paginate? - php

I get comments paginated. And I need to check, if I have the id of a comment, then open the page that comment is in. Here is my code:
public function show_comments(Request $request) {
$cmnt_id = $request->input('cmnt_id');
if ( isset( $cmnt_id ) ){
// I need to get all comments paginated but start from the page that $cmnt_id is in it
} else {
// starts from page 1
$comments = Cmnt_tb::paginate(10);
}
return View('show_cmnts', compact('comments'));
}
How can I do that? any idea?
I think I have to make a new URL and redirect to it.

Maybe there is a better solution, but I guess something like this will work for you:
public function show_comments(Request $request) {
$cmnt_id = $request->input('cmnt_id');
$perPage = 10;
if (isset($cmnt_id)){
// Get row number.
$rowNumber = Cmnt_tb::where('id', '<=', $cmnt_id)->count();
// Get page.
$page = ceil($rowNumber / $perPage);
return redirect('show-comments-url?page='.$page);
} else {
$comments = Cmnt_tb::paginate($perPage);
return view('show_cmnts', compact('comments'));
}
}

Related

wc_get_order does not allow to use variable

Hei everybody. Maybe someone could give me some tips , because I am stuck on this for second day.
function get_url_id() {
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_NUMBER_INT);
$data = $_GET['id'];
$final= xss_clean($data); // function to prevent xss, working
return $final;
}
function relax() {
;
}
function show_order_details_created_date() {
if( $user = wp_get_current_user() ){
$order_id= get_url_id();
if(!isset($order_id)) { relax(); } else {
$order = wc_get_order ($order_id);
$date=$order->get_date_created();
$date_mod=explode("T" , $date);
return $date_mod[0];
}
}
}
Wordpress is giving me critical error , that I can not even open to edit page section. If instead of $order_id I insert existing order number ITS WORKS ABSOLUTELY fine. If I print out variable it also shows ID without problem. Why I can not use variable here? Any hints, guys?

How can I do a self-join in Laravel?

Here is my code:
$role_id = Auth::user()->role_id;
$related = Page::where('path', $request->path())->where('method', $_SERVER["REQUEST_METHOD"])->first()->related;
$pages = Page::where('related', $related)->get();
foreach ($pages as $page){
$accessGiven = page_role::where('role_id', $role_id)->where('page_id', $page->id)->first();
if ( sizeof($accessGiven) > 0 ) {
return $next($request);
}
}
return redirect('/');
It works well logically, but it is a little slow for huge dataset. You know, it's actually a middleware and will be executed before most of requests.
Anyway, I guess I can combine line 2 and line 3 and make one query of them. Any idea how can I do that?
Try this:
In your Page model:
public function relatedPages(){
return $this->hasMany(self::class, 'related', 'related');
}
then
$pages = Page::where('path', $request->path())->where('method', $_SERVER["REQUEST_METHOD"])->first()->relatedPages
Look in this example (I didn't test it):
$requestPath = $request->path();
$serverMethod = $_SERVER["REQUEST_METHOD"];
$pages = Page::where('related', function($query) use($requestPath,$serverMethod) {
$query->where('path', $requestPath )->where('method',
$serverMethod)->first()->related; }
)->get()

If statement inside elseif statement causing error

Okay, so I used to have this code and it worked fine:
$lastpost = ForumPos::where('user_id', '=', Auth::id())->orderby('created_at', 'desc')->first();
if ($validator->fails())
{
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors($validator->messages());
}
elseif ($lastpost->created_at->diffInSeconds() < 15)
{
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors('You really need to slow down with your posting ;)');
}
else
{
$new_thread = new ForumThr;
$new_thread->topic = $id;
$new_thread->user_id = Auth::id();
$new_thread->title = Input::get('title');
$new_thread->save();
$new_post = new ForumPos;
$new_post->thread = $new_thread->id;
$new_post->user_id = Auth::id();
$new_post->body = Input::get('body');
$new_post->save();
return Redirect::to('/forum/thread/'.$new_thread->id.'');
}
and this worked fine, until I noticed a little problem so I had to change this a bit to get this:
$hasposted = ForumPos::where('user_id', '=', Auth::id())->count();
if ($validator->fails()){
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors($validator->messages());
} elseif ($hasposted != 0) {
$last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first();
if ($last_post->created_at->diffInSeconds() < 15) {
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors('You really need to slow down with your posting ;)');
}
} else {
$new_thread = new ForumThr;
$new_thread->topic = $id;
$new_thread->user_id = Auth::id();
$new_thread->title = Input::get('title');
$new_thread->save();
$new_post = new ForumPos;
$new_post->thread = $new_thread->id;
$new_post->user_id = Auth::id();
$new_post->body = Input::get('body');
$new_post->save();
return Redirect::to('/forum/thread/'.$new_thread->id.'');
}
Now when I post a thread and get to the if statement inside the elseif statement, I hit a roadblock. I get the following error:
I only get this error when I haven't specified the title variable in the controller so the view gets it, however there shouldn't be a view. Any ideas? :S
Take a look at your elseif block (second condition)
if(...)
{
//first condition
return ...;
}
elseif ($hasposted != 0) {
{
//second condition
$last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first();
if ($last_post->created_at->diffInSeconds() < 15) {
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors('You really need to slow down with your posting ;)');
}
}
else
{
//third condition
return ...;
}
When your nested if statement fails
$last_post->created_at->diffInSeconds() < 15
this block finishes, and the rest of the conditional finishes without issuing a Redirect. That is, your nested if statement knows nothing about the third conditional. PHP/Laravel are doing what you told it to -- so tell it to do something else.
This is purely a style suggestion, but I've reached a point where I avoid multiple branch conditionals whenever possible, especially when returning from inside a branch. A style more like
if(...)
{
return Redirect(); //...
}
if(...)
{
return Redirect(); //...
}
if(...)
{
return Redirect(); //...
}
if(...)
{
return Redirect(); //...
}
might look longer on the page, but it's much clearer what's going on.
If this? Do something and go away (`return`)
Still here? Well if this-other-thing then do something and go away (`return`)
**Still** here? Well if this-other-thing then do something and go away (`return`)
You end up thinking in a series of yes/no tests, and avoid the very human/programmer problem you ran into with nested conditional logic.
In all your other conditions you do a redirect. If the elseif succeeds, but the if does not succeed then you do nothing. It is then trying to render a page using your master template but you have not set any of the variables that it needs. You could fix this by adding another redirect:
if ($last_post->created_at->diffInSeconds() < 15) {
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors('You really need to slow down with your posting ;)');
}
else
{
return Redirect::to('/somewhere/else/');
}
After discussing this in the Laravel IRC room, we found the solution (and I believe answers here would have sufficed too)
In the end, I came up with this:
$hasposted = ForumPos::where('user_id', '=', Auth::id())->count();
if ($validator->fails()){
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors($validator->messages());
} elseif ($hasposted != 0) {
$last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first();
if ($last_post->created_at->diffInSeconds() < 15) {
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors('You really need to slow down with your posting ;)');
}
}
$new_thread = new ForumThr;
$new_thread->topic = $id;
$new_thread->user_id = Auth::id();
$new_thread->title = Input::get('title');
$new_thread->save();
$new_post = new ForumPos;
$new_post->thread = $new_thread->id;
$new_post->user_id = Auth::id();
$new_post->body = Input::get('body');
$new_post->save();
return Redirect::to('/forum/thread/'.$new_thread->id.'');
If it passes all the if statements, it'll get through to the final request and now I'm happy to say it all works as planned. Thanks, lads!

Limit pagination Laravel 4 when page not exist

i have this code:
public function test(){
$results = Cand::paginate(4);
if (Request::ajax()) {
return Response::json(View::make('admin.posts')->with('results', $results)->render());
}
$this->layout->content = View::make('admin.dashboard')->with('results', $results);
}
It works fine, but when a user enters a page that does not exist in the URL, like "?page=9999", how can I restrict this?
You can get the last page by calling getLastPage() on your paginator instance:
if ( Input::get('page', 1) > $results->getLastPage() )
{
App::abort(404);
}

Delete Images -> codeigniter

I have reworded my question to help gain my specific answer, sorry if my last question was confusing:
I have an issue with trying to delete a row from the database but also incorporate how to unlink the to images from different locations. I cannot get the row to delete or the images to unlink.
Here is my -> db structure
My image locations are:
fullpath = /includes/uploads/gallery/
thumbpath = /includes/uploads/gallery/thumbs/
Ideal Situation:
What my ideal situation would be is when the delete link is clicked that it will run the function within the index function and then reload the same page with a success message -> I have not successfully been able to get them to work.
My code:
Model:
function deleteImage($id,$path){
$this->db->delete('images', array('id' => $id));
if($this->db->affected_rows() >= 1) {
if(unlink($path)
return TRUE;
} else{
return FALSE;
}
}
View:
<?php if(is_array($get_images)): ?>
<?php foreach($get_images as $image): ?>
<img src ="<?=base_url()?>includes/uploads/gallery/thumbs/<?=$image->thumbpath?>" alt="<?= $image->description?>"> Delete
<?php endforeach; ?>
<?php endif; ?>
Controller:
function index() {
if(!$this->session->userdata('logged_in')) {
redirect('admin/home');
}
// Main Page Data
$page['get_images'] = $this->image_model->getImages();
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Delete Gallery Image';
$data['content'] = $this->load->view('admin/deleteimage',$page,TRUE);
$this->load->view('admintemplate', $data);
}
function delete_data($record_id)
{
$query = $this->db->get_where('projukti_committee',array('record_id' => $record_id));
if( $query->num_rows() > 0 )
{
$row = $query->row();
$picture = $row->picture;
unlink(realpath('assets/photo/'.$picture));
$this->db->delete('projukti_committee', array('record_id' => $record_id));
return true;
}
return false;
}
What you're looking for if you're using active record is:
$this->db->delete();
Now from memory this will only return false if an error has occurred, so will constantly return true. But it's ok we can use another function to check whether the row was deleted.
Oh and you will also want to pass into the function the path of the file you want deleting.
function deleteImage($id,$path) {
$this->db->delete('table', array('id' => $id));
if($this->db->affected_rows() >= 1){
if(unlink($path))
return TRUE;
} else {
return FALSE;
}
}
Presumably you have an image ID of some sort.
Append the ID to the deleteimage/delete URI.
Create a method that deletes the ID from your table, and use PHP's unlink($path) function to actually delete the image.

Categories