0
for ($x = 0; $x <= 3; $x++) {
    for ($y = 0; $y <= 4; $y++) {
        if ($y == 0) {
            mysql_query("insert into tb_weight_rate_management 
                       (nation,zone_id,rate) values ('Domestic',1,'$del_100')");
        }
    }
}

hello i am little troubled using for loop....in the code above i have two loops ...1st loop will work 3 times and the the inside loop will work 4 times.... now when i click on submit button then it checks loop one and then enter second loop and inserts data 4 times in the database....which is wrong...i want if $y=0; then it should insert data only once but it is inserting data 4 times can anyone please correct the above condition

5
  • 1
    Conditionnal equal is 2 = not 1 Commented Jan 22, 2014 at 7:52
  • you must try if($y == 0 ) Commented Jan 22, 2014 at 7:54
  • ohk but it is not working like that also i have tried it using == Commented Jan 22, 2014 at 7:54
  • what is y doing here ? why are you creating a loop ? you just using y ONE TIME Commented Jan 22, 2014 at 7:56
  • this problem is solved..thnx guyss Commented Jan 22, 2014 at 7:59

3 Answers 3

1

You should use == instead of =. Like this:

if ($y == 0) {
Sign up to request clarification or add additional context in comments.

3 Comments

i did it but still it is inserting data 4 times
The first loop also has 4 iterations. (0,1,2,3)
Because there is 2 loops!
0

the first loop runs 4 times, hence 4 inserts In the inner loop you declare $y = 0 on every iteration of the outer loop

Comments

0

for each x iteration, there are one time y=0. and since x executes 4 times (0, 1, 2 and 3) y gets 0 value 4 times as well. if you want to do the insertion only once then you must add x value as well like if($y == 0 and x == 0).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.