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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.