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..,
Related
This it´s my little script, but don´t get right results at the moment :
<?php
// Delimiters betweeb data "*" elements in each data delimiters ","
$data_string="house1,403,phone1*house2,404,phone2*house3,403,phone3*house4,405,phone3";
// Explode $data_string for "~" delimiter
$data_exp=explode("*",$data_string);
//
// Loop 1
foreach($data_exp as $data_1)
{
$data_exp_compar=explode(",",$data_1);
// We want see the elements with the same data in common in second position (403,404,etc)
$data_common_1[]=$data_exp_compar[1];
$data_common_2[]=$data_exp_compar[1];
}
$a=array_values(array_intersect_key($data_common_1,$data_common_2));
$b=array_count_values(array_intersect_key($data_common_1,$data_common_2));
foreach($a as $aa=>$values)
{
echo $aa;
print "<br>";
}
?>
The idea in this script. It scans the data inside "$data_string", as you can see, all data delimiters is "*" and inside each data we have elements with "," as delimiter
I want get this output results and in this format :
PRODUCT Id: 403 (2 Actually)
1- house1,403,phone1
2- house3,403,phone3
PRODUCT Id: 404 (1 Actually)
1 - house2,404,phone2
Product Id: 405 (1 Actually)
1 - house4,405,phone4
As you can see the only element for compare it´s in the second position and it´s product´s id
I try many things but i can´t get works, or get finally results as i want show
Thank´s in advanced for all , regards
You can group them first then another foreach loop for printing result
$data_string="house1,403,phone1*house2,404,phone2*house3,403,phone3*house4,405,phone3";
$data_exp = explode("*",$data_string);
$group = []; // Initialize group array
foreach($data_exp as $data_1)
{
$data_exp_compar=explode(",",$data_1);
$group[$data_exp_compar[1]][] = $data_exp_compar; // Group by the number key after exploding
}
// Loop to each group, then print desired format
foreach ($group as $key => $value) {
echo 'Product ID: ' . $key . ' (' . count($value) . ' Actually)<br>';
foreach ($value as $k => $v) {
echo ++$k . ' - ' . implode(',', $v) . '<br>';
}
echo '<br>';
}
I would suggest using array_map and array_filter functions. Let me know if you have questions about this.
<?php
// Prepare data and input
$id = 403;
$data = "house1,403,phone1*house2,404,phone2*house3,403,phone3*house4,405,phone3";
// Convert string data to array
$data = explode("*", $data);
$data = array_map(function ($row) {
return explode(",", $row);
}, $data);
// Search the array
$response = array_filter($data, function ($row) use ($id) {
return $row[1] == $id;
});
print_r($response);
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I display variable $player->name inside a form option value so user could select variable and submit form.
Here's my code that is not working :
<?php
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.j son");
$json = json_decode($result);
$goalies = $json->goali;
foreach ($json->goalie as $player) {
**echo "<option value=\"".$player->name."\">".$player->name."</option>**
}
?>
You did not have the double quotes and semicolon at the end of statement.
<?php
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.json");
$json = json_decode($result);
$goalies = $json->goali;
foreach ($json->goalie as $player) {
echo "<option value=\"".$player->name."\">".$player->name."</option>";
}
?>
What is this $json->goalie value in foreach , use $goalies instead
<?php
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.json");
$json = json_decode($result);
$goalies = $json->goali;
foreach ($goalies as $player)
{
echo "<option value='".$player->name."'>".$player->name."</option>";
}
?>
Besides the obvious parse error you have in your code, here are some other things to look out for:
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.json");
You can't just use $team inside a URL like that, you're supposed to encode it:
$url = sprintf("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/%s/iphone/clubroster.json",
urlencode($team)
);
$result = file_get_contents($url);
$json = json_decode($result);
foreach ($json->goalie as $player) {
echo "<option value=\"".$player->name."\">".$player->name."</option>";
}
You should always escape your variables in output:
printf('<option value="%s">%1$s</option>',
htmlspecialchars($player->name, ENT_QUOTES, 'UTF-8')
);
I'd recommend decoding your JSON to an array, not an object, it is easier to work with.
You aren't using the goalies variable in your above code, I'm assuming that's what you want to use in the foreach statement?
Error checking would be a good idea
If so, then your code may look something like this:
<?php
$team = $_POST['team'];
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/$team/iphone/clubroster.json");
$json = json_decode($result, true);
$goalies = array();
if (!empty($json['goali'])) {
$goalies = $json['goali'];
}
foreach ($goalies as $player) {
echo '<option value="' . $player['name'] . '">' . $player['name'] . '</option>'
}
?>
If you want to display varibles inside string then always use "" for displaying the variable. You can modify your code as:
echo "<option value='{$player->name}'>{$player->name}</option>";
So always remember:
1. You can use ''(singlequots) inside ""(doublequotes)
You were not using the ' correctly around your variables.
Demo
<?php
// Create a dummy Object
$player = new stdClass();
// Create a dummy property
$player->name = "Occam's Razor";
// Print it out
echo '<option value=" '.htmlspecialchars($player->name).' ">' .htmlspecialchars($player->name). '</option>';
This prints out the following.
<option value=" Occam's Razor ">Occam's Razor</option>
Instead of writing about what the problem is, I think it would be more helpful to just show you, and give you some pointers:
// Make sure all errors are displayed
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Get the team variable and make sure that it was set
$team = filter_input(INPUT_POST, 'team', FILTER_UNSAFE_RAW);
if ($team === null) {
die('Error: The "team" POST variable is not set, cannot continue');
}
// Since you are using the variable inside a URL it makes
// sense to remove potentially unsafe characters; or in
// other words, only preserve characters that are allowed.
// Encoding the variable, as in Ja͢ck's answer, is also an option.
$team = preg_replace('/[^a-zA-Z0-9_-]/', null, $team);
if ($team === "") {
die('Error: The "team" variable is empty, cannot continue');
}
// Construct the URL
$url = "http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/{$team}/iphone/clubroster.json";
// Grab the data from the website
$data = file_get_contents($url);
if ($data === false) {
die('Error: Could not grab the data from the URL, cannot continue');
}
// Attempt to decode the data
$json = json_decode($data);
if (json_last_error() !== JSON_ERROR_NONE) {
die('Error: There was a JSON error ('.json_last_error_msg().'), cannot continue');
}
// Since we are expecting an object we should check for it
// before using it
if ( ! is_object($json)) {
die('Error: The JSON was decoded, but is not an object, cannot use it...');
}
// If the following code gives you problems then read the error
// message, try to understand what it says, and then google it if
// you are getting nowhere
$goalies = $json->goali;
foreach ($json->goalie as $player) {
echo "<option value=\"".$player->name."\">".$player->name."</option>";
}
I am setting up an input autocomplete using jquery tokeninput plugin and XML data.
I have an XML file structured as shown:
<?xml version="1.0" encoding="UTF-8"?>
<majors>
<major program="GCIS">Computing & Info Sci (PHD)</major>
<major program="UINT">Business Administration (AAS)</major>
etc..
</majors>
I load it into PHP:
$majors = simplexml_load_file('../ajax/majors.xml');
I then want to do the following things:
reformat each <major>element into string, including program attribute
(ex: <major program="GCIS">Computing & Info Sci (PHD)</major> turns into string GCIS - Computing & Info Sci (PHD)
run the converted string through a filter function. The filter function checks for strpos($convertedString, $userQuery) and returns true/false if the user's query is present
elements which DO contain the $userQuery are all then encoded with json_encode($arr)
return JSON data.
This is the code I currently have... I can't seem to get the formatting / filtering to work correctly.
if(isset($_POST['query']) ) {
$majors = simplexml_load_file('../ajax/majors.xml');
# iterate through.
foreach ($majors as $key => $value) {
$arr = array('major' => $value['program'] . " - " . $value);
}
# filter the response using our query
$arr = array_filter($arr, 'filterArrayWithQuery');
# JSON-encode the response
$json_response = json_encode($arr);
# Return the response
return $json_response;
}
# ensures that the search query is present
function filterArrayWithQuery( $string ) {
return !strpos( $string, $query ) === false;
}
The end-result JSON output should look like this:
{"major":"GCIS - Computing & Info Sci (PHD)","major":"UINT - Business Administration (AAS)"}
In your iterate through line, you are not appending new entries -
# iterate through.
foreach ($majors as $key => $value) {
$arr[] = array('major' => $value['program'] . " - " . $value);
// ^^
}
With this the output would be-
[{"major":"GCIS - Computing & Info Sci (PHD)"},{"major":"UINT - Business Administration (AAS)"}]
Try this:
function convert($xml) {
# ensures that the search query is present
function filterArrayWithQuery($string) {
static $query = null;
if (is_null($query) && isset($_POST['query'])) $query = $_POST['query'];
return !$query || strpos($string, $_POST['query']) !== false;
}
$majors = simplexml_load_string($xml);
$arr = array();
# iterate through
foreach ($majors as $key => $value) {
$arr[] = array('major' => $value['program'] . " - " . htmlentities($value));
}
# filter the response using our query
$arr = array_filter($arr, 'filterArrayWithQuery');
# Return the response (JSON-encoded)
return json_encode(array('majors' => $arr));
}
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<majors>
<major program="GCIS">Computing & Info Sci (PHD)</major>
<major program="UINT">Business Administration (AAS)</major>
</majors>
XML;
$_POST['query'] = 'Info';
echo convert($xml);
Thank you to the input I received, however I was able to figure out the solution.
Here is the final PHP script for those interested.
<?php
# vars
$reqMethod = $_SERVER['REQUEST_METHOD'];
$ref = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : "http://www.google.com";
$allowedHosts = array('redacted', 'localhost');
$majors = null;
$arr = array();
$query = isset($_POST['q']) ? $_POST['q'] : "";
# confirm request is being sent from where we allow.
if ( $reqMethod != 'POST' || !in_array(parse_url($ref, PHP_URL_HOST), $allowedHosts) ) {
header('Location: ' . $ref);
} else {
# load in XML
$majors = simplexml_load_file('../ajax/majors.xml');
# iterate through XML. if element contains query, add it to an array.
foreach ($majors as $key => $value) {
if( !stristr( $value, $query ) === false ){
$arr[] = array('major' => $value['program'] . " - " . $value);
}
}
# Return the response - JSON encoded
header('Content-type: application/json');
echo json_encode($arr);
}
?>
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/>';
}