add variable to url file get php - php

I have a variable define
$commsIP = ['192.168.1.1'];
I am trying to add it to a url
$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/$commsIP");
but I get the following error
Notice: Array to string conversion
but if I put the link like so
$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/192.168.1.1");
It displays fine.

$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/".$commsIP[0]);
or you could not declare it as array
$commsIP ='192.168.1.1';

You defined the variable as an Array, which is why it's saying it can't convert from an Array to a string. Placing []'s around the variable signifies that it's an array.
Just remove the []'s and it will work fine.
$comssIP = '192.168.1.1';

You put brackets around the IP address, when you do this it has the same functionality as an array.
If you change this:
$commsIP = ['192.168.1.1'];
To this:
$commsIP = '192.168.1.1';
It will work.
Alternativly you can also do this:
$commsDisplay = file_get_contents("http://www.dangergaming.com/comms/{$commsIP[0]}");
When you do that it will get the first result out of the $commsIP array.

Related

GET Request not working in WordPress

I'm trying to pull a string from my URL in WordPress, and create a function around it in the functions file.
The URL is:
"https://made-up-domain.com/?page_id=2/?variables=78685,66752"
My PHP is:
$string = $_GET("variables");
echo $string;
So I'm trying to return "78685,66752". Nothing happens. Is the first question mark a problem? Or what am I doing incorrectly? Thanks!
$_GET should be in the form
$string = $_GET["variables"];
and not
$string = $_GET("variables");
$_GET is not a function but an Array so correct way of reading it is
$string = $_GET['variables'];
You are also creating the query string all wrong, you should be using
?variables=123,456&page=1
Read more about $_GET here http://php.net/manual/en/reserved.variables.get.php
Your URL should be like:
https://made-up-domain.com/?page_id=2&variables=78685,66752
instead of:
https://made-up-domain.com/?page_id=2/?variables=78685,66752
& char is separating the queries in URL.
And you have syntax error. Use $string = $_GET["variables"]; because $_GET is a superglobal array, not a function.
Use $variables = explode(",", $string); separate values into an array if you want. Simplier way is $variables = explode(",", $_GET["variables"]);
You should format your href and get parameter like this
http://example.com/mypage.html?var1=value1&var2=value2&var3=value3
+ Edit your get method syntax
$string = $_GET['variables'];

PHP Value from Array

I'm trying to work out how to get values from one of three arrays based on the array name.
$ABC001 = array('A'=>'10','B'=>'2','C'=>'1.0');
$ABC002 = array('A'=>'20','B'=>'4','C'=>'1.1');
$ABC003 = array('A'=>'30','B'=>'6','C'=>'1.2');
I have a variable passed to my script it will be contain something like ABC#001 or ABC#002
I'm removing the # so the var value now matches the array name/
$test = str_replace('#','',$var);
If I do var_dump ( $$test ) I get all the values from the correct array, but if I do echo $$test['A'] or echo $$test[0] I don't get the value from the first key in the correct array.
Can someone advise how to do this.
Thanks
try this ${$test} to get the values of the array
<?php
$ABC001 = array('A'=>'10','B'=>'2','C'=>'1.0');
$ABC002 = array('A'=>'20','B'=>'4','C'=>'1.1');
$ABC003 = array('A'=>'30','B'=>'6','C'=>'1.2');
$var = "ABC#002";
$test = str_replace('#','',$var);
var_dump(${$test}['A']);
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$test['A'] then the parser needs to know if you meant to use $test['A'] as a variable, The syntax for resolving this ambiguity is: ${$test}['A'] . Please check the documention here PHP Variable Variable

PHP - Adding string value to associative array

This seem so simple, and yet I can't find a solution anywhere.
What I want to do is add the contents (r-value) of a variable to an associative array instead of a reference to the variable.
For example, I want this:
$myStr1 = "sometext";
$myStr2 = "someothertext";
$myArray = array(
"key1"=>$myStr1,
"key2"=>$myStr2
);
echo($myArray["key1"]);
To produce this:
"sometext"
Instead of this:
"1" // why??
Any help would be appreciated.
EDIT:
The above works; my bad. Here's the real problem - my $myStr1 variable isn't just assiged a string literal like in the above; it's created using the following syntax:
$myStr1 = "sometext" + anObject->intProperty + "moretext";
Basically I use the + to concatenate various types into a string. Maybe + isn't doing what I think it's doing?
EDIT:
It was definitely the + operator. I casted all non-strings to strings and used . to concatenate instead.
You've got it correct the first time. Try this:
$myStr1 = "sometext";
$myStr2 = "someothertext";
$myArray = array(
"key1"=>$myStr1,
"key2"=>$myStr2
);
unset($myStr1);
echo($myArray["key1"]);
Even though we unset() the $myStr1 variable, it still echoed sometext.
It should be noted that while it is possible to set $myStr1 by reference, it's not the default.
Try your code and its result is:
sometext

pChart variables in arrays

I am just editing the Naked example of pChart, I have setup all of my MySQL information to retrive the AddPoints from the database.
This uses something like the below.
$DataSet->AddPoint(array(1,2,3,4,5,6,7,8,9));
When I attempt to do
$var = '1,2,3,4,5,6,7,8,9';
$DataSet->AddPoint($var);
It doesn't work, but when I do
$var = array('1,2,3,4,5,6,7,8,9');
$DataSet->AddPoint($var);
it does work.
I have also tried:
$var2 = "1,2,3,4,5,6,90";
$var = array("$var2");
$DataSet = new pData;
$DataSet->AddPoint($var);
Any suggestions?
Simple you must provide array to this method:
$DataSet->AddPoint()
I am not sure why you are not giving array but if you want to give a string seperated by comma. You could do this way:
$DataSet->AddPoint(explode(',', '1,2,3,4,5,6,90'));

parse_str doesn't work when the question mark is present?

why I get error when I pass the string with something like 'form.php?', for instance,
parse_str('form.php?category=contacts');
echo $category;
I get this,
Notice: Undefined variable: category in C:\wamp\www\1hundred_2011_MVC\applications\CMS\category_manage.php on line xx
but,
parse_str('category=contacts');
echo $category;
I get what I want,
contacts
how can I fix it? I have to pass something like 'xxx.php?category=contacts' to get 'contacts' or something in the variable.
thanks.
The function parse_str only parses the query string, not the entire URL. Try using parse_url with the component set to PHP_URL_QUERY to extract the query string first, then use parse_str on that.
$url_query = parse_url('form.php?category=contacts', PHP_URL_QUERY);
parse_str($url_query, $output);
echo $output['category'];
Result:
contacts
See it at ideone.
parse_str will only accept query strings:
$q = 'foo?hello=world';
parse_str($q);
echo ${'foo?hello'}; // outputs 'world'
Strip the beginning of the URL first:
$q = 'foo?hello=world';
parse_str(substr($q, strpos($q, '?')+1);
echo $hello; // outputs 'world'
Consider using parse_str second argument to have the data in an array instead, to avoid overwriting local variables.
You may want to use urldecode plus return extracted variables like so:
... some helper class...
/**
* Return parsed serialized JQuery object as PHP array of extracted variables
*/
protected static function parseFields($encoded){
parse_str(urldecode($encoded));
unset($encoded);
return get_defined_vars();
}
Also, you may want to utilize the JQuery function "$.param(data)" for creating URL encoded string:
var encoded=$.param(fields);
and submit via AJAX/POST request to server to function parseFields().

Categories