Jump to content

Nested for loop problem


majocmatt

Recommended Posts

[code]
// On the previous page, if you select USA, you pick multiple states.
// For some reason, my loop isn't inserting multiple entries into the DB
// If I run print statements within the loop, everything prints to the screen
// correctly... But it only inserts the first one in the array of #states
// Any idea why?

// $repid is the user id, insignificant to the problem, asssume its 1

// get values from country and states
$countries = $_POST['countries']; // array
$states = $_POST['state'];          // array      

// get amount of each entity
$number_of_countries = count($_POST['countries']);
$number_of_states = count($_POST['state']);            
$number_of_provinces = count($_POST['province']);      
$number_of_territories = count($_POST['territory']);    

// loop thru countries then states and insert into db accordingly
for($c = 0; $c < $number_of_countries; $c++) {
        if($countries[$c] == "100") { // 100 == USA
                // state id entry
                for($s = 0; $s < $number_of_states; $s++) {          
                        mysql_query("INSERT INTO reps_coverage VALUES ('".$countries[$c]."','$repid','".$states[$s]."')");
                }
        }
}

[/code]
Link to comment
https://forums.phpfreaks.com/topic/6827-nested-for-loop-problem/
Share on other sites

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.