I'm trying to clean up some code and am getting a warning
"Do not Access Superglobal $_GET Array Directly"
on a loop that is used to collect what was returned.
foreach ($_GET as $name => $value) {
$allinfo.= "_GET = $name : $value<br>";
}
Now is is nice and easy to do individual records so
$token = $_REQUEST['token'];
becomes
$token = filter_input(INPUT_REQUEST, 'token');
but I'm slightly stuck on how to fix this for loops.
foreach ($_GET as $name => $value) {
$allinfo.= "_GET = $name : " . filter_input(INPUT_GET, $name) . "<br>";
}
OR
foreach (filter_input_array(INPUT_GET) as $name => $value) {
$allinfo.= "_GET = $name : $value <br>";
}
foreach ($_GET as $name => $value) {
$allinfo.= "_GET = $name : " . filter_input(INPUT_GET, $name) . "<br>";
}
I don't know if this is enough for your code validator (Netbeans I guess). You can also try:
foreach (array_keys($_GET) as $name) {
$allinfo.= "_GET = $name : " . filter_input(INPUT_GET, $name) . "<br>";
}
Related
I'm fairly new to php and I'm having trouble understanding why this piece of code doesn't work:
foreach ($titles as $values) {
echo '' . $values . '';
}
$titles=array('title1','title2','title3');
if (is_array($titles)){
foreach ($titles as $values) {
echo '' . $values . '';
}
}
Please check if your $titles is an array first with is_array().
im experience a proble tryng to read a json the reply from url is this
{"metadata":{"iserror":"false","responsetime":"0.00s"},"results":{"result":[{"Name":"testcampaign","Id":86124,"statuscode":"Success","statusmessage":"Campaign Added Successfully..","URL":"http:\/\/ufa.askyou.co.uk","MaxBid":"1.00","Cap":"None","GeoTargets":"US","BrowserTargets":"ie","UniquesOnly":"true","Schedule":"No Schedule, Free To Run"}]}}
I need to read reasults result status code
i have try this but have not worked :
$json = file_get_contents($url . $Params . $Params2);
$obj = json_decode($json, true);
echo $obj['results']['result']['statusmessage'];
if I use his function to get all data
$obj = json_decode($data, true);
foreach ($json as $key => $value) {
if (!is_array($value)) {
echo $key . '=>' . $value . '<br/>';
} else {
foreach ($value as $key => $val) {
echo $key . '=>' . $val . '<br/>';
}
}
}
they say result Array.
How can i read this correct?
I need to work out how i can get showBtn(3) to match up against the first result in every other key.
ShowBtn/3
btnMenulink/101
btnArticleLink/2
btnPhone/036244789
btnUrl/
btnName/Office
PHP:
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$array = json_decode($jsonresult,true);
foreach ($array as $key => $value) {
foreach ($value as $next_key => $next_value) {
echo $key . ":" . $next_key . ":" . $next_value . "\n";
}
}
I want this:
if (showBtn == 3) {
echo '<a href='tel:btnPhone'>btnName</a>';
}
the result would be
Office Mobile
I almost have it!
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$parsed = json_decode($jsonresult,true);
echo 'Showbtn: '.$parsed['showBtn'][0].' Phone: '.$parsed['btnPhone'][0].' Name: '.$parsed['btnName'][0];
echo '<hr/>Showbtn: '.$parsed['showBtn'][1].' Phone: '.$parsed['btnPhone'][1].' Name: '.$parsed['btnName'][1];
Now i just gotta get that [0] [1] into the loop somehow
I have it but can it be done better?
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$parsed = json_decode($jsonresult,true);
$i=0;
foreach ($parsed as $key => $value) {
if ($parsed['showBtn'][$i] == 3) {
echo 'Showbtn: '.$parsed['showBtn'][$i].' Phone: '.$parsed['btnPhone'][$i].' Name: '.$parsed['btnName'][$i].'<hr/>';
}
$i++;
}
This'll work for you. You need to work it as this way
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$array = json_decode($jsonresult,true);
foreach($array['showBtn'] as $key => $value){
if($value == 3){
echo ''.$array['btnName'][$key].'<br>';
}
}
Output:
Office
Mobile
i have retrieved courseList and courseId from databse like this
foreach ($courses as $item) {
$checkBoxText = '';
$checkBoxText .= $item['courseRubric']. "-". $item['courseNumber']. " ". $item['courseTitle']. " [".$item['semester']. " " . $item['year']. "]";
$this->courseList[] = $checkBoxText;
$checkboxId = '';
$checkboxId .= $item['id'];
$this->courseId[] = $checkboxId;
}
Now, I want to add these array items to Zend_MultiCheckbox,
foreach ($this->courseId as $key => $value) {
$courseId[$value]= $value;
}
foreach ($this->courseList as $key => $value) {
$element->addMultiOptions(array(
$courseId[$key] => $value
));
}
This logic is not working.Can anyone suggest me how I can get
Course
Thanks
You have 2 solutions:
1 :
foreach ($this->courseList as $key => $value) {
$element->addMultiOption("$courseId[$key]", "$value");
}
2:
$opions = array();
foreach ($this->courseList as $key => $value) {
$options[$courseId[$key]] = $value;
}
$element->addMultiOptions($options);
I think, the 2nd is better.
Good luck.
I have a POST in PHP for which I won't always know the names of the variable fields I will be processing.
I have a function that will loop through the values (however I would also like to capture the variable name that goes with it.)
foreach ($_POST as $entry)
{
print $entry . "<br>";
}
Once I figure out how to grab the variable names, I also need to figure out how I can make the function smart enough to detect and loop through arrays for a variable if they are present (i.e. if I have some checkbox values.)
If you just want to print the entire $_POST array to verify your data is being sent correctly, use print_r:
print_r($_POST);
To recursively print the contents of an array:
printArray($_POST);
function printArray($array){
foreach ($array as $key => $value){
echo "$key => $value";
if(is_array($value)){ //If $value is an array, print it as well!
printArray($value);
}
}
}
Apply some padding to nested arrays:
printArray($_POST);
/*
* $pad='' gives $pad a default value, meaning we don't have
* to pass printArray a value for it if we don't want to if we're
* happy with the given default value (no padding)
*/
function printArray($array, $pad=''){
foreach ($array as $key => $value){
echo $pad . "$key => $value";
if(is_array($value)){
printArray($value, $pad.' ');
}
}
}
is_array returns true if the given variable is an array.
You can also use array_keys which will return all the string names.
You can have the foreach loop show the index along with the value:
foreach ($_POST as $key => $entry)
{
print $key . ": " . $entry . "<br>";
}
As to the array checking, use the is_array() function:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)) {
foreach($entry as $value) {
print $key . ": " . $value . "<br>";
}
} else {
print $key . ": " . $entry . "<br>";
}
}
It's much better to use:
if (${'_'.$_SERVER['REQUEST_METHOD']}) {
$kv = array();
foreach (${'_'.$_SERVER['REQUEST_METHOD']} as $key => $value) {
$kv[] = "$key=$value";
}
}
If you want to detect array fields use a code like this:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)){
print $key . ": " . implode(',',$entry) . "<br>";
}
else {
print $key . ": " . $entry . "<br>";
}
}