Jump to content

for loop


karimali831

Recommended Posts

I am trying to update 7 rows by setting the "wk" column in the sequence 1-7,

so each row will have "wk" set to 1-7, right now the query is updating all rows and setting wk column to 7 and not 1-7?

 

for($i = 1; $i <= 7; $i++) {
        safe_query("UPDATE ".PREFIX."cup_matches SET wk='$i' WHERE type='gs' && $type_opp='0' && matchno='$ID' && clan1='".$sPT['clan1']."'");	    
       }

 

Any help on this please?

 

Link to comment
Share on other sites

I am trying to update 7 rows by setting the "wk" column in the sequence 1-7,

so each row will have "wk" set to 1-7, right now the query is updating all rows and setting wk column to 7 and not 1-7?

 

Can you try explaining that again? What do you mean by setting the value to "1-7". The code you have sets the value to 1, then on the 2nd iteration sets it to 2, then 3, then 4, etc. So, when the loop completes the record will only have the value of 7. If you want a value of "1-7" then set that value.

 

Instead of running the queries you should echo them to the page to see what is actually happening. That is why I advise to always create queries as a string variable instead of building them directly i the mysql_query() function.

 

for($i = 1; $i <= 7; $i++)
{
    $query = "UPDATE ".PREFIX."cup_matches SET wk='$i' WHERE type='gs' && $type_opp='0' && matchno='$ID' && clan1='{$sPT['clan1']}'";
    echo $query . "<br>\n";
    //safe_query($query);	    
}

Link to comment
Share on other sites

This is what I get and I want it like this:-

 

UPDATE ws_bi2_cup_matches SET wk='1' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1'
UPDATE ws_bi2_cup_matches SET wk='2' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1'
UPDATE ws_bi2_cup_matches SET wk='3' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1'
UPDATE ws_bi2_cup_matches SET wk='4' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1'
UPDATE ws_bi2_cup_matches SET wk='5' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1'
UPDATE ws_bi2_cup_matches SET wk='6' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1'
UPDATE ws_bi2_cup_matches SET wk='7' WHERE type='gs' && ladID='0' && matchno='6' && clan1='1'

 

But when I select:

 

SELECT *

FROM  `ws_bi2_cup_matches`

WHERE  WHERE type='gs' && ladID='0' && matchno='6' && clan1='1'

 

Each 7 rows has a set value of "7" ?

Pic: http://s16.postimage.org/i3zumstv7/prob6.png

 

1st row should have 1..

2nd row 2..

3rd row 3.. so forth.

Link to comment
Share on other sites

How are you expecting to update seven different records with the same WHERE clause? Do you see why that doesn't make any sense, and will never work?

 

I do understand what you are saying but you still unsure what I am trying to do?

I am able to fetch 7 rows and simply want to update each row to set value 1,2,3,4,5,6 and 7.

Link to comment
Share on other sites

  • 5 weeks later...

Did you solve this?

 

This query will always result in wk == 7 because your WHERE doesn't differentiate between themselves, so the same rows are getting updated, first with 1, then with 2, then 3, etc, until 7. The problem isn't necessarily your code as much as the table structure or the way you're querying it.  Seems to me that maybe this shouldn't be an UPDATE so much as an INSERT query.

Link to comment
Share on other sites

Perhaps if you now add another condition "AND wk > $i"

 

for($i = 1; $i <= 7; $i++)
{
    $query = "UPDATE ".PREFIX."cup_matches SET wk='$i' WHERE type='gs' AND $type_opp='0' AND matchno='$ID' AND clan1='{$sPT['clan1']}' AND wk > $i LIMIT 1";
    echo $query . "<br>\n";
    //safe_query($query);



    
}

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.