html array index name to variable - php

I have a form such as the below:
<form name="basketSelectionForm" action="processBasket.php" method="POST">
<div id="tabs-1">
<table cellpadding="10" cellspacing="10" width="inherit">
<tr>
<td><img alt="itemNameb" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="0" id="itemName" name="basket[itemName]" type="text" style="width:40px;"/> </td>
<td><img alt="itemName" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="0" id="itemName" name="basket[itemName]" type="text" style="width:40px;"/></td>
Now when I go to second page to look at the entries of the array, I do this:
<?php
$itemsBasket = array( );
$itemsBasket = $_POST['basket'];
echo "<h1>The Items Are...</h1><br>";
//print_r($itemsBasket);
foreach ($itemsBasket as $value)
{
if($value > 0){
echo $value . "<br>";
}
}
?>
This will print the value at the indexes of the array...but I need to store the name of the index so lets say the item is chocolate and value of 12. I want to extract that index name from array to store it in variable and then assign value to that variable...
Any way I can do that? right now I get only the value while iterating...
Thanks for help and sorry if question isn't clear I will help explain better if so...
UPDATE: this is the unexpected output....
whitethoab: Array woolthoab: 22 shemag: 22 undershirt: 1 serwalthoab:
22 socks: 12
and this is the definition of the element showing as two dimensional array...
<td><img alt="White Thoab" src="images/whitethoub.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="0" id="whitethoab" name="basket[whitethoab]" type="text" style="width:40px;"/> </td>

Something like:
<form name="basketSelectionForm" action="processBasket.php" method="POST">
<div id="tabs-1">
<table cellpadding="10" cellspacing="10" width="inherit">
<tr>
<td><img alt="itemNameb" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="12" id="itemName" name="basket[chocolate]" type="text" style="width:40px;"/> </td>
<td><img alt="itemName" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="9" id="itemName" name="basket[onions]" type="text" style="width:40px;"/></td>
...and...
<?php
echo "<h1>The Items Are...</h1><br>";
foreach ($_POST['basket'] as $name => $value)
{
if($value > 0){
echo $name . ": " . $value . "<br>";
}
}
/* Output:
chocolate: 12
onions: 9
*/
?>
?

Not sure, but i think you want this
foreach ($itemsBasket as $key => $value)
{
if($value > 0){
echo $key. "<br>\n"
echo $value . "<br>\n";
}
}

Related

Multi input values using foreach and calculating values

<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
when i print using: var_dump($_GET["own_pools"]); I get
array(4) {
[0]=> string(1) "5"
[1]=> string(3) "300"
[2]=> string(3) "280"
[3]=> string(2) "50"
}
the parameters in the link are showing correctly
index?own_pools[]=5&own_pools[]=300&own_pools[]=280&own_pools[]=50&visitor_expect=100000000&ticket_price=15&submit_calc=Calculate
but after that the values did not get the right data back in the form and shown like this:
How can I get it calculated and get the values correctly from the input and returned the entered values back to the input value.
I have searched and read but couldn't find the answer...
I have the following code
<?php
$ticket_price=0;
$visitor_expect=0;
$total_value=0;
$own_pools=0;
$pool_result=0;
if(isset($_GET['submit_calc']) && isset($_GET['own_pools'])) {
$ticket_price = $_GET['ticket_price'];
$visitor_expect =$_GET['visitor_expect'];
$own_pools=$_GET['own_pools'];
if(is_numeric($ticket_price) && is_numeric($visitor_expect)){
// pools calculations
$rev_visitors=((int)$_GET["visitor_expect"]* (int)$_GET["ticket_price"]);
$total_value=($rev_visitors*0.01)*30;
$pool_result = $total_value ;
}
}
?>
<form action="" method="get" >
<table>
<tr>
<?php
// Display headers in one row as colmun
$tds = '';
$sum_main_pools=0;
foreach( $calculations as $index =>$calculation ) {
?>
<th>
<?php echo $calculation['Calculation']['poolname']; ?>
</th>
<?php
// create TDs for the values
if ($calculation['Calculation']['poolname']<>"Regional Pool"){
$tds .= '<td>
<div class="input text" id="pool_calc">
' . $calculation['Calculation']['total_units'] . '
<input name="own_pools[]" type="text" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
</div>
</td>';
if(isset($_GET['own_pools'])){
$own_pools=(int)$_GET['own_pools'];
$global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
$sum_main_pools +=$global_calc;
}
} else {
$tds .= '<td>
' . $calculation['Calculation']['total_units'] . '
</td>';
}
}
var_dump($_GET["own_pools"]);
?>
</tr>
<tr>
<?php
// Printing the values
echo $tds;
?>
</tr>
</table>
<?php
$pool_result = $total_value + $sum_main_pools;
?>
<table>
<tr>
<td style="width: 30%">
<div class="input text" id="pool_calc">
Expected Visitors per day
<input name="visitor_expect" type="text" value="100000000" id="visitor_expect" maxlength="9" size="9" >
</div>
</td>
<td style="width: 30%">
<div class="input text" id="pool_calc">
Ticket Price
<input name="ticket_price" type="text" value="15" id="ticket_price" maxlength="2" size="3" >
</div>
</td>
</tr>
<tr >
<td style="width: 60%">
<div>
<label > <?php echo (isset($pool_result) ? $pool_result : "");?></label>
<input name="submit_calc" type="submit" value="Calculate" id="submit_calc">
</div>
</td>
</tr>
</table>
</form>
Solved it:
added in foreach a key in foreach
foreach( $calculations as $key =>$calculation ) {
and in input value added as array
<input name="own_pools[]" type="number" value="'.$own_pools.'" id="own_pools" maxlength="5" size="5" >
if(isset($_GET['own_pools'])){
$own_pools=(int)$_GET['own_pools'][$key];
$global_calc=($own_pools * (int)$calculation['Calculation']['total_units']);
$sum_main_pools +=$global_calc;
}

How can I use 4 different input-values in php and save form?

I don't have much experience with php that's why I hope someone can help me to solve the problem.
I am trying to extend my database with new files. I need to be able to type in four different values in my form and save the form with the typed in values. If I only use the first input-tag #inschriftennummer1, it works. My saved form shows me what I've typed in. But I need three more inputs in order to tag the document with 4 values. If I use the code like that, no input value is shown when I save the form.
Maybe someone has a solution :)
<body>
<form name="Formular" method="post" action="">
<table style="width: 100%;">
<tr>
<td style="width: 0%;">
</td>
<td style="width: 85%;">
<?php
if (isset($_GET["change"]))
{
$xml = simplexml_load_file("Inschriften/".$_GET["change"].".xml");
echo '<input type="hidden" name="inschriftennummerAlt" value="'.$xml- >Inschriftennummer.'"/>';
echo '<input type="hidden" name="change"/>';
}
else
{
$xml = simplexml_load_file("Inschriften/Null.xml");
}
/*Gibt zu einem String die jeweilige Hexadezimalform zurück*/
function strToHex($string)
{
$hex='';
for ($i=0; $i < strlen($string); $i++)
{
$hex .= dechex(ord($string[$i]));
}
return str_replace("da", "", $hex);
}
/*Gibt zu einer Hexadezimalform den jeweiligen String zurück*/
function hexToStr($hex)
{
$string='';
for ($i=0; $i <strlen($hex)-1; $i+=2)
{
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
?>
<div class="main">
<div class="meta">
<table>
<tbody>
<tr>
<td colspan="2">
<?php
if (strlen($_GET["inschriftennummer"] == 0))
{
// echo "Bitte Inschriftennummer überprüfen.";
}
?>
</td>
</tr>
<tr>
<td class="I"><span class="label">Inschriftennummer</span>:</td>
<?php echo '<input type="hidden" name="inschriftvorher" value="'.$xml- >Inschriftennummer.'">'; ?>
<td class="II">
<input name="inschriftennummer1" type="text" size="7" value="<?php echo str_replace("_", " ", $xml->Inschriftennummer);?>"/>
<input name="inschriftennummer2" type="text" size="7" value="<?php echo str_replace("_", " ", $xml->Inschriftennummer);?>"/>
<input name="inschriftennummer3" type="text" size="7" value="<?php echo str_replace("_", " ", $xml->Inschriftennummer);?>"/>
<input name="inschriftennummer4" type="text" size="7" value="<?php echo str_replace("_", " ", $xml->Inschriftennummer);?>"/>
</td>

php loop over array that has been sent from html form with the same variable name

this is my html code:
<input name="dependencies[]"
and in php i do this:
$dependencies = $_POST['dependencies'];
and when I do this:
print_r($dependencies);
I can see the values like this:
Array ( [0] => [1] => )
My question
I want to add each value from that array to another array:
I didn't know how to do that
I tried:
foreach ($dependencies as $number){
echo $number;
}
but nothing has been printed
Update
this is the html
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
and I can see the check boxes checked or not when I run the page
Update2
the whole code
<form action="../DB/addDependencies.php" method="post">
<input type="hidden" name="questionID" />
<table>
<tr>
<th>Porgugese Name</th>
<th>Englisn Name</th>
<th>Dependent</th>
</tr>
<?php
foreach ($questions as $question) {
?>
<tr>
<td>
<?php echo $question->ptName; ?>
</td>
<td>
<?php echo $question->enName; ?>
</td>
<td>
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
</td>
</tr>
<?php
}
?>
</table>
<input type="submit" value="Save" name="submit" />
</form>
You need to change your HTML to this:
<input name="dependencies[<?php $question->id; ?>]" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
Then to test if a checkbox if checked you just need to do something like
function isQuestionChecked($question_id) {
return isset($_POST['dependencies']) && isset($_POST['dependencies'][$question_id]);
}
try to use
foreach ($dependencies as $key=>$val){
var_dump($key);
var_dump ($val);
}
instead of
foreach ($dependencies as $number){
echo $number;
}
;-) hope that will bring some ideas to your mind in this case

php Looping input numeric with random value

Hy, can anyone help me, i have problem with my script..
if I input 4324 in input field nmber, i want the result like this :
4324
4342
4234
4243
4432
4423
3424
3442
3244
2434
2443
2344
this is my script :
<form name="a" method="POST" action="">
<table border="1" width="100%">
<tbody><tr>
<td height="38" align="center"><b>Number</b>
<input name="nmber" size="8.5" maxlength="4" type="text" value="<?php echo $_POST['nmber']; ?>"> <b>Buy</b>
<input name="buy" size="6" type="text" value="<?php echo $_POST['buy']; ?>"> <font color="#000000" size="2"><b>(x 1000)</b></font>
<input name="save" style="padding:7px;" value="Submit" type="submit">
</td>
</tr>
</tbody></table>
</form>
And this is my php script :
<?php
if(isset($_POST['save']))
{
$dataangka=$_POST['nmber'];
$databetnya=$_POST['buy'];
$rupiahkali=$databetnya*1000;
$dataangkasplit=str_split($dataangka);
$angka1=$dataangkasplit[0];
$angka2=$dataangkasplit[1];
$angka3=$dataangkasplit[2];
$angka4=$dataangkasplit[3];
$no=1;
$n=24;
for($i=1;$i<=$n;$i++)
{
?>
<tr align="center">
<td><?=$no?></td>
<td><input name="cek[<?=$i?>]" value="1" checked="checked" type="checkbox"></td>
<td><?php echo substr(str_shuffle("$dataangka"),0,$n); ?>
<input size="2" name="res[<?=$i?>]" value="<?php echo substr(str_shuffle("$dataangka"),0,$angka4); ?>" type="hidden">
</td>
<td><?=$rupiahkali?></b> <input size="2" name="bet[<?=$i?>]" value="<?=$rupiahkali?>" type="hidden"></td>
</tr>
<?php
$no++;
}
}
?>
I have already try with substr and str_shuffle but result not like what i want..
Please help me.. :(
Thank you so much..
You are trying to generate all permutation of length 4 using the string 4324.
The easiest way to generate all permutation (imho) is recursion. But it you can do it in iterative method too.
I would suggest you study the algorithm first and get a grip on recursion. A quick google search returned the following results
http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
How to print all permutations of String both iterative and Recursive way? VB.NET
Generate list of all possible permutations of a string
This does what you need:
function getCombinations(array $a)
{
switch (TRUE)
{
case !isset($a[1]):
return $a;
case !isset($a[2]):
return array(implode($a), implode(array_reverse($a)));
default:
$return = [];
foreach ($a as $k => $e)
{
$c = $a;
array_splice($c, $k, 1);
foreach (getCombinations($c) as $r)
{
$return[] = $e . $r;
}
}
return array_unique($return);
}
}
$s = '4324';
echo implode('<br>', getCombinations(str_split($s)));

arrange checkbox values in two columns

I have a form with check box values i want to arrange the checkbox in two columns instead of
one single long column.
How can i split it to two columns ?
Here is the code :
<form id="form" name="form" method="post" action="">
<table width="502" border="0">
<tr>
<td>
Name :
<input type="textbox" name="rcv_group_name" value="">
<input type="hidden" name="add" value="add" />
</td>
</tr>
<tr>
<td align="left">
<strong>HEADING</strong>
<?php
$q = "select * from Config_RCV";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) > 0)
{
$k=0;
while ($row = mysqli_fetch_array($r,MYSQLI_NUM))
{
?>
<br> <input type="checkbox" name ="rcv_val[]" value ="<? echo $row[0];?>" /> <? echo $row[1];?>
<?
$k++;
}
}
?>
</td>
</tr>
<tr>
<td align="right"><input type="submit" name="Submit" id="Submit" value="Submit" style="background-color:#999" />
< /td>
<td height="26" align="left"> </td>
<td> </td>
</tr>
</table>
<form>
add another table and make 2 columns by splitting 2'nd result:
if (mysqli_num_rows($r) > 0) {
$k=0;
echo '<table><tr>';
while ($row = mysqli_fetch_array($r,MYSQLI_NUM)) {
?>
<td><input type="checkbox" name ="rcv_val[]" value ="<?php echo $row[0];?>" /> <?php echo $row[1];?> </td>
<?php
$k++;
if($k%2 == 0){
echo '</tr><tr>';
}
}
echo '</table>';
}
use your $k
if ($k%2==0){
echo "<br />";
}
instead of the lone now
Using tables like above will probably be better for looks
You could just add a second column to your table:
echo "<tr>";
$i = 0;
while ($row = mysqli_fetch_array($r,MYSQLI_NUM)) {
if ($i++%2==0) {
echo "</tr><tr>";
}
echo "<td>".$CELL_CONTENT_HERE."</td>";
}
echo "</tr>";

Categories