How to echo the post value received in post - php

I'm using image uploader plugin which is posting data in multi dimension array like shown below
Array
(
[file] => {"input":{"name":"KE6Cc2ea2b584.jpg",
"type":"image/jpeg",
"size":61224,
"width":800,
"height":643},
"output":{"width":320,"height":180,
"image"
:"data:image/jpeg;base64,/9j/just an example of base 64 encoded image"},
"actions":{"crop":{"x":0,"y":96.5,"height":450,"width":800 },
"size":{"width":320,"height":320}}}
)
Now i tried lot to echo these post values like shown below
echo $_POST['file']; worked but brought all the data
echo $_POST['file']['output']; didn't worked
echo $_POST['file']['output']['image']; didn't worked
How can i get the value of these post

When trying to echo out any sort of values while debugging, it's a good idea to see exactly what you're getting via var_dump. For example, instead of your echo lines, I'd simply put:
echo "<pre>";
var_dump($_POST['file']);
Check your output there and it should show you exactly what keys you have to work with.
Also, depending on how your plugin is sending the data, you may not get any actual keys. According to your comment about $_POST['file'] printing all the output, it seems as though it's still a string. Try putting the JSON into an object, like so:
$test = json_decode($_POST['file']);
Or, if you'd prefer an associative array as mentioned:
$test = json_decode($_POST['file'], true);
Then get the output of $test:
echo "<pre>";
var_dump($test);

Related

Can not echo data from MYSQL database

Hello! The data in table is like that, and I cannot echo these entries individually. I want to show this data in HTML table like for example, Subject: Math, Correct: 34, Wrong: 6.
I am new in php, please help me with this case.
The data in the table seems to be in JSON format. You can convert it to a PHP object using json_decode() then print that object using print_r().
When put all together, you get
echo print_r(json_decode($data), true);
Note, I have to edit this response back to what it is now because #fenil-shah had changed it some something else that was not suitable. Please send your own response instead of making others say what they didn't intend to.
#fenil-shah, my answer was written the way it was on purpose. Here are the explanations:
In general, if you want to stay in control of the result of the print_r() function, you need to return its result as a string. That's why I have passed true as second parameter for the print_r() function. You wouldn't using print_r() without returning its result as a string in a middle of another script because it will output some text in an uncontrolled manner that can break an add. Once you have the return string, you can send it to a log file or do whatever you want with it.
json_decode works beautifully without passing it a true as second parameter. The OP's example is showing a object. You edit was purposely changing the decoded object to an array with associative array. Why?
You have removed the space after the comma which you had moved in from the print_r() to the json_decode() to make it look like json_decode($data,true). You need to know that the space was there for two reasons: Readability and Compliance with the PSR-2 standard (read more about that PHP coding standard at https://www.php-fig.org/psr/psr-2/#46-method-and-function-calls).
There are echo "",print_r() and print() function available in php to print data.If query is returning array then use print_r() function but if query is not returning anything or invalid fetch method in php then data is not getting display by any of these functions.
I think this is what you want as a output.
You can decode the JSON received from database using json_decode() function and get your result transformed in associative array.
Later on passing the array in foreach() loop will do your work accordingly
<?php
$result = '{
"30": {
"subject_id":343,
"correct_answers":34,
"wrong _answers":61,
"not_answered":0,
"time_spent":3801,
"time_to_spend":3680,
"time_spent_correct_ answers":3286,
"time_spent_wrong_answers":515
},
"52": {
"subject_id":52,
"correct_answers":7,
"wrong_answers":3,
"not_answered":0,
"time_spent":883 ,
"time_to_spend":94343,
"time_spent_correct_ans wers":352,
"time_spent wrong_answers":441
},
"53": {
"subject_id":53,
"correct_answers":3,
"wrong_answers":7,
"not_answered":43,
"time_spent":584 ,
"time_to_spend":900,
"time_spent_correct_ans wers":154,
"time_spent wrong_answers":430
}
}';
$json_decoded_data = json_decode($result,true);
?>
<table>
<tbody>
<tr>
<th>subject_id</th>
<th>correct_answers</th>
<th>wrong_answers</th>
</tr>
<?php
foreach($json_decoded_data as $row){
echo "<tr>";
echo "<td>".$row['subject_id']."</td>";
echo "<td>".$row['correct_answers']."</td>";
echo "<td>".$row['correct_answers']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>

Retrieve string from json

I'm trying to retrieve a URL string from some json code.
Here is the json code
{"files":["www.example1.com"],"previews":["www.example1preview.com"],"meta":{},"userId":"guest","product":{"id":"2335","name":"standard"},"type":"u"}
Looking at what I've seen in the PHP manual I'm trying to retrieve previews like this.
<?php
ob_start();
include('getjson.php');
$meta_value_json = ob_get_clean();
echo $meta_value_json;
$meta_value_json = json_decode($meta_value_json);
print $meta_value_json->{'previews'};
?>
This doesn't seem to output the value however.
By experimenting with php -a command on terminal, I've put your json into json_decode and managed to get your link by just doing:
print $meta_value_json->previews[0];
The only reason to use print $meta_value_json->{'previews'}; at least according to php documentation is if you want an object as output and the key trying to retrieve is numerical or of a type that is not supported by php.
By experimenting a bit further, the reason that print $meta_value_json->{'previews'}; fails is because print expects a string, in our case here previews is an array. Therefore if you do print $meta_value_json->{'previews'}[0]; it will also work as expected.
You need to get the value from your decoded json like this: $class->parameter.
Knowing that previews is an array, you will also need to choose a specific element from it to print ( I got the first one ):
<?php
ob_start();
include('getjson.php');
$meta_value_json = ob_get_clean();
echo $meta_value_json;
$meta_value_json = json_decode($meta_value_json);
print $meta_value_json->previews[0]; /// get the specific value
?>

How to view print_r values

I have been given this php code
echo "<pre>";
print_r(htmlentities($data));
echo "</pre>";
I am really not into php programming and I really don't know where am I gonna see the output of the print_r function. Such a petty question but I hope I could get a decent answer. Thanks!
As per the PHP manual: print_r: 'Prints human-readable information about a variable'. Assuming that $data is an array, you will have an output of that data as plain text - it is basically var_dump (the main difference is that var_dump will also output types).
$data = array('item', 'name', 'form');
echo "<pre>";
print_r($data);
echo "</pre>";
Output:
Array
(
[0] => item
[1] => name
[2] => form
)
However, keep in mind htmlentities is looking for a string; and if you input an array you will get an error. So if you do have a string it would be okay to use htmlentities
<?php
$data = 'Hello World!';
echo "<pre>";
print_r(htmlentities($data));
echo "</pre>";
?>
Output:
Hello World!
It goes to PHP's output buffer. If you are running the script from a command line client the output will display on the terminal but typically PHP is run on a web server upon the request of a user. In the latter case, if your script was named 'example.php' and accessible at http://example.com/example.php visiting that URL would show the output in the web browser.
Should display right at the top of your page you're running. print_r displays an arrays contents. You're better off doing this:
$array = ("cat","dog");
echo "<xmp>";
print_r($data);
echo "</xmp>";
So in the browser navigate to page.php, it'll display your array. Now if you're in command line doing this it'll come to the output buffer but that depends on your setup.

JSON PHP using API pointers

I'm trying to learn JSON and PHP using API.
My problem is that I can't get out the data of example Array 0, the output as can seen at bottom if you visit the link, just Array googled as mutch as I can but kinda new on the json array.
$json_data=json_decode($json_array,true);
print_r($json_data);
echo "<br>";
echo $json_data['total'],"<br>";
echo $json_data['skipped'],"<br>";
echo $json_data['count'],"<br>";
echo $json_data['0'],['name'],"<br>";
Based on your comment, I think I see what you want to do. You're trying to access the second level of your array incorrectly. You don't put anything between the two bracket sets. You also don't need to put the 0 in quotes, as it is not a string. I think you want this:
$json_data=json_decode($json_array,true);
print_r($json_data);
echo "<br>";
echo $json_data['total'],"<br>";
echo $json_data['skipped'],"<br>";
echo $json_data['count'],"<br>";
echo $json_data[0]['name'],"<br>"; // Line changed, removed comma and quotes.
EDIT: Note that the [0]['name'] is inside the hits value, so you'll probably want this instead:
echo $json_data['hits'][0]['name'];

fetch array from a given url in php

i have a url.. eg http://www.example.com/index.php?rou=feed/web_api/categories&key=4&parent=0&level=10
when i hit the link simply on browser it displays a long list of multidimensional array.
I want to know that is it possible to store the url in a variable, something like this
$link=http://www.example.com/index.php?rou=feed/web_api/categories&key=4&parent=0&level=10
And then display the array in it as
echo '<pre>';
print_r($link);
echo '</pre>';
and then fetch the values from the array
I tried doing so but it displayed only a blank screen. Not sure if i am going on the right track. can anyone guide me
I think you are looking for one of these:
$data = file_get_contents('http://example.com/data.php');
This will fetch the raw data from the specified URL, and store the contents to the variable.
You may then need to implement your own code to actually parse the raw data, depending on your encoding type.

Categories