echo $_POST["name"]; //returns the value a user typed into the "name" field
I would like to be able to also return the text of the key. In this example, I want to return the text "name". Can I do this?
$_POST is just a normal associative array so you can also loop over the entire thing like this:
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
Check out the array_keys() function assuming this is PHP.
http://us2.php.net/array_keys
#Tim: there was a ) missing. so it should be:
while( list( $field, $value ) = each( $_POST )) {
echo "<p>" . $field . " = " . $value . "</p>\n";
}
while( list( $field, $value ) = each( $_POST )) {
echo "<p>" . $field . " = " . $value . "</p>\n";
}
array_keys($_POST)
Manual
foreach($_POST as $rvar)
{
$rvarkey=key($_POST)
$$rvarkey=mysql_real_escape_string($rvar);
}
it creates variables having the name of the request parameters which is pretty awesome.
Related
Hey i'm currently trying to get randomuser.me's api to work so that i can get users from there and put them into my Mysql Table ´users´ but some how i can't make it happen. And then hows the best way to put it into the mysql i know i can just place it in the foreach but maybe there is better ways?
I've tried to put it in a foreach but i always end up with the results*:
gender = female
name = Array
location = Array (*how to i get the json under location?)
email = francesca.ozturk#example.com
login = Array
dob = Array
registered = Array
phone = 0906-4547969
cell = 0172-3081101
id = Array
picture = Array
nat = DE
I use this as code currently
<?php
$user = json_decode(file_get_contents('https://randomuser.me/api/?
results=20&gender=female&nat=de'), true);
foreach($user as $key => $arrays){
foreach($arrays as $array){
foreach($array as $key => $value ){
echo $key . " = " . $value . "<br />";
}
echo "<br />";
}
echo "<br />";
}
?>
You can use something like this:
<?php
$user = json_decode(
file_get_contents(
'https://randomuser.me/api/?results=20&gender=female&nat=de'
),
true);
function printArray( $entryKey, $entry ){
if ( gettype( $entry ) != "array" ){
echo $entryKey . " = " . $entry . "<br />";
} else {
foreach ( $entry as $key => $value ){
if ( gettype( $value) == "array" ){
printArray( $entryKey, $value );
}
// 1) Do not display parent key:
// echo $key . " = " . $value . "<br />";
// 2) Display parent key:
echo $entryKey . "." . $key . " = " . $value . "<br />";
}
}
}
foreach( $user['results'] as $key => $arrays ){
foreach( $arrays as $innerkey => $innervalue ){
printArray( $innerkey, $innervalue );
}
echo "<br>";
}
?>
You can try case 1) or 2), and have the result properties displayed along with the parent key or without.
For MySQL insertion of multiple rows, you can check out this post:
insert multiple rows via a php array into mysql
The following code returns the field names of a result set. I also want it to return the values. How can I do this?
while ($row = mysqli_fetch_assoc($result)) {
foreach( $row as $field => $name) {
echo $field."<br>";
}
}
If we assume that your array looks like this:
$row["first_name"] = "John";
$row["last_name"] = "Doe";
$row["username"] = "john.doe";
Using this code:
while ($row = mysqli_fetch_assoc($result)) {
foreach( $row as $field => $value) {
echo "{$field} - {$value}<br>";
}
}
You will get an output like this:
first_name - John
last_name - Doe
username - john.doe
When you iterate through an array, using => operator, you are iterating in a "key-value" pair style. Every iteration holds the key and value as you can see.
Take a look at foreach for more information.
you are getting the value in $name variable
foreach( $row as $field => $name) {
echo $field . " = " . $name . "<br>";
}
I am in need of a PHP-program which will take all values from a HTML-link (with GET), regardless of how much one changes things in the address-bar, and then prints it out on the page. The code I have now is this:
HTML:
Link
PHP:
<?php
Echo "Value1: "; Echo $_GET["a"]; Echo "\n";
Echo "Value2: "; Echo $_GET["b"]; Echo "\n";
Echo "Value3: "; Echo $_GET ["c"];
?>
It works as intended for just these values, but it cannot cope with, for example, another variable being added in the address bar. I need some kind of PHP-function which can look for all kinds of variables through the GET-function.
Any help is appreciated!
Use a loop (modify as needed):
foreach ($_GET as $num => $value)
{
echo 'Value ' . $num . ': ' . htmlentities($value). "<br>" . PHP_EOL;
}
References:
Control structures
htmlentities()
Something like this:
<?php
foreach ($_GET as $key => $value) {
echo htmlspecialchars($key) . " - " . htmlspecialchars($value) . "<br>";
}
?>
<?php
print_r($_GET);
?>
Use a foreach loop
foreach ($_GET as $key => $val){
echo htmlentities($key).": ".htmlentities($val)."<br />\n";
}
or simply -
echo "<pre>".print_r($_GET,1)."</pre>";
To get all values and their corresponding names:
foreach($_GET as $key => $value)
{
echo $key . ' - ' . $value;
}
$my_get = array(); // store $_GET vars
if ('GET' == $_SERVER['REQUEST_METHOD']) // check that request method is "get"
foreach ($_GET as $key => $val)
{
$my_get[$key] = $val; // store
}
output $my_get:
array (
'a' => 'value1',
'b' => 'value2',
'c' => 'value3',
)
Since $_GET is nothing but an array, you can use for each loop to get the elements of it.
you get all $_GET values using print_r($GET) ...
otherwise you could to something like this
foreach(array_keys($_GET) as $key) {
echo htmlspecialchars($_GET[$key]);
}
Later Edit : took into account the security issue exposed in the comment
using this method is good because you know what kind of variable you have then you could do something cool like :
if ($key == 'a') do this
if ($key == 'b' && $_GET[$key] == 'bar') do that
I just wrote the following code:
<?php
$email = checkPost("email");
$username = checkPost("username");
$firstname = checkPost("firstname");
$lastname = checkPost("lastname");
$zipcode = checkPost("zipcode");
echo("EMAIL: ".$email." USERENAME: ".$username);
function checkPost($formData) {
if (isset($_POST[$formData])) {
return $_POST[$formData];
}
}
?>
What I'd like to do is eliminate all those calls to checkPost() at the top. This code below doesn't require any knowledge of the fields in the form that submits to it, it just loops through all of the fields and spits out their values.
<?php
// loop through every form field
while( list( $field, $value ) = each( $_POST )) {
// display values
if( is_array( $value )) {
// if checkbox (or other multiple value fields)
while( list( $arrayField, $arrayValue ) = each( $value ) {
echo "<p>" . $arrayValue . "</p>\n";
}
} else {
echo "<p>" . $value . "</p>\n";
}
}
?>
I want to modify this code such that variables like $email, etc would be created and assigned values on the fly. Like say you run this on a form that has "email" and "name". You won't have to give php a variable name $email or $name. It will just loop through and for "email" create and fill a variable named $email and so on and so forth.
Am I dreaming?
foreach ($_POST as $key=>$value) {
echo '<p>', htmlspecialchars($key), ' = ', htmlspecialchars($value), '</p>';
}
I think that's what you're looking for anyway. Basically, we just loop through all $_POST elements and display them. You can of course do whatever you need with them, within the loop.
You could use...
extract($_POST, EXTR_SKIP);
...but I don't recommend it. This will unpack the array's keys into the scope it is in. I've set it to not overwrite existing variables.
You should explicitly define your variables.
I have an array $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);. How can i get (echo) all names and corresponding ages in a single instance (like foreach $value as $value)?? The array may have more data than shown here.
foreach ($ages as $name => $age) {
echo "$name = $age\n";
}
u have a lot options
like
print_r
print_r($array)
var_dump , foreach
print_r($array);
or
var_dump($array)
foreach ($ages as $key => $value) {
echo $key . "'s age is " . $value . "<br />";
}