pChart variables in arrays - php

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'));

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

Get a variable name from database

I'm a little confused - how do I get a variable name stored to a database?!
Record in the database is a string: "$test"
The variable $user is set before the records a fetched from database. So I want to "convert" this string to a real variable to get the value of it.
The following didn't work:
// $test is set to 'bla'
$test = 'bla';
// $var is the value from the database
$var = '$test';
// print $test
echo ${$var};
I know that it would work if I remove the '$' from the database record
$var = 'test';
echo $$var;
But how to handle this without?
String replace in better option, if you don't want to replace $ you can use eval function:
$test = 'bla';
$var = '$test';
eval("\$var = \"$var\";");
echo $var; //output: bla
I was facing a similar problem just now.
Here is what I did:
$res[0] is obtained from mysql_fetch_array() and it contains another query which has $variable embedded.
$qry="select query from sql_list where id=".$sql_id;
$result=mysql_query($qry);
$res=mysql_fetch_array($result);
eval("\$qry_2 = \"$res[0]\";");
mysql_query($qry_2)
It works! Maybe someone can suggest a better way.
This is 3 years later but since no one give you your answer, maybe this will help you or the next person searching for this.
From what I understand, you already declared a variable and stored the name of that var in the db.
$var = '$test'; // from db
$var[0] = ''; // remove first letter, simple & much faster than ( trim or substr )
//content of var
$var_content = ${$var};

creating variable name by concatenating strings in php

I have a file named questions.php with an array as follows :
$question12 = array("Which is the tallest mountain","Mt Everest");
I am including this file in another file as follows :
require_once('questions.php');
$var = 12;
$question = '$question'.$var.'[0]';
echo $question;
The above code just outputs the following string (not the contents of the variable):
$question12[0]
But I want the variable $question to contain the string present in $question12[0].
How do I accomplish this?
Variable variable is not recommended, but the answer is below:
$question = ${'question'.$var}[0];
You're looking for variable variables.
$id = 12;
$q = "question{$id}";
$q = $$q[0];
You should seriously consider looking into multidimensional arrays to stop having multiple arrays.
Just use $question12[0]. It will give you the desired output.
Using the $var you can do it like this:-
$question = ${'question'. $var}[index];
Sorry, im going to get some hate for mentioning something evil but still it is one of the options
<?php
$question12 = array("Which is the tallest mountain","Mt Everest");
$var = 12;
$question = '$question'.$var.'[0]';
eval("echo $question;");
?>
P.S: eval() is that evil

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

Categories