Avatar of error2013
error2013 asked on

PHP Code modification needed (Variables in loop)

Hi all,

First of all this is my current code:


$conn = mysqli_connect('localhost', 'admin', '', 'test');
$rs = mysqli_query($conn, "select count(*) cnt from table where field3 = 8");
$row = mysqli_fetch_assoc($rs);

$cnt = $row['cnt'];
$middle = ceil($cnt/2);

$var1 = 'string1';
$var2 = 'string2';

    "update table a,
           (SELECT @rownum:=0) b
     set article = concat(article, if((@rownum := @rownum+1) > $middle, '$var1', '$var2'))     
     where field3 = 8


Open in new window

This code above works fine but right now:

$var1 = 'string1';
$var2 = 'string2';

Open in new window

populate everything 50% 50%.

My problem here is that if the result on cnt is 10 (for example)...

At the moment 5 records will be populated with 'string1' and the other 5 records with 'string2'...


My problem is that $var1 and $var2 are going to be generated on the fly so $var1 and $var2 will be different on every run instead of now....

So it should be like this::;;

//If cnt was 10 for example...

$var1 = 'string1a';
$var2 = 'string2a';

Open in new window


update the database ...with those 2 variables above...

//then generate another 2 values...

$var1 = 'string1b';
$var2 = 'string2b';

Open in new window

update the database ...with those 2 variables above...

etc until it's reached cnt.

Note: I don't need help with generating the different $var values, I just need to have the code store different ones in a loop.


Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes