Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an array and I want to call the array by button, Also I wanted it keep calling the array every time when I click on button. Please correct the array if it has any issue.
my array is :
array (
"name" => __('name1','vbegy'),
"id" => $this->shortname."_menu_1",
"desc" => __('name2)','vbegy'),
"type" => "text",
"std" => __('name3','vbegy'),
),
array (
"name" => __('name4','vbegy'),
"id" => $this->shortname."_menu_1_url",
"desc" => __('name5)','vbegy'),
"type" => "text",
),
I think first thing you should do is to learn how to use HTML forms/buttons and use PHP to process them. Below is a simple form example.
In your HTML file, add
<form method="post" action="your-php-file.php">
<input type="text" name="text-name" value="">
<input type="submit" name="button-name" value="Submit">
</form>
In your PHP file (your-php-file.php), add
$array1 = array(
"name" => array('name1' => 'vbegy'),
"id" => $this->shortname."_menu_1",
"desc" => array('name2' => 'vbegy'),
"type" => "text",
"std" => array('name3','vbegy')
);
$array2 = array (
"name" => array('name4' => 'vbegy'),
"id" => $this->shortname."_menu_1_url",
"desc" => array('name5' => 'vbegy'),
"type" => "text"
);
if (isset($_POST['button-name'])) {
//use php to process your array data as you want
echo $array1['name']['name1']; //print vbegy
echo $array1['type']; //print text
echo $array2['desc']['name5']; //print vbegy
$std = $array1['std']['name3'];
echo $std; //print vbegy
echo "You clicked submit button!";
}
For detailed explanation, please use following links.
http://www.w3schools.com/php/php_forms.asp
http://www.w3schools.com/php/php_form_complete.asp
Hope this will help.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I have an array as below in php:
enter image description here
i need to loop through the above array to get video url and push it in the below array using a for loop:
$post_data = array(
'sources' => array(
'src' => "video_url",
'type' => "mp4"
)
);
for each element in the first array, it need to add a new element in the second array to get the below result
$post_data = array(
'sources' => array(
'src' => "video1.mp4",
'type' => "video/mp4"
),
'sources' => array(
'src' => "video2.mp4",
'type' => "video/mp4"
)
);
I tried using a loop as below
for ($x = 0; $x < count($allAds); $x++) {
}
and using array_push did not do the work.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I am trying to encode an associative array in php
$data = array(
"firstName" => $_POST["firstName"],
"lastName" => $_POST["lastName"],
"email" => $_POST["email"],
"telephone" => $_POST["telephone"]
);
however the output is in string format and not in array format.
string(98) "{"firstName":"rob","lastName":"shelford","email":"some#domain.co.uk","telephone":"01245454545"}"
The output needs to have the square brackets so the receiving server can read the data correctly.
Is there another syntax for the $data array I need to use?
EDIT 1
The offical PHP documentation https://www.php.net/manual/en/function.json-encode.php
states that
echo "Normal: ", json_encode($a), "\n";
can be used to output
Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
however when I try to use the "\n" flag, I receive an error
EDIT 2
The entire data I wish to convert
$data = array(
"applicants" => array(
"firstName" => $_POST["firstName"],
"lastName" => $_POST["lastName"],
"email" => $_POST["email"],
"telephone" => $_POST["telephone"]
),
"buyerType" => $_POST["buyerType"],
"organisationId" => "xxxxxxxxxx",
"introducerId" => "0",
"introducerBranchId" => "0",
"introducerNegotiatorId" => "0",
);
EDIT 3
I have made it work by passing this into json_encode() :
$data = array(
"applicants"=> [
array(
"email"=> $_POST["email"],
"firstName"=> $_POST["firstName"],
"lastName"=> $_POST["lastName"],
"telephone"=> $_POST["telephone"]
)
],
"buyerType" => $_POST["buyerType"],
"organisationId" => "xxxxxxxxxx",
);
I have also removed the unnecessary lines in the above example
EDIT 4
The actual output I was expecting, and actually got, after implementing EDIT3
string(138) "{"applicants":[{"email":"some#domain.co.uk","firstName":"rob","lastName":"test","telephone":"01200000000"}],"buyerType":"Purchase"}"
If you want nested array to be encoded with square brackets surrounded, just put it in an array in php.
$data['applicants'] = [$data['applicants']];
echo json_encode($data);
FYI indexed array [1,2] will be encoded to json list [1,2], and associative array ['x'=>1,'y'=>2] will be encoded to json object {"x":1,"y":1}.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am currently trying to create a register page but I am required to use an array of arrays to create a function to use to create a form. But I don't know how to do that exactly.
display_form(
array(
array(
"type" => "text",
"name" => "first_name",
"value" => "",
"label" => "First Name"
),
array(
"type" => "text",
"name" => "last_name",
"value" => "",
"label" => "Last Name"
),
array(
"type" => "email",
"name" => "email",
"value" => "",
"label" => "Email"
),
array(
"type" => "number",
"name" => "extension",
"value" => "",
"label" => "Extension"
)
)
);
This I need to make into a function to use it for making multiple forms on different pages but I don't understand how to call that function and actually display the form.
If anyone could provide some insight that will be greatly appreciated.
Not clear what you expect but maybe something like this will help you:
function display_form($fields) {
foreach ($fields as $field) {?>
<label><?=$field['label']?>
<input type="<?=$field['type']?>" name="<?=$field['name']?>" value="<?=$field['value']?>" />
</label>
<?php
}
}
Fiddle here
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am a beginner in PHP and I have a large amount of data in a multidimensional array
$result =
array
(
array(
array("name" => "Volvo", "price" => 2222, "model" =>18),
array("name" => "Tata", "price" => 2222, "model" =>18),
array("name" => "Asoka", "price" => 2222, "model" =>18),
),
array(
array("name" => "BMW", "price" => 2222, "model" =>17),
array("name" => "Benz", "price" => 2222, "model" =>17),
array("name" => "Maroti", "price" => 2222, "model" =>17),
),
array(
array("name" => "porse", "price" => 25648, "model" =>16),
array("name" => "farari", "price" => 25486, "model" =>16),
array("name" => "Volvo", "price" => 25422, "model" =>16),
),
);
I iterate through the array with a foreach loop, and print the model row ways,
foreach($result as $k => $datas){
echo $datas[$k]['model'].'-->';
foreach($datas as $key => $data){
//print_r($data);
echo $data['price'] ;
echo $data['name'].'-*-';
}
echo '<br>';
}
and when I run this code, I receive the following output:
18-->2222Volvo-*-2222Tata-*-2222Asoka-*-
17-->2222BMW-*-2222Benz-*-2222Maroti-*-
16-->25648porse-*-25486farari-*-25422Volvo-*-
However, I am trying to print column ways this array in html table, in a similar fashion to below:
18 --> 17 --> 16-->
2222-Volvo 2222-BMW 25648-porse
2222-Tata 2222-Benz 25486-farari
2222Asoka 2222-Maroti 25422-Volvo
Try this :
<div style='display:flex; flex-direction: row; flex-wrap: wrap;'>
<?php
foreach($result as $k => $datas){
echo "<div style='display:flex; flex-direction: column;'>";
echo "<div>" . $datas[$k]['model'] . "--></div>";
foreach($datas as $key => $data){
//print_r($data);
echo "<div>" . $data['price'] . "-" . $data['name'] . "</div>" ;
}
echo '</div>';
}
?>
</div>
The output is :
As you can see, the logic is pure HTML/CSS :
First I wrap your data inside a flex container with direction as row and wrap so if you have a lot of element they will go in the next row.
Now in your foreach, each element are wrapped inside an other flex div but with the direction as column : so each new div inside this one will be display under the previous one.
All the div inside are simple div, now your turn to make some css to center text, add padding or whatever
Is it what you are looking hope ? If you have question about flexbox check this : https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array $arr_name which prints out
{"name" => "tom", "age" => 10}, {"name" => "jack", "age" => 11}
how do i get it to print the desired output using php ?
Desired Output in JSON is as follows
{
"Students" : [ {"name" => "tom", "age" => 10}, {"name" => "jack", "age" => 11}
]
}
Sorry but i find this very very confusing and have spent the last 2 hrs trying to get "Students" key asssigned to have a value with the contents of $arr_name
PHP already has a built in function to convert an array into JSON:
json_encode
Here simple example given below like:
Put your array place of $put_your_array in loop:
$output=array();
foreach ($put_your_array as $key => $value) {
$output['Students'][]=array(
'name'=>$value['name'],
'age'=>$value['age']
);
}
echo json_encode($output);
And get output would you seem like json format...
the easiest way to do that, loop through the array then assign it to new array
Ex:
$new_array=array();
foreach(your array as $key => $val)
{
$new_array['Students'][] = $val;
}
return json_encode($new_array);