Jump to content

$_POST array and more (beginner)


pengo

Recommended Posts

Hi all, this is my first post so be nice :)

 

I have a mysql table named tab, with field_1 field_2 and some values inserted. I have file1.php that display dinamically values in field_1 as checkboxes, on submit I need file2.php to show: which values have been selected and the total sum of values in field_2 for each value in field_1 selected.

 

I have these problems:

1) file1.php: What should I consider when naming the checkboxes? [How does $_POST array work?] Something like:

 

$result = mysql_query("SELECT field_1 FROM tab");

 

while ($stuff = mysql_fetch_row($result))  {

echo "<tr><td>field_1: ", $stuff['0'], "</td><td> <input type='checkbox' name='", $stuff['0'], "' value='", $stuff['0'],"'> </td> </tr>";

}

 

doesn't look too right... do I want the checkbox name to be generated dinamically? and the value? I don't know how they are going to be stored in the $_POST array.

 

2) file2.php: How do I [do anything? :)]

I need to make a query that selects in both field_1 and field_2 just those rows that have been checked, then get the field_1 values displayed and field_2 values summed up. How do I get this?

 

As you can see my level is quite below zero :), hope you can help me understand how this works.

 

disclaimer (kind of): I'm actually far from my home country, yet no chance to easily access a library, no chance of finding english books, so I'm relying just on the net. I'll be happy to read any tutorial you would suggest me :) 

Link to comment
https://forums.phpfreaks.com/topic/123455-_post-array-and-more-beginner/
Share on other sites

you'll find that vicodin's answer is probably entirely useless for you.

 

when deciding how to name checkboxes, you need to decide how you want to process your form information.  checkbox elements are only sent through the form when they are checked; this contrasts from text inputs where the element is always sent, but if it is blank, it simply has no value.

 

here your best bet is likely to give the checkboxes an array name, and give the field_1 value as the value of the element:

 

while ($stuff = mysql_fetch_row($result))  {
  echo "<tr><td>field_1: {$stuff[0]}</td><td> <input type='checkbox' name='fields[]' value='{$stuff[0]}'> </td> </tr>";
}

 

this will result in $_POST['fields'] being an array that contains every field_1 value that was checked.  when processing that to retrieve the field_2 values, you can simply glue them together and use the IN clause:

 

$ids = implode(',', $_POST['fields']);
$query = "SELECT field_1, field_2 FROM tab WHERE field_1 IN ($ids) ORDER BY whatever";

 

to sum up (in case you want to have a read up on this), you can name any kind of input as an array, which is very handy when you need to group items together for processing in the target file; you can use implode() to glue those pieces together into a string (have a look in the manual); and, you can use the IN () when you just want to SELECT everything in the database that's part of a list.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.