Hi,
I am in the middle of developing a web application (customer DB) and currently attempting to create a module to update the number of member registered under 1 agent.
I have 2 table, 1 is 'users' and another 1 is 'dealers'. The 'users' table contains some normal user credential "agent" column which contains the "username" of 'dealers'. This table also includes "user_registration_timestamp" which indicates the registration time of this particular user.
On 'dealers' table, I have "registration_timestamp" too but with"expiredtime" to indicate the agent active period.
My script is to calculate how many "users" contains the same dealers in the period of that dealers active status (by using users' registration_timestamp and compare it with dealers registration_timestamp and expiredtime) and update into "thisperiodusercount" column inside 'dealers'.
Since the dealer has 2 types of active period, I have to use the "usercountbalance" column to add balance after 1 active period to another so when updating, it needs to add total users + usercount balance.
Below is my attempt:
//Query to get the max dealer ID
$getMaxID = mysql_query("SELECT MAX(`id`) AS maxid FROM dealers");
$row = mysql_fetch_array($getMaxID);
$maxID = $row[maxid];
for ($i = 0; $i <= $maxID; $i++){
//get registration_timestamp, expiredtime and usercountbalance from dealer
$getDealerAttributes = mysql_query("SELECT registration_timestamp, expiredtime, usercountbalance, username FROM dealers WHERE `id` = '$i'") or die;
while (($row2 = mysql_fetch_array($getDealerAttributes)) != false){
$registration_timestamp = $row2['registration_timestamp'];
$expiredtime = $row2['expiredtime'];
$usercountbalance = $row2['usercountbalance'];
$username = $row2['username'];
$getAgentCountResult = mysql_query("SELECT `agent`, COUNT(`agent`) AS agentcount FROM users WHERE `registration_timestamp` BETWEEN '$registration_timestamp' AND '$expiredtime' AND `agent` = '$username' GROUP BY `agent` ORDER BY `agent` ASC");
while ($row3 = mysql_fetch_assoc($getAgentCountResult)) {
$nu=$row3['agentcount'];
$name=$row3['agent'];
mysql_query("UPDATE `dealers` SET thisperiodusercount = ('$nu' + '$usercountbalance') WHERE username = '$name' AND dealershipstatus = 'subsequent3months'");
}
}
}
Currently it is still not working and it does not update the table correctly. Can somebody help to correct my syntax and concept? Thanks.