POSTing data using for-each with javascript and php? - php

foreach loop only repeating the last element of the arary?
java Script Code :
<?php foreach($_REQUEST["itemprice"] as $itemprice)
{
?>
+ "&itemname_price[]=<?php echo $itemname?>_price=" + <?=$itemname?>_price.value
<?php
}
?>
Code to request that data
$items_price = array();
foreach($_REQUEST['itemname_price'] as $itemname_pric) {
$items_price[]=$itemname_pric;
}
print_r($items_price);

foreach is used to get the values from an array, and what you are trying here is to get data from a specific array key. I guess that's why you are getting a single value. You can refer to
http://php.net/manual/en/control-structures.foreach.php to get the exact use of foreach.
Let me know if you have anything to know.

Related

I want a query result using codeigniter

i have an array result. i want to print 2 rows(product data) in first page.
next 2 rows in second page and so on. if anybody knows this,please help me to solve it
my array
$data['product_list']
foreach($data['product_list'] as $dat)
{
echo $dat->prd_id;
echo $dat->prd_name;
}
You are doing a foreach loop on an associative array and then trying to access the the contents as objects by using ->. I can only given assumption of what you might be doing. If your array is already populated with a name and id like you have described in your foreach loop this is how you would access the contents in the loop:
foreach($data['product_list'] as $dat)
{
echo $dat['prd_id'];
echo $dat['prd_name'];
}
That is how you would print out the contents providing you had the data stored in your array like so:
$data['product_list'][0] = array('prd_id'=>'id0','prd_name'=>'name0');
$data['product_list'][1] = array('prd_id'=>'id1','prd_name'=>'name1');
$data['product_list'][2] = array('prd_id'=>'id2','prd_name'=>'name2');
Better you try with array_slice();
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>

PHP JSON foreach Array Issue

I'm trying to use PHP to display some JSON data from an API. I need to use foreach to return all my results but nested within them is an array. The array is "highlights" which sometimes has "description" and sometimes "content" and sometimes both. I need to do a foreach within a foreach or something along those lines but everything I try just returns "Array".
Here's the JSON...
https://api.data.gov/gsa/fbopen/v0/opps?q=lte+test+bed+system&data_source=FBO&limit=100&show_closed=true&api_key=CTrs3pcYimTdR4WKn50aI1GcUxyL9M4s1fyBbSer
Here's my PHP...
$json_returned = file_get_contents("JSON_URL");
$decoded_results = json_decode($json_returned, true);
echo "Number Found:".$decoded_results['numFound']."</br> ";
echo "Start:".$decoded_results['start']."</br>";
echo "Max Score:".$decoded_results['maxScore']."</br>";
foreach($decoded_results['docs'] as $results){
echo "Parent Link T:".$results['parent_link_t']."</br>";
echo "Description:".$results['highlights']['description']."</br>";
}
Obviously the working version I'm using has a lot more fields programmed in but I cut them out to keep this code short and simple and show how I have everything else besides the "hightlights" field in one foreach. The JSON returns require that I keep everything in that foreach, so how to I display the array inside of it?
Thanks for any help and thanks for taking the time to read this even if you can contribute.
The 'description' is array with one element so you can use this.
echo 'Description:' . $results['highlights']['description'][0];
If it sometimes has 'description' and sometimes 'content'. Use isset to check which one it is, or even if there are both and print accordingly.
// for description
if(isset($results['highlights']['description'])) {
echo 'Description:' . $results['highlights']['description'][0];
}
// for content
if(isset($results['highlights']['content'])) {
echo 'Content:' . $results['highlights']['content'][0];
}
Hope this helps.
Look into the php array_column() function: http://php.net/manual/de/function.array-column.php

get all data from a variable

if i want to see what data is available into a variable, i fetch the data like ---
get(0) to get the first data like 'www.hello.com/23/23', get(1) to get the second data like 'www.mydomain.com/it/12', and so on ....
$apartmentId = $user->getApartment()->get(0)->getId();
Now if there are more data available in the getApartment(), which method i should use to get all the id's which is available in getApartment() !
i Have tried getAll() method which dose not working in this case. Anyone have any idea how to solve this problem. Thanks in advanced .
One way to get all values, is to use a foreach() loop.
<?php
$var = array(
'value_One',
'value_Two',
'value_Three',
'value_Four',
'value_Five'
);
foreach($var AS $value){
echo $value . "<br>";
}
?>

foreach $_POST as $value create

I just started learning PHP and I am having some difficulties with some of the coding.
Hopefully, someone could help me a little.
I'm using this:
if(!empty($_POST['yyy'])) {
foreach($_POST['yyy'] as $a1) {
echo " $a1";}}
The echo will write several results of $a1 depending on how many were selected in the form.
What I want is to save those results to some values so I can add them in MySQL.
Something like this:
if(!empty($_POST['yyy']))
{
foreach($_POST['yyy'] as $a1)
{
echo " $a1"; where $a1 will create a $result1,$result2,$result3(for each isset)
}
}
Then if I use:
echo "$result2";
it will give me the second result.
Not clear whether you are asking about this kind of result or not. But you can use an array to store each values inside the foreach loop.
var data=[];// define an array to access outside of if statement later..
if(!empty($_POST['yyy'])) {
foreach($_POST['yyy'] as $a1){
data[]=$a1;
//or can use array_push() method
array_push(data,$a1);
}
}
/*this will give the second result(because array indexing starts from 0. So to get third result
use data[2])*/
echo data[1];
Furthermore by echoing quoted variable will not give the value of that variable but gives a string literal.
echo "$result2" //output---> $result

Proper way to get attribute from json

<?php
$json_string = file_get_contents('infile.json');
$get_json_values=json_decode($json_string,true);
foreach ($get_json_values as $key=>$getlikes) {
if($getlikes[$key]['type']=='like') {
?>
<div><p><?php echo $getlikes['name'] ?></p></div>
<?php
}}
?>
There is an error in the code above: undefined offset on the line with the if. I guess it is because $getlikes[$key]['type'] is wrong. What would be the proper code to get all the objects with the type attribute being "like"?
Json can be downloaded from here
This really depends on the content of your .json object. But most likely the undefined offset is being caused because not all $getlikes[$key] has an array key of 'type'. Validate to make sure it exists before you compare. Try
foreach ($get_json_values as $key=>$getlikes) {
if(!isset($getlikes['type'])) continue;
if($getlikes['type']=='like') {
....
EDIT:
Based on your .json file you were just adding an additional key which was unneeded. Changed the code to reflect the correct call. Still not a bad idea to check if a value is set. Try a var_dump($getlikes) or on the file you are using to get a good look at what you are iterating over.
Given the JSON string it's clear what you're doing wrong. $get_json_values is a list with no indexes, you're doubling up on the key, hard to explain. Do this instead.
foreach ($get_json_values as $getlikes) {
if($getlikes['type']=='like') {
If you were to use $key, you'd use it like this
foreach ($get_json_values as $key=>$getlikes) {
if($get_json_values[$key]['type']=='like') {
but you wouldn't do that anyway. Foreach extracts the elements one by one.

Categories