Matching array elements to get an array element at another index
I have a PHP array that I'm using to generate an HTML form. The PHP array is this:
<?php
$vdb = array (
array( "Alabama", 275),
array( "Alaska", 197),
array( "Arizona", 3322));
?>
The PHP to generate the HTML form is below. I need to have the value be the name of the state, because there is some AJAX I'm using to display which states a user has chosen.
<?php
ec开发者_运维技巧ho "<table border='1'><thead><tr><th></th><th>State</th><th>Contacts</th><th>Email</th></tr></thead>";
for ($row = 0; $row < 42; $row++) {
echo "<tr><td class='input_button'><input type='checkbox' name='vdb[]' value='".$vdb[$row][0]."' title='".$vdb[$row][1]."' /></td>";
echo "<td>".$vdb[$row][0]."</td>";
echo "<td>".$vdb[$row][1]."</td>";
}
echo "</table>";
?>
What I'm trying to do is, on submission of the form, with the states the user selected, loop through the PHP array and total the numbers from the selected states. So if I checked Alabama and Alaska, I'd want to add 275 + 197.
This is what I thought would have worked, but it's not:
<?php
$vendors = array();
if (isset($_POST["vdb"])) {
$vendors = $_POST["vdb"];
}
$ven_i = 0;
$ven_j = 0;
$ven_total = 0;
foreach ($vendors as $value) {
foreach ($vdb as $vdb_value) {
if ($vendors[$ven_i] == $vdb[$ven_j][0]) {
$ven_total += $vdb[$ven_j][1];
}
$ven_j++;
}
$ven_i++;
}
?>
and then $ven_total should be the total I'm looking for. However, $ven_total just ends up being the first checkbox selected, and it ignores the rest. I am doing this correctly with the AJAX, displaying the total on the front end, but I don't know how to pass that on to the form submission. I'd rather not using GET and URL variables, because a user could type something into the URL and modify the count. Any idea what I'm doing wrong, or a better way to approach this that I would be able to understand? (Very much a novice programmer.)
This answer is going to end up having a couple different suggestions, but bear with me. First off, using a list as a data structure gets messy because you end up forgetting what index 1 actually means. So, since you have more than just a pairing of name to value, you can try making it a basic class and make your code more readable.
<?php
class State {
public $name;
public $contact_count;
public $email_count;
function __construct($name, $contact_count, $email_count) {
$this->name = $name;
$this->contact_count = $contact_count;
$this->email_count = $email_count;
}
}
$states = array (
new State("Alabama", 275, 210),
new State("Alaska", 197, 149),
new State("Arizona", 3322, 2145));
$selected_vendors = array("Alabama", "Arizona");
$ven_total = 0;
foreach ($selected_vendors as $selected) {
foreach ($states as $state) {
if ($selected == $state->name) {
$ven_total += $state->contact_count;
}
}
}
echo $ven_total
?>
This is still your code just cleaned up some. The looping and adding seems to be working fine. Have you done print_r($_POST)
to make sure you are getting the values the way you expect? You mentioned not wanting to use GET, but POST can be spoofed just like GET, always validate input from the user.
why don't you use the number as a key to the $vdb array ?
$vdb = array (
array( 275=>"Alabama" ),
array( 197=>"Alaska" ),
array( 3322=>"Arizona" )
);
and then
$sum = array_intersect($vdb, $vendors);
$total = 0;
foreach ($sum as $key=>$value) {
$total = $total+$key;
}
unset($key, $value, $sum);
The array_intersect() function compares two or more arrays, and returns an array with the keys and values from the first array, only if the value is present in all of the other arrays.
精彩评论