Jump to content


Photo

Bracket Question

brackets php variables

  • Please log in to reply
7 replies to this topic

#1 cutielou22

cutielou22
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts
  • LocationUSA

Posted 19 April 2017 - 02:26 PM

I am working on a item helper script. It consists of users selecting from multiple radio buttons for multiple items and updating them all at once.

 

My question is the code below is used for each item the user has (used in "while ($stmt->fetch()){" statement") . With the way I have it set up, how will I make it so it updates the correct item for the right option? Also. how do I grab the bracket variable use_item[$id] ?

<tr width=\"100%\">
<td>
<p>$name</p>
</td>
<td>
<center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"keep\"$check_keep></center>
</td>
<td width=\"50\">
<center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"stock\"$check_stock></center>
</td>
<td width=\"50\">
<center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"discard\"$check_discard></center>
</td>
<td width=\"50\">
<center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"donate\"$check_donate></center>
</td>
<td width=\"50\">
<center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"gallery\"$check_gallery></center>
</td>
<td width=\"50\">
<center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"gallery3\"$check_gallery3></center>
</td>
<td width=\"50\">
<center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"deposit\"$check_deposit></center>
</td>
</tr>
 

 

The form takes them to the page that updates using the information they choose from above. A small snippet below.

if(isset($_POST['submit'])){
	
	$use = $_POST['use_item'];
	
	foreach($use as $use_item){
	
		$item = $_POST['item_id'];
                /////// UPDATING MYSQLI INSERTED HERE //////////////////

        }
}

Thank you for your time. Hopefully I posted enough information for your help.


Edited by cutielou22, 19 April 2017 - 02:27 PM.

Free music site to find new bands - http://musicworldanonymous.com | mandyjosmith.com | solpets.com


#2 dkub

dkub
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 19 April 2017 - 03:04 PM

What is $_POST['item_id']? I don't see that included in the form snippet above. What I do see, however, is an ID passed to use_item. $_POST['use_item'][3] would therefore be used to update the item with an ID of 3, no? See where I'm going with this?



#3 cutielou22

cutielou22
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts
  • LocationUSA

Posted 19 April 2017 - 03:49 PM

$_POST['item_id'] is there - it is just not shown in the top snippet.

<input type=\"hidden\" name=\"item_id\" value=\"$item_id\">

So for $_POST['use_item'][3] - the "3" is the variable in the [] brackets right? And this is how I will get the id.


Free music site to find new bands - http://musicworldanonymous.com | mandyjosmith.com | solpets.com


#4 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,169 posts
  • LocationVoorheesville NY

Posted 19 April 2017 - 03:53 PM

"And this is how I will get the id."

Ummm - You understand that you have to set that $id value before you echo out your input elements to have a value there? It doesn't just show up when you use your PHP code to retrieve the POST values.

Just making sure you know that since I didn't see any line setting a value and assumed that it was happening earlier in the snippet.
JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#5 Strider64

Strider64
  • Members
  • PipPipPip
  • Advanced Member
  • 247 posts
  • LocationA burb of Detroit, MI

Posted 19 April 2017 - 04:14 PM

I agree with ginerjm. I just wanted to add Radio Buttons can have only one value in the first place, so I don't even know why you are using an array? Unless all of the $id is going to be the same? An if that was the case there would be no need for an array for you are using Radio Buttons.

               <div class="radioBlock">
                    <input type="radio" id="radio1" name="reason" value="support" checked>
                    <label class="radioStyle" for="radio1">support</label>
                    <input type="radio" id="radio2" name="reason" value="advertise">
                    <label class="radioStyle" for="radio2">advertise</label>  
                    <input type="radio" id="radio3" name="reason" value="error">
                    <label class="radioStyle" for="radio3">Report a Bug</label>    
                </div>

However, I really don't understand what you are trying to do, so onward.....

 

An to use an array you would in a form you wouldn't use $id, you would do something like:

<input id="example" type="radio" name="use_item[id]" value="">

and this is how I retrieve it using php:

$data = filter_input(INPUT_POST, 'use_item', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY);

Edited by Strider64, 19 April 2017 - 04:23 PM.

 http://www.pepster.com  ||  "Insanity: doing the same thing over and over again and expecting different results." - Albert Einstein 


#6 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,169 posts
  • LocationVoorheesville NY

Posted 19 April 2017 - 04:22 PM

I don't like this code at all. A lot of bogus html and center tags and P tags inside td elements? WTF?
As for the brackets on name attributes - as was said, if it's a radio element then only one can be selected for a given name, so why all these buttons using values from different sources all having the same name? That means only one can be selected, unless the bracket syntax allows multiples. I wouldn't know - I don't use radios that much.
JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#7 Jacques1

Jacques1
  • Members
  • PipPipPip
  • Turtles all the way down
  • 4,224 posts

Posted 19 April 2017 - 04:25 PM

The approach makes sense if there are in fact multiple items. But then the item IDs come from the array indexes, not this strange hidden field:

foreach ($_POST['use_item'] as $item_id => $item_value)
{
    // update $item_id with $item_value
}


#8 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,169 posts
  • LocationVoorheesville NY

Posted 19 April 2017 - 04:31 PM

I agree Jacques that your sample makes sense, but the OP's use of an index as well as a disparate set of values from multiple folders is hard to follow. Normally a set of radio buttons pertains to a set of values that appear to be related. These don't, although they very well could be.

Adding the index to the name's array nomenclature just confuses everything. I wonder if the OP has ever read up on how to use? She obviously has never used an html table properly.
JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users