php multiple dropdown menu with same name can't get the value - php

okay, im new in this site also new in php and can't get the logic on this,
i have a product page that shows the name, quantity and an add to cart button in each row of product
i made this just cutted of some code
while($showProducts = mysql_fetch_array($products))
{
$currenQuantity = $showProducts['current_quantity'];
$prodid = $showProducts['product_id'];
echo"<select name='quan'>";
for ($x=0;$x<=$currenQuantity;$x++)
{
if($currenQuantity != 0)
{
echo "<option value=$x> $x </option>";
}
}
echo"</select><br/>";
}
now the problem is every time i tried to get the value by using $_POST['quan'] the value that i always get is the default value 1 even i select a different value of quantity of a certain product, and i'm blanked with ideas.

You can't use the same name for an input/select field in a form.
You have to specify a diffrent name or create an indexed array:
<select name="quan[$prodid]">
You can acces it via
$_POST['quan'][$prodid]

Just a wild guess: has each of your product rows a select named 'quan'?
Then you're getting the last select named 'quan' in your POST data.
Prefix each of the quantities selects with name of the product, or some kind of a (scrambled) id.
Additionally:
If you don't want to have a 0 in your select, don't start the for loop with 0
for ($x=1;$x<=$currenQuantity;$x++)
if the current quantity is 0, the for loop doesn't get executed.

If you have more selectboxes with the same name (quan) in one form, you are getting value of the last one.
You should change the selectbox definition to:
echo"<select name='quan[" . $prodid . "]'>";
then you can access the values using:
$_POST["quan"][$some_product_id]
$_POST["quan"][$another_product_id]

Related

HTML/PHP Checkbox Foreach Loop

I'm creating a control panel and having check boxes like the following
The value of $myboxid is the checkbox id, example: cb1 and the name is just the value eg Name of place: London. It grabs this information from my database
<input id='".$myboxid."' name='cplace[]' checked type='checkbox' value='".$box."'><label for='".$myboxid."'>".$boxname."</label>
What I'm trying to do is check which box is selected out of the multi box selection. I can get which boxes are selected and for that to output the value, my problem is I also need it to tell me which boxes are not selected.
My Form method is POST, my php back end is the following
$lname=$_POST['cplace'];
if(isset($_POST['cplace'])) {
foreach($lname as $place){
echo $place." CHECKED <BR>";
}
}
I'm trying to get this to output the checkbed boxes and the ones which are not selected.
Thanks for the help!
Well you can check the value of the checkbox in the POST array.
Simply print all the checkbox from the db and then check the value
In one line you can do like this
Inside your db loop
$isChecked=(in_array($_POST['cplace'], $box)) ? "checked" : "";
echo "<input id='".$myboxid."' name='cplace[]' ".$isChecked." type='checkbox' value='".$box."'><label for='".$myboxid."'>".$boxname."</label>";

How to insert Multiple records into mysql using php

i am fetching different record from the database but using single quantity field trying to insert multiple records into MYSQL but every time for loop or any other loop overriding single value in all rows(inserting single first value in every field), badly stuck. kindly suggest what kind of appropriate steps should be done.
<input type="text" name="qty[]" id="qtyid" style="width:40px;">
<?php
if(isset($_POST["update"])) {
$usersCount=count($_POST['qty']);
$qtys=implode(",",$_POST['qty']);
for($i=0;$i<$usersCount;$i++) {
$query="UPDATE cart set qty='".$qtys."' WHERE prodid='$cartid'";
$dbh->query($query);
echo $qtys;
}
}
$total=$total*$qtys;
?>
Your "question" is unclear. I read it couple times nad i still dont know what is your problem.
Anyway. I'll try to explain what your code do in case it would help you to understand where the problem is.
//if form was submited and input of name "update" was in
if(isset($_POST["update"])) {
// count array created by inputs of name="qty[]"
$usersCount=count($_POST['qty']);
// make a string from that array, separating the keys by a "," char
$qtys=implode(",",$_POST['qty']);
// loop as many times as number of elements in that array = <input name="qty[]"> you had in submited form
for($i=0;$i<$usersCount;$i++) {
// and finally, do always the same for each loop repeat
$query="UPDATE cart set qty='".$qtys."' WHERE prodid='$cartid'";
$dbh->query($query);
echo $qtys;
}
}
So in conclusion your loop is doing always the same - every each repeat:
`$query="UPDATE cart set qty='".$qtys."' WHERE prodid='$cartid'";`
Basically to do that you would't need to have loop.
Because in that query you change value of column qty wherever column prodid is equal to something

After I display all the elements in a database, how do I rearrange them?

I have a textbox and a button that puts whatever you just typed into a database, then I have this to display all those things, along with a delete button and a "top" button
<?php
$resultset2 = $db3->query('SELECT * FROM ask');
if($resultset2->num_rows != 0) {
while ($rows = $resultset2->fetch_assoc()) {
$question_enter = $rows['question'];
$id = $rows['id'];
echo "<form action='' method='post'><input type='hidden' value='".$id."'
name='the_id'><div><p>$question_enter<input type='submit' value='Delete'
name='deletedefaultnote'><input type='submit' value='Top' name='putontop'>
</p></div></form>";
}
} else {
echo 'no results.';
}
?>
I have the delete button working, but I can't figure out how to get the "top" button to put an element on the top of the list that's being displayed. How should I do this?
If it's relevant, say item1 and item2 are on the bottom of a long list, if I put item1 on top of the list, then put item2 on top, item2 would be on top and item1 would be second on the list. I don't want the elements to return to their original position if something else goes on top.
To store the new ordering into database you have to update all positions. One simple approach would be to use a "position" column like sym mentioned.
With an position-column beginning with 1 (0 is the "guardian") you could then
update the "new top" item and set the position to 0 (guardian) after that you would have to update ALL items/rows and set the position to old_position+1.
As you can see this approach is heavy on queries so I would recommend not to update the table every time you click on your "top" button and instead order the items on frontend with something like jQuery (what Kuya) said and after you're done re-ordering your list save it in one big request with the method explained above.
UPDATE: To update the rows in your database you can also use a single query when so following conditions are met:
column position starts at 1 and is NOT unique
the new top position item's position was set to 0
Then you can simply do UPDATE tablename SET position = position + 1. The database will then automatically increment all position values (and position 0, the guardian will be available again).

Populate selected items in multi select list from URL

I have a list that allows multiple selections to be made and then submitted. What I now need to do is populate the select box so that the values submitted remain in the following page.
I'm passing the variable in the url and can list them using the following:
foreach ($_GET['sector'] as $selectedOption)
My thinking is to store the results in an array and then when the list is created loop through with the following:
if (in_array($optionvalue, $selectedOption))
{
echo "<option value='".$optionvalue."' selected='selected>".$optionvalue."</option>";
}
else
{
echo "<option value='".$optionvalue."'>".$optionvalue."</option>";
}
The above works but only for whatever the last selected item is.
Is this the correct way to do this or is there a simpler method?

How can I retrieve checkbox array values when the checkbox name is dynamically created?

I am dynamically creating forms based on values in a database. Each form element corresponds to a specific database entry, which includes the name, label, type, and (depending upon the type) possible values to be displayed with the form element.
The name value for each form element is set to the identity column value of the corresponding database entry.
I'm running into a problem when I am working with checkboxes, though. I'm trying to retrieve the array of selected values, but I am unable to retrieve more than one. I believe that this is because I am not properly setting the checkbox names to an array, but I am not certain.
Here is where I am generating the checkbox tags:
$answers = explode(',',$answerKey);
for($i=0; $i < count($answers); $i++) {
$questionTag .= "<INPUT TYPE='checkbox' name='$id' value='$answers[$i]' />$answers[$i]";
}
Yet when I post back my results, the results of $_POST["$id"] only returns the last value in the checkbox list.
Any suggestions would be appreciated!
$questionTag .= "<INPUT TYPE='checkbox' name='".$id."[]' value='$answers[$i]' />$answers[$i]";

Categories