error2013
asked on
PHP Code modification needed (Variables in loop)
Hi all,
First of all this is my current code:
This code above works fine but right now:
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::;;
....
update the database ...with those 2 variables above...
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
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
"
);
?>
This code above works fine but right now:
$var1 = 'string1';
$var2 = 'string2';
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';
....
update the database ...with those 2 variables above...
//then generate another 2 values...
$var1 = 'string1b';
$var2 = 'string2b';
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.