Forum Moderators: coopster

Message Too Old, No Replies

$ POST array and more

I really need to get a manual, I know...

         

pengo

4:03 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



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 :)

d40sithui

5:04 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



Hello and welcome.
This is a friendly community so you do not have to worry about us picking on you for being new.
Anyway,
To display the form and manage the checkbox integrity, you need to name your checkbox fields with the same name but with a "[]" to signify an array. Notice I use "tab[]" as the name::

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

while($stuff = mysql_fetch_row($result)){
echo "<tr><td><input type=\"checkbox\" name=\"tab[]\" value=\"".$stuff['field_1']."\">Field 1</td></tr>\n";
}

In file2.php, you said you wanted to select both fields and to sum their values up depending on what was checked. This makes me assume that field_1 is an ID field, and field_2 is a VALUE field. If this is correct, you'll most likely need to loop through your checkbox array.


$tab = $_POST['tab']; //gets checkbox variable array
$field2sum = 0; //initiates field 2 sum variable

for($i = 0; $i < sizeof($tab); $i++){
$field_1 = $tab[$i]; //assigns field_1
$result = mysql_query("SELECT field_2 FROM tab WHERE field_1=$field_1";
$stuff = mysql_fetch_row($result);//gets field_2
$field2 = $stuff['field_2']; //assigns field_2
$field2sum+=$field2; //increments field_2 sum
}

pengo

6:37 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



Thanks for the answer, helped a lot. But what if field_1 is NOT an ID field (right now, it is just some random numbers...)? RIght now, I just get 0 as the total...

pengo

6:39 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



better, assume field_1 has this entries
a
b
c

field_2
1
2
3

SHOULD I use an ID field?

d40sithui

6:58 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



Should you use an ID field? 99% of the time yes. It would help to identify what your data represents. I'm not clear on what you want to accomplish with this project or you are just playing around with php at the moment, so I can't give you a straight answer. I can only assume that it is some sort of productId/price list from your previous post.
If you want to use field_1 as a checkbox, yes you will need it be an id. It will act as a unique identifier. this keeps the item unique and its data will not overlap others (since they do not have the same id).
If you have your field_1 data as "a", "b", "c",... you have to then change this

$result = mysql_query("SELECT field_2 FROM tab WHERE field_1=$field_1";

to (notice the quotation to compare text)

$result = mysql_query("SELECT field_2 FROM tab WHERE field_1='$field_1'";

For your purpose, it is not practical to use anything else besides a common integer as the row id, especially not letters since there are only 26 available. Numbers go on forever. You can write an algorithm to mix them, but that is more involved and not yet nesessary!

Perhaps you can explain what exactly your project is suppose to do?

PHP_Chimp

7:41 pm on Sep 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You said that you dont have access to books...you dont need them. As the php manual [php.net] is online.

There are also a load of good link in the forum library [webmasterworld.com] and the learning php [webmasterworld.com] thread.

pengo

8:16 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



d40sithui, thanks again, the project is a fast point based character generator for a roleplaying game.
in field_1 I would put the name of the specialties a player can choose, and in field_2 the point cost to get them. You just select which ones you want, and get the total cost. Later it will become more complicated...

I have a problem: in file2.php, I don't get anything if I insert:

$field2 = $stuff['field_2']; //assigns field_2

I changed $stuff['field_2'] to $stuff['0'] and it works. So... I did mysql_fetch_row($result), and got $stuff, which is an array. Now var_dump($stuff) prints:
array(1) { [0]=> string(1) "3" }
which is correct and just what I want, but if I try to access to that "3" as $stuff['field_2'], I just get a nice 0, while $stuff['0'] works just fine. Why?

here's the code:

$tab = $_POST['tab']; //gets checkbox variable array
$field2sum = 0; //initiates field 2 sum variable
for ($y = 0; $y < sizeof($tab); $y++)
{
$field_1 = $tab[$y]; //assigns field_1

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

$stuff = mysql_fetch_row($result);//gets field_2
var_dump($stuff);
echo "<br>";
$field2 = $stuff['field_2']; //assigns field_2
$field2sum += $field2; //increments field_2 sum
}
echo "<br>il costo totale &egrave; ", $field2sum;

****

thanks for the links, chimp. I'm in browsing frenzy, right now... any good link is always welcome :)

d40sithui

3:25 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Oh that was a mistake on my part. I usually use mysql_fetch_assoc() rather than mysql_fetch_row() so that's why $stuff['field_2'] returned empty. mysql_fetch_row() returns an array with integers as the array key instead of the field name as the key, which mysql_fetch_assoc() does.


So use $stuff[insert number here] if you use mysql_fetch_row().
Or use $stuff['insert_field_name_here'] if you use mysql_fetch_assoc()

Anyway, either function is fine. I tend to prefer mysql_fetch_assoc() because I can better manage which field I'm getting rather than keep track of numerical values.
That project you have is pretty cool and you'll learn alot after you finish it. Yes, you definitely would need to use IDs in place for the "specialty" table (specialty_id, specialty_name, specialty_cost).

To learn more about PHP, I think the tutorial on w3schools website is good:: [w3schools.com...]
To look up PHP functions:: [php.net...]

pengo

3:48 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Ok, I had made a few experiments so I had catched the difference between the two functions ;) Later today, or tomorrow, I'll post some code, because I'm not being able to extract a value out of an array using _row, _assoc or _array, and still haven't understood where the problem is... stay tuned!
Oh, and I just downloaded the php manual, so I'll have a good time reading it I guess. Ciao

andrewsmd

4:28 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Quick FYI, have you ever used PEAR. It makes working with MySQL much easier. You can download it and then require_once("DB.php");

pengo

8:50 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Ok, last post here just to say that everything is working fine, thanks a lot again... a new post coming in!