Link to home
Start Free TrialLog in
Avatar of error2013
error2013

asked on

PHP Code modification needed (Variables in loop)

Hi all,

First of all this is my current code:

<?php

$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';

mysqli_query(
    $conn, 
    "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'...

Now...

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.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial