Forum Moderators: coopster
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 :)
$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 variablefor($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
}
$result = mysql_query("SELECT field_2 FROM tab WHERE field_1=$field_1";
$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?
There are also a load of good link in the forum library [webmasterworld.com] and the learning php [webmasterworld.com] thread.
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 è ", $field2sum;
****
thanks for the links, chimp. I'm in browsing frenzy, right now... any good link is always welcome :)
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...]