Returning values from php file to ajax - ajax

I have written ajax engine to connect to php file. In php file i am accesing data from database. I want to return the array fetched from database back to ajax engine.. How to return the values..

what you can do is join the array with something in your php let's say
$return_string=join('||||', $array);
Then in the javascript split the response you get
var response_array=response.split('||||');

with JSON :)
$json_str = json_encode($arr);
echo $json_str;
I assume you are wanting to use the array in another language such as JavaScript.
Decoding in JavaScript is easy: var arr = JSON.parse(request.responseText);
Hope this helps..

Related

How to reverse Array of Data in Laravel when using Eloquent with Pagination?

I am fetching a list of data using Eloquent method and trying to reverse the order of array, I am getting an error.
Here is the code :(Previous Code)
$temp = Product::paginate(20);
New Code
$temp = Product::paginate(20)->orderBy('id','desc');
Error :
Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.
New method :
$temp = Product::paginate(20)->sortDesc();
Error :
Method Illuminate\Database\Eloquent\Collection::appends does not exist.
Can anyone help me?
use below syntax
Product::orderBy('id','desc')->paginate(20);
try to write it in this way,
use orderBy before the paginate
$temp = Product::orderBy('id','desc')->paginate(20);
->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.
Product::latest()->paginate(20)

How to handle new incoming entries while paging through posts?

Let's assume I have a pagination like this
return App\Post::paginate(1);
After loading this someone creates a new entry in the database for posts. How can i ensure that the "second page" of my pagination does not return the first one again?
Will I always need to send a timestamp with every request to make an additional query before pagination is used?
like
App\Post::where('created_at', '<', $request->input('max_created_at'))->paginate(1);
You can use orderBy. You could do something like that:
// Get the last post (most recent)
return App\Post::orderBy('created_at', 'DESC')->paginate(1);
Or:
// Same as above
return App\Post::orderBy('created_at', 'DESC')->first();
orderBy means all you result are sorted in your query. Hope it will help.
You don't need to pass any timestamp value to verify pagination.
In the controller, in view file you need to pass result of this query.
$result = App\Post::orderBy('created_at', 'desc')->paginate(1);
return view('pagination',compact('result'));
In view file you need below line for pagination work automatically as per Laravel syntax.
{{ $result->links() }}
Visit https://laravel.com/docs/5.2/pagination/ how laravel pagination work.

ALERT SESSION VALUE FROM first page to 2nd page using JQuery,ajax, or javascript

hi guyz this is my first time here. ahmmm i'd like to ask some question regarding session. ahmm
how i cant alert session value from first page to 2nd page using jquery,ajax, or javascript??
please help, im new on that language. ):
You can pass value from one page to other using url query string.
one link to second page add data as parameter like http://your-url.com?data=user-session-value
in the second page call the below javascript function in onload or where you need value
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
you can alert the value like alert(getParameterByName('data'));
In this way your data will show in url. if you don't want to show it then use localstorage. But you need to be in same domain. I mean u can't pass data from one website to other.
In page one just set the data in local storage like localStorage.setItem('data', 'user-session-value');To alert the data in second page call alert(localStorage.getItem('data'));
Hope this can solve your issue... Happy coding...

mysql_fetch_length equivalent function in CodeIgniter ?

I want to count how many characters are generated by a database query.
In PHP I'm using mysql_fetch_length. Is there an equivalent function in CodeIgniter?
"In Codeigniter" is to php as "with jquery" is to javascript, these days. It's just php.
No, CI doesn't have this. Return a row_array() and then:
$row_array = ['test', 'this'];
$new_total = array_sum(array_map(function($value){$total = strlen($value);return $total;}, $row_array));
var_dump($new_total);

how to get a return value from ajax and php

ive set up a php file, called by ajax, to interact with my database. so far i have successfully used json to get the data back from the sql requests. now i am returning my own values from the php file, and trying to get the value back into my javascript. but they are returning as empty. this is how i am doing it:
// in the php file, gets data and database, and returns a number through echo
$result = 2;
echo $return;
//the javascript which calls the ajax and gets the value
var temp = ajaxdb(args);
showError(temp);
show error displays the variable in a div.
i know that my functions are working properly, as they work well when i return a json string, and parse it into an array. but how can i get my custom values from the php?
echo json_encode($return);

Resources