Jump to content


Photo

Nested for loop problem


  • Please log in to reply
1 reply to this topic

#1 majocmatt

majocmatt
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 07 April 2006 - 06:00 PM

// 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]."')");
                }
        }
} 



#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 April 2006 - 07:25 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users