Display foreach array item one by one in ajax - php

In PHP I am getting an array from foreach. I am JSON encoding and showing all the results at once. This works fine. But what if I need to display array items one by one as foreach still continues?
<?php
$array = //somearray;
$data_arr = array();
foreach($array as $arr){
//do something
$data_arr[] = $arr;
}
echo json_encode(array('success'=>true,'data'=>$data_arr));
//here i can display the data in my jquery using each function
//i can display the whole data at once after the foreach is completed
//what i need is i want to display first element of data_arr after its loop is completed and then the second element and so on
?>

Did you try http://www.php.net/array_shift ? its php function you need to call this function after foreach
<?php
foreach($array as $arr){
//do something
$data_arr[] = $arr;
}
print_r array_shift($data_arr);
//Print First array value from this $data_arr list.
?>
Thanks.

What you looking for is Streaming Response.
Checkout following link to learn how to do that.
https://www.sitepoint.com/php-streaming-output-buffering-explained/

Related

How to put foreach in array

I have this
$homedata = $user->getbanks();
foreach ($homedata as $data)
echo json_encode(array("replies" => array(array("message" => $data->bankname))));
I have this put the result am getting is only one array from the list. I want to get all.
If I understand your requirement correctly, you need the banknames in an array replies within a key message. For that the below code will do the job. But you can optimize the query for better performance.
$homedata = $user->getbanks();
$banks['replies'] = [];
foreach ($homedata as $data) {
$banks['replies'][]['message'] = $data->bankname;
}
print_r(json_encode($banks)); exit;
Further, you were only getting the first content because you are overriding the previous content when you iterate using foreach.

merge array inside foreach loop

I want to merge array result inside foreach loop. Is there any solution about this or other best way. Thanks in advance.
PHP code
include('Config.php');
$data = json_decode(file_get_contents("php://input"));
foreach($data->dataVal as $key => $value){
echo json_encode($config->query($key,$value));}
//this is the response code above --> how to merge this 2,3 array result into one only
[{"name":"Roi"}][{"name":"Tom"}][{"name":"Chad"}]
//result to be expected like this
[{"name":"Roi","name":"Tom","name":"Chad"}] // this is i want
The nearest you will get is by building up an array of the data in the foreach loop and JSON encoding the result...
$result = array();
foreach($data->dataVal as $key => $value){
$result[] = $config->query($key,$value);
}
echo json_encode($result);
This stops having repeated values for the name.
In your original attempt, you were encoding each row separately, so the JSON was invalid.

How to get properties through loop from a REST JSON response using Httpfull in PHP and how to show in grid

As in the URL, I don't want to give a limit, I want to show in loop
[0]. I don't want to give [0] or [1] etc. I want to do it in a loop.
http://buildout.com/api/v1/d7d8b6a8cbd23c1f69e3a327b32c8217d6bea635/properties.json?limit=2
<?php
$url='my url';
$res = \Httpful\Request::get($url)->expectsJson()->send();
$data = json_decode($res,true);
$name1 = $data['properties'][0]['broker_id']; // I WANT LOOP HERE INSTED OF [0]
echo $city1;
?>
You can loop through the response data dynamically to get each datum without specifying the index as follows:
foreach ($data['properties'] as $key=>$val) {
var_dump($key);
}
The $key can be:
First $data['properties'][0];
Next $data['properties'][1];
and so on...

Stripslashes() on array

I have some problems using stripslashes() on array.
Here is my array :
$tabRegion = array(
1=>"Alsace",
2=>"Aquitaine",
3=>"Auvergne",
4=>"Basse-Normandie",
5=>"Bourgogne",
6=>"Bretagne",
7=>"Centre",
8=>"Champagne-Ardenne",
9=>"Corse",
10=>"Franche-Comté",
(...)
21=>"Provence-Alpes-Côte d'Azur",
22=>"Rhône-Alpes",);
In order to stripslash, I have adapted this PHP code :
foreach ($tabRegion as $key=>$region) {
$tabRegion[$key] = stripslashes($region);
}
After in the file, I generate URL with it for example :
if (file_exists('../region/$tabRegion[$region]/$fonction/messages/$lecturefichier (...)
But the fact is that the last value of the array is always selected ("Rhône-Alpes") by the code... I don't know why.
Do you have an idea? :)
Thank you !
You are using foreach loop then you have to generate url in that loop.
In that loop you will get each region value
$tabRegion = array(
1=>"Alsace",
2=>"Aquitaine",
3=>"Auvergne",
4=>"Basse-Normandie",
5=>"Bourgogne",
6=>"Bretagne",
7=>"Centre",
8=>"Champagne-Ardenne",
9=>"Corse");
foreach ($tabRegion as $key=>$region)
{
$tabRegion[$key] = stripslashes($region);
print "<br>".$region;
}
Output will be :
Alsace
Aquitaine
Auvergne
Basse-Normandie
Bourgogne
Bretagne
Centre
Champagne-Ardenne
Corse
So that,you have to insert following line in that for loop :
if (file_exists('../region/$tabRegion[$region]/$fonction/messages/$lecturefichier (...)
You are using $region variable in foreach loop and you should know that it is treated like any other variable in your script. So for example:
$fruit = 'Banana';
foreach(array('Tomato', 'Orange') as $fruit) {
echo $fruit;
}
echo $fruit; // it will output 'Orange';

Display most voted posts

I'm using ThumbsUp V2 Voting Script and i'm trying to display the posts based on the number of votes:
<?php $items = ThumbsUp::items()->orderby('votes_total')->get() ?>
The values are stored in mysql so i thought of:
$display = mysql_query("SELECT * FROM banat");
$items = ThumbsUp::items($display)->orderby('votes_total')->get() ;
But i thik i've definitely done this wrong since it's just displaying an output:
Array
If you get an array, you must loop on it to show the data. You cannot just do an echo on a var that is an array. You should have something like this:
foreach($items as $item)
{
echo $item;
}
Your $items is an array, and I assumed you attempted to
echo $items;
Instead you will need to loop over it:
foreach ($items as $item) {
// View the structure of `$item`
print_r($item);
}
If you only expect $items to hold one thing, then just do:
print_r($items[0]);

Categories