I have a problem showing the quantity for each article I check through a form which is getting data from a MySQL database I made.
Here is my code:
<?php foreach ($results_articles as $input) : ?>
<input type="text" name="<?php echo $input->id_article; ?>_txtbox[]" style="text-align: center; width:30px;"></input>
<input
type="checkbox"
name="checked_articles[]"
value="<?php echo $input->id_article; ?>">
<?php echo "(".
($input->ref_article).")".
($input->nom_article)." (".
($input->prix_article)." €)"; ?><br>
<?php endforeach; ?>
So, the "checked_articles" are showing up properly on my next page; but I really don't know how to show the quantity for each article checked.
I would like help from you !
Thanks! If you have any other infos, I'll reply of course!
I presume "quantity" is put in the text input, if so it should be accessible using:
foreach ($_POST['checked_articles'] as $checked_id)
{
$quantity = $_POST[$checked_id . '_txtbox'];
}
However, since the txtbox input is named to become an array, $quantity would result in an array. Maybe that is not what you intend. Then you could do it this way instead:
<input type="text" name="txtbox[<?php echo $input->id_article; ?>]" style="text-align: center; width:30px;" />
Having that, the textbox would be accessible using:
$_POST['txtbox'][$checked_id];
Related
So I have a categories table in my database and each category has a certain amount of steps assigned to it. I want to have all of the categories displayed as tabs titled with the category name so I need this from the database. I then have a form inside the tabs to insert the amount of steps needed for that category, but this form is looping... I need it inside the foreach to get the category id but without the form looping... Hope that makes sense..
Here is my code:
<?php foreach($categories as $category){ ?>
<div id="<?php echo $category->category ?>" class="tab">
<form id="maxSteps" method="POST" name="maxSteps" action="<?php $PHP_SELF; ?>" enctype="multipart/form-data">
<label for="maxSteps">Amount of steps in form: </label><input style="width:50px;" id="maxSteps" type="text" name="maxSteps" />
<input type="hidden" name="catId" value="<?php echo $category->cat_id; ?>" />
<input type="Submit" value="Go" name="maxStepsSubmit" />
</form>
<table id="amountOfStepsForm">
<?php $maxStepsById = $wpdb->get_results( "SELECT * FROM metal_work_max_steps WHERE cat_id = '$category->cat_id'" ); ?>
<?php foreach($maxStepsById as $maxStep){ ?>
<tr><td id="maxStepsRow<?php echo $maxStep->id; ?>"><?php echo "<p>Amount of steps in form is: <b>".$maxStep->steps."</b>" ?></td><td id="editRow<?php echo $maxStep->id; ?>"><a id="<?php echo $maxStep->id; ?>" class='edit'>Edit</a></td></tr>
<input type="hidden" name="catId" value="<?php echo $category->cat_id; ?>" id="catId<?php echo $maxStep->id; ?>" />
<?php } ?>
</table>
</div>
<?php } ?>
Regards
The best thing you can do is to run only one query and retrieve all your data inside an associative array. Then just loop trough this data structure.
The query can be done using a JOIN statement, in order to retrieve the information regarding the max steps for each category.
1- put the Form tag above the for loop
2- <input type="hidden" name="catId" val... need to have dynamic name: name="catId_<?php echo $category->cat_id;?>"
3- the button Submit label GO, and the colse tag of Form need to be out of the for loop (below)
so all the fields will be in 1 form,
and you will have 1 submit button, if that what you need,
and the field names are not the same, so on submit you can read all of them
these are general comments, i think you need to pay attention to.
So i've created an administration page that creates X-number of forms based on how many users we have in our database, each of which have a submit button next to them to submit changes to the dates we have in our DBs. My problem is that when I want to get the value of what gets posted I can't extract exactly what I need from what gets posted. It gets saved into an array called and when I print_r the array I get exactly what I want, which is:
[1] => "whatever date they typed in"
(obviously the 1 changes depending on which item they changed the date of)
I need be able to query my datebase by:
UPDATE users SET subdate="whatever they typed in" WHERE id="the array reference number"
I know exactly what I need to do, I'm just not as familiar with SQL as i'd like to be, so any help would be greatly appreciated. Thanks in advance.
Code for reference:
<div class="form-section grid12" id="changedates">
<h1>Change Dates</h1>
<?php
$query = mysql_query("SELECT * FROM users WHERE admin='y'");
?>
<table>
<?php
while($row = mysql_fetch_assoc($query)) {
?>
<tr>
<td>
<h5><?php echo $row['displayname'];?></h5>
</td>
<td>
<form action="" method="POST">
<input type="text" name="subdate[<? echo $row['id'] ?>]" value="<?php echo $row['submissiondate'];?>">
<input type="text" name="nextupdate[<? echo $row['id'] ?>]" value="<?php echo $row['nextupdate'];?>">
</td>
<td>
<input type="submit" value="Set Date" name="setdate">
</form>
</td>
<?php
}
?>
</table>
</div>
You could use foreach...
foreach ($_POST[nextupdate] as $rowId => $time)
{
// do db update
}
Edit: Just realised you have more than one input per form.
Why not name each input with an array name:
<input type="text" name="form_data[<?= $row_id ?>][subdate]">
<input type="text" name="form_data[<?= $row_id ?>][nextupdate]">
In PHP:
foreach ($_POST[form_data] as $rowId => $values)
{
$subdate = $values[subdate];
$nextupdate = $values[nextupdate];
// do SQL stuff
}
I have a form with rows which are populated from a table. Each row has a "checkbox" which the user can check or not.
When the form is submitted I want to be able to read which checkbox have been selected and insert the result in to a data table.
My code so far
FORM:
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table
<?php do { ?>
<tr>
<td>input type="text" name="InspectRoomNo" value="<?php print $row_InspectItems['AuditItemNo']; ?>"></td>
<td>php echo $row_InspectItems['AuditItem']; ?>td>
<td>input name="check[]" type="checkbox" ></td>
</tr>
<?php } while ($row_InspectItems = mysql_fetch_assoc($InspectItems)); ?>
<input type="submit" value="Insert record">
</table>
The insert: fetchs $Items from table
while($row = mysql_fetch_assoc($Items))
{
$array[] = $row['AuditItem'];
}
foreach($array as $id) {
$AuditItemID = mysql_real_escape_string($id);
if(isset($_POST['check'])){
$Checked = mysql_real_escape_string($_POST['check'][$row]);
}
}
The problem I am having is the returned values for all the checkbox is true, even if a checkbox was not selected.
Can anyone help me sort this issue.
Many thanks.
Do it like this:
if(!empty($_POST['check'])) {
foreach($_POST['check'] as $check) {
echo $check;
}
}
You should put the item id inside the checkbox name:
<td><input name="check[<?= $row_InspectItems['AuditItem']; ?>]" type="checkbox" /></td>
Then, you can simply iterate over it:
foreach ($_POST['check'] as $id => $value) {
// do stuff with your database
}
I'm assuming than whomever runs this script is trusted, because it would be easy to forge the list of ids; make sure the current user has permissions to update those records.
What is happening, is that only selected checkboxes get sent to the server, so you will see that your $_POST['check'] array (this is an array!) is smaller than the number of items you have displayed on the screen.
You should add your ID's so that you know what checkboxes got checked and adapt your php processing code to handle an array instead of a single value.
You are also overwriting your InspectRoomNo every row, so you should use an array there as well.
The form side would look something like:
<td><input type="text" name="InspectRoomNo[<?php echo row_InspectItems['AuditItemNo']; ?>]" value="<?php print row_InspectItems['AuditItemNo']; ?>"></td>
<td><?php echo $row_InspectItems['AuditItem']; ?></td>
<td><input name="check[<?php echo row_InspectItems['AuditItemNo']; ?>]" type="checkbox" ></td>
I have a loop below that is showing a quantity box and includes a hidden field containing the product name.
I want these to be tied together so if out of 100 inputs the user changes the quantity of input 90, then and want the hidden field input 90 to be tied to it.
This then gives me the quantity and the product name for items that have more than zero.
<?php if(get_field('sizes')) {
while(the_repeater_field('sizes')) { ?>
<input type="text" name="quantity[]" value="0"> <?php the_title(); ?>
<input type="hidden" name="product[]" value="<?php the_title(); ?>">
<?php } } ?>
I want to tie these two together so it would echo out the following:
1 x Product One
10 x Product Three
20 x Product Eight
How can I output the quantity AND product name only if the quantity is more than zero?
This is the actual code used:
<?php if(get_field('sizes')) { ?>
<?php while(the_repeater_field('sizes')) { ?>
<tr>
<td width="150"><p><?php echo the_sub_field('size'); ?></p></td>
<td width="30" align="right">
<p>
<input type="text" class="quantity" name="quantity[]" style="width:15px;text-align:center!IMPORTANT;margin-left:10px;" value="0">
<input type="hidden" class="productinput" name="product[]" value="<?php echo the_title(); ?> - <?php echo the_sub_field('size'); ?>"></td>
</p>
</td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td width="150"><p>Quantity</p></td>
<td width="30" align="right">
<p>
<input type="text" class="quantity" name="quantity[]" style="width:15px;text-align:center!IMPORTANT;margin-left:10px;" value="0"><?php echo the_sub_field('size'); ?>
<input type="hidden" class="productinput" name="product[]" value="<?php echo the_title(); ?>">
</p>
</td>
</tr>
<?php } ?>
This is then creating the code ready to be output in an email:
$quantities = array_combine($_POST['product'], $_POST['quantity']);
foreach ($quantities as $product => $quantity) {
if ($quantity > 0) {
$productresults = "$quantity x $product";
}
}
This is the page I'm working on. If you click on "Get Quotation", the second step is the code above.
#Sn0opy
foreach($_POST['quantity'] as $check) {
if($check > 0) {
$quantityresults .= $check."\n";
}
}
echo $quantityresults;
You obviously need to iterate over both arrays in tandem so that you can see if a product's quantity is nonzero in order to decide if it should be displayed.
In general foreach is an awkward tool for this job, and the way to go is with a for loop and indexing into both arrays using the same counter. However, in this specific case you can easily transform your two arrays into one where the keys are product names and the quantities are the values using array_combine:
$quantities = array_combine($_POST['product'], $_POST['quantity']);
You can then easily iterate with foreach:
foreach ($quantities as $product => $quantity) {
if ($quantity > 0) {
echo "$quantity x $product<br>";
}
}
I recommend using the product ID in the quantity array like this:
<input type="text" name="quantity[<?php the_title(); ?>][]" value="0">
Ofc, this is not the answer you were looking for, but an alternate version which should also work.
Rob, I see you have a good answer but you might like to be aware of a significant issue.
By posting independent quantities[] and products[], then you are relying on the two serializations being conformal with each other - ie. that both are serialized in DOM order - hence that the indexes of $_POST['quantity'] and $_POST['product'] correspond element-by-element. For me, this is not a completely safe assumption - see the selected answer here.
It would be far safer, and more conventional, to have one <input> field per product, named with a representation of product-id and a value representing quantity. Thus, product-ids and their values are guaranteed to correspond.
Client-side and server-side code would need to be reviewed accordingly.
I am setting up a form using this PHP that loops through all records a user may have:
<?php foreach ($items as $row): ?>
<tr>
<td>
<?php echo form_hidden('id', $row->id); ?>
</td>
<td>
<?php echo '<strong>' . $row->name . '</strong>'; ?>
</td>
<td>
<?php echo form_input('number', $number); ?>
</td>
<td>
<?php echo form_input('registry', $registry); ?>
</td>
<td>
<?php echo form_checkbox('OK', $ok, $ok); ?>
</td>
</tr>
<?php endforeach; ?>
This gives me a form with the following look:
The idea here is that each row belongs to a unique ID/row in the database, and I would like to allow the user to edit all on the same page/form, using a single submit button.
What would be the best way of implementing this?
When this data is submitted, there should be a way of looping through each packet of information (from each user) in my controller. Would this be done via ajax/json?
This does not use codeigntier, but you should be familiar with the general technique before attempting to use CI to shortcut this process. Codeigniter will help you with rendering the form elements, performing validation, escaping your input and performing your query - but it will only help you (do anything) if you understand the basic principles involved. Hope this helps
MARKUP
<form action="/process.php">
<div>
<h2>GORDON</h2>
<input type="text" name="user[1][number]" /> <!-- The number corresponds to the row id -->
<input type="text" name="user[1][registry]" />
<input type="checkbox" name="user[1][ok]" value="1" />
</div>
<div>
<h2>ANDY</h2>
<input type="text" name="user[242][number]" />
<input type="text" name="user[242][registry]" />
<input type="checkbox" name="user[242][ok]" value="1" />
</div>
<div>
<h2>STEWART</h2>
<input type="text" name="user[11][number]" />
<input type="text" name="user[11][registry]" />
<input type="checkbox" name="user[11][ok]" value="1" />
</div>
<input type="submit" />
PHP
$users = $_REQUEST['user'];
foreach ($users as $rowId => $info){
// YOU SHOULD MAKE SURE TO CLEAN YOUR INPUT - THIS IS A GUESS AT WHAT YOUR DATA TYPES MIGHT BE
$id = (int) $rowId;
$number = (int) $info['number'];
$registry = mysql_real_escape_string($info['registry']);
$ok = (int) ($info['ok']);
$q = "UPDATE user SET number = $number, registry = '$registry', ok = $ok WHERE id = $id";
mysql_query($q);
// You may want to check that the above query was sucessful and log any errors etc.
}
There's no need to use ajax mate.
For each put a hidden input with the ID of the row in this format:
<input type="hidden" name="id[<?= $row->id ?>]" value="<?= $row->id ?>" ?>
Do the same for each element in the tr, i.e. name them as
name="number[<?= $row->$id ?>]"
name="registry[<?=$row->$id ?>]"
name="ok[<?=$row->$id ?>]"
and once you post the FORM you can iterate each row with:
foreach ($_POST['id'] as $key => $value) {
echo $_POST['name'][$key];
}
You need to set up input-names as array-names, so you will send the whole form and may iterate over the entries.
e.g.
<?php
echo form_input('userdata[' . $row->id . '][number]', $number);
?>
which would possibly create an
<input name="userdata[1][number]" />
(I don't know where those form-functions came from…)
This will result in an array $_POST['userdata'] which may be iterated via:
foreach($_POST['userdata'] as $userId => $userInputFields)
{
$user = new User($userId);
$user->number = $userInputFields['number'];
// …
}