I used http build query to send data from URL
The Url is in the following format :
http://www.abc.in/xyz/Tak-Wadi?0%5Bmerchant_location_id%5D=1&1%5Bmerchant_location_id%5D=2
Now How to get data ie. merchant_location_id 1 & 2.
This might help you.
echo $id1 = $_GET[0]['merchant_location_id'];
echo $id2 = $_GET[1]['merchant_location_id'];
if you do the following:
$url = urldecode("http://www.abc.in/xyz/Tak-Wadi?0%5Bmerchant_location_id%5D=1&1%5Bmerchant_location_id%5D=2");
echo $url;
That will clear it up a bit:
http://www.abc.in/xyz/Tak-Wadi?0[merchant_location_id]=1&1[merchant_location_id]=2
Now, this url let's you know that you've got two arrays each containing one key (merchant_location_id)
$_GET[0]
$_GET[1]
Now that we know this it's easy to retrieve the data.
echo $_GET[0]['merchant_location_id'];
echo $_GET[1]['merchant_location_id'];
That should be all there is to it.
$queryString = array();
foreach ($_GET as $key => $value) {
$queryString[] = $key . '=' . $value;
}
$queryString = implode('&', $queryString);
For further assistance you can get reference from this weblink.
Related
I am trying to get the data from an API and save it to a MySQL database. The problem is when I want to echo the data to check if it gets all the values I'm getting this error:
Notice: Trying to get property of non-object
My JSON data looks like this:
{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"},
I think this is because it is in an array. Because when I use this bit of code it gives no errors but I have to define the index of the array. What is the best way to loop through my data and put it in variables?
<?php
//Get content
$url = "https://eftelingapi.herokuapp.com/attractions";
$data = json_decode(file_get_contents($url));
//Fetch data
$attractieId = $data->AttractionInfo[0]->Id;
$attractieType = $data->AttractionInfo[0]->Type;
$attractieStatus = $data->AttractionInfo[0]->State;
$attractieStatusKleur = $data->AttractionInfo[0]->StateColor;
$attractieWachttijd = $data->AttractionInfo[0]->WaitingTime;
$attractieStatusPercentage = $data->AttractionInfo[0]->StatePercentage;
?>
I tried to find some solutions but all they use is a foreach loop. But i don't think that'll work right. Can anyone help me in the good direction or tell me how I might possibly fix this? I am not very experienced so any advice is welcome. Thanks in advance.
Update code:
require 'database-connection.php';
//Get content
$url = "https://eftelingapi.herokuapp.com/attractions";
$data = json_decode(file_get_contents($url));
//Fetch data
$attractieId = isset($data->AttractionInfo->Id);
$attractieType = isset($data->AttractionInfo->Type);
$attractieStatus = isset($data->AttractionInfo->State);
$attractieStatusKleur = isset($data->AttractionInfo->StateColor);
$attractieWachttijd = isset($data->AttractionInfo->WaitingTime);
$attractieStatusPercentage = isset($data->AttractionInfo->StatePercentage);
$sql = "INSERT INTO attracties (attractieId, attractieType, attractieStatus, attractieStatusKleur, attractieWachttijd, attractieStatusPercentage)
VALUES ('$attractieId', '$attractieType', '$attractieStatus', '$attractieStatusKleur', '$attractieWachttijd', '$attractieStatusPercentage')";
if ($db->query($sql) === TRUE) {
echo "success";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
It says 'success' but when I look into my database it inserted only empty data. I need to add all the attractions to my database so not just one row. So I need to loop through my data.
try this...
$content = json_decode(file_get_contents($url));
foreach($content->AttractionInfo as $data ){
$id = $data->Id;
$type = $data->Type;
$map = $data->MapLocation;
$state = $data->State;
$color = $data->StateColor;
if(!empty($data->WaitingTime)) {
$time = $data->WaitingTime;
}
if(!empty($data->StatePercentage)) {
$percent = $data->StatePercentage;
}
//persist your data into DB....
}
I think you need something like this:
$json = '{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"}]}';
$arr = json_decode($json);
foreach ($arr->AttractionInfo as $key => $attraction) {
foreach($attraction as $key=> $value) {
print_r($key.' - '.$value.'<br>');
$$key = $value;
}
}
echo '<br>';
echo $Id; // it will be last item/attraction id.
We can improve this code. Just say where and how do you want to use it
In id==2 I am not able to capture the data (url and title) of the array of id==1 using $ _GET. Where am I going wrong?
P.S.: I do not intend to use an external library.
<?php
$id = $_GET['id'];
//var_dump($_GET);
if ($id == 1) {
$re = '/href="(?P<link>.*?)" title="(?P<title>.*?)" class="iGh__menu-link/';
$html = file_get_contents('https://www.ig.com.br/');
preg_match_all($re, $html, $data);
//print_r ($data['link']);
//print_r ($data['title']);
foreach ($data as $key => $value) {
"http://$_SERVER[HTTP_HOST]/loja.php" . '&url=' . $data['link'][$key] . '&title=' . $data['title'][$key] . '&s=1&logo=""';
}
} else if ($id == 2) {
$url = $_GET['url'];
$title = $_GET['title'];
}
?>
check $_GET for below if its empty than store none
$id=isset($_GET['id'])?$_GET['id']:"";
if $_GET get empty than store none otherwise store value on id and also check witch method you use to send data like post,get etc..,
I am using a URL to fetch data stored/shown within URL. I get all the value of variable using $_REQUEST['v_name'] but if there is a array in URL how can i retrieve that value.
For Example:
WWW.example.com/rooms?&hid=213421&type=E
I got the value hid and type using
$hid=$_REQUEST['hid'];
but in URL like:
WWW.example.com/rooms?&rooms=2&rooms[0].adults=2&rooms[0].children=0&rooms[1].adults=2&rooms[1].children=0
how can i retrieve value of adults and children in each room.
please help.
Thanks in Advance
You could also try something like this, since most of your original $_REQUEST isn't really an array (because of the .s in between each key/value pair):
<?php
$original_string = rawurldecode($_SERVER["QUERY_STRING"]);
$original_string_split = preg_split('/&/', $original_string);
$rooms = array();
foreach ($original_string_split as $split_one) {
$splits_two[] = preg_split('/\./', $split_one);
}
foreach ($splits_two as $split_two) {
if (isset($split_two[0]) && isset($split_two[1])) {
$split_three = preg_split('/=/', $split_two[1]);
if (isset($split_three[0]) && isset($split_three[1])) {
$rooms[$split_two[0]][$split_three[0]] = $split_three[1];
}
}
}
// Print the output if you want:
print '<pre>' . print_r($rooms, 1) . '</pre>';
$valuse = $_GET;
foreach ($valuse as $key=>$value)
{
echo $key .'='. $value. '<br/>';
}
How do I split the URL and then get its value and store the value on each text input?
URL:
other.php?add_client_verify&f_name=test&l_name=testing&dob_day=03&dob_month=01&dob_year=2009&gender=0&house_no=&street_address=&city=&county=&postcode=&email=&telp=234&mobile=2342&newsletter=1&deposit=1
PHP:
$url = $_SERVER['QUERY_STRING'];
$para = explode("&", $url);
foreach($para as $key => $value){
echo '<input type="text" value="" name="">';
}
above code will return:
l_name=testing
dob_day=3
doby_month=01
....
and i tried another method:
$url = $_SERVER['QUERY_STRING'];
$para = explode("&", $url);
foreach($para as $key => $value){
$p = explode("&", $value);
foreach($p as $key => $val) {
echo '<input type="text" value="" name="">';
}
}
Why not using $_GET global variable?
foreach($_GET as $key => $value)
{
// do your thing.
}
$queryArray = array();
parse_str($_SERVER['QUERY_STRING'],$queryArray);
var_dump($queryArray);
php has a predefined variable that is an associative array of the query string ... so you can use $_GET['<name>'] to return a value, link to $_GET[] docs
You really need to have a look at the Code Injection page on wikipedia you need to ensure that any values sent to a PHP script are carefully checked for malicious code, PHP provides methods to assist in preventing this problem, htmlspecialchars
You should use the $_GET array.
If your query string looks like this:
?foo=bar&fuz=baz
Then you can access the values like this:
echo $_GET['foo']; // "bar"
echo $_GET['fuz']; // "baz"
I need to pass a variable to a foreach loop from a mySQL result.
So I have this code:
$GetClaim = "SELECT * FROM cR_Claimants WHERE memberID = '".$memberID."' AND ParentSubmission ='".$refNumb."'";
$resultGetClaim=mysql_query($GetClaim) or die("Error select claimants: ".mysql_error());
while($rowGetClaim = mysql_fetch_array($resultGetClaim)) {
$name = $rowGetClaim['Name'];
$city = $rowGetClaim['city'];
$region = $rowGetClaim['region'];
}
Now I need to pass the variable to the foreach
foreach($name as $k=>$v) {
echo $city;
echo $region;
etc..
}
The above code does not work. I think I cannot pass a variable from a mySQL loop. The problem is also tat every row I get from the database should be related to the specific $name. So obvioiusly one $name will have its own $city etc..
How do I achieve this?
Please help
You are not retrieving an array with all returned records, you are retrieving an array which contains a single record.
To get the next name (the next record), you must make another call to mysql_fetch_array.
The code you present does that implicitly by assigning $rowGetClaim within a while conditional. A failed mysql_fetch_array call would return false, which would exit the while loop.
There is absolutely no need to use the for each as you presented. Just place the echo right after the assignment (e.g.
$region = $rowGetClaim['region'];
echo $region
Either out put directly fromt eh loop or build an array and then loop through it.
while($rowGetClaim = mysql_fetch_array($resultGetClaim)) {
echo $rowGetClaim['Name'];
echo $rowGetClaim['city'];
echo $rowGetClaim['region'];
}
OR
while($rowGetClaim = mysql_fetch_array($resultGetClaim)) {
foreach($rowGetClaim as $k => $v{
echo $v;
}
}
OR
$names = array();
while($rowGetClaim = mysql_fetch_array($resultGetClaim)) {
$names[] = $rowGetClaim;
}
foreach($names as $data){
foreach($data as $k => $v) {
echo $v;
}
}