Jump to content

Archived

This topic is now archived and is closed to further replies.

majocmatt

Nested for loop problem

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]

Share this post


Link to post
Share on other sites
And your question is?

When ever you post any code, explain what you want it to do and tell us what it is doing wrong. Don't just post code and expect the correct answer to appear.

Ken

Share this post


Link to post
Share on other sites

×

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.