i am trying to output statistics of calls made by agents across all dialing campaigns called for the day. there are multiple campaigns, currently 3, dialed in a day. I get the campaign data from a table name that changes by campaign like so custom_16546546 and custom_1564654. I run a query to get the numeric part of the column name then pass that as a variable in to another query that outputs the users statistics. This is where it only displays data from what i can tell is the last query loops information. I have displayed the array data from the first query to confirm the table names are being stored and it looks fine.
$today = "2013-05-29";
$customquery = "SELECT distinct( entry_list_id) FROM vicidial_list where entry_list_id > 0 and last_local_call_time between '$today 00:00:00' and '$today 23:59:59';" ;
$customresult = mysql_query($customquery) or die(mysql_error());
$customrows = mysql_num_rows($customresult);
while ($customrowResult = mysql_fetch_array($customresult))
{
$customID[] = $customrowResult["entry_list_id"];
}
$z = 0;
if(!$customID){
//echo " Please Select a valid date range: ".$today. " - ".$today." is not valid" ;
}else{
while($z<$customrows)
{
$query = "SELECT
vicidial_users.user,
vicidial_users.full_name,
vicidial_log.list_id,
COUNT(CASE WHEN (vicidial_log.status = 'SALE') THEN 1 ELSE null END) as sumcountSamnt,
COUNT(CASE WHEN (vicidial_log.status = 'SALE' AND custom_$customID[$z].up_amt <> '') THEN 1 ELSE null END) as sumcountupAmnt,
COUNT(CASE WHEN (vicidial_log.status = 'SALE' AND custom_$customID[$z].cc_num <> '') THEN 1 ELSE null END) as sumccverifiedcountAmnt,
COUNT(CASE WHEN (vicidial_log.status = 'SALE' AND custom_$customID[$z].bank_act <> '') THEN 1 ELSE null END) as sumbankverifiedcountAmnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE' AND custom_$customID[$z].cc_num <> '') THEN custom_$customID[$z].s_amount ELSE null END) as sumccverifiedAmnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE' AND custom_$customID[$z].bank_act <> '') THEN custom_$customID[$z].s_amount ELSE null END) as sumbankverifiedAmnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE') THEN custom_$customID[$z].d_amt ELSE null END) as sumDamnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE') THEN custom_$customID[$z].up_amt ELSE null END) as sumUpamnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE') THEN custom_$customID[$z].md_amt ELSE null END) as sumMdamnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE') THEN custom_$customID[$z].s_amount ELSE null END) as sumSamnt
FROM
vicidial_log
INNER JOIN
vicidial_users
ON
(vicidial_log.user = vicidial_users.user)
INNER JOIN
custom_$customID[$z]
ON
(vicidial_log.lead_id = custom_$customID[$z].lead_id)
WHERE
call_date
BETWEEN
'$today 00:00:00'
AND
'$today 23:59:59'
AND
vicidial_log.user != 'VDAD'
AND
vicidial_users.user_group != 'TX-Training'
GROUP BY
vicidial_log.user, vicidial_log.campaign_id
";
//This query is used to sum loop data data
$queryResult = mysql_query($query) or die(mysql_error()); //This line perfomrs query error handling
$z++;
}
here is a snippet of how the data is displayed
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>\n";
//--fullname and userID
echo "<td> ".$row["full_name"]. " - ".$row["user"]."</td>\n";
//Agent total sales amount
if ($row["sumcountSamnt"] == '0') {
echo "<td>$nosalestxt</td>\n";
} else {
echo "<td> $".$row["sumSamnt"]."</td>\n";
}
}
So i want to be able to display a sum total of data for each user from all the campaigns they dialed from.
Thanks in advance for your help
EDIT: i use &row from a while loop to output the data and yes Chris i would like to store it an array, but i guess im just missing it, thanks again
Based on the code snippets you have given it looks like you are overwriting the value of $queryResult after each execution of the loop. Perhaps you meant to execute the query and put the results into an array (or something similar) to loop over and display later.
Also I notice you reference $row[] in your output, how are you assigning a value to that variable?
Related
Today I met the problem in CodeIgniter framework when I was trying to filter using date field with yearweek(). Here is my query in normal without CodeIgniter style definition
SELECT b.name as branch,str_to_date(concat(yearweek(gp.createddate), 'sunday'), '%X%V %W') as week,sum(case when amount>0 then qty else 0 end) as purchase_quantity,sum(case when amount>0 then gp.amount else 0 end) as purchase_amount,sum(case when amount<0 then qty else 0 end) as return_quantity,sum(case when amount<0 then gp.amount else 0 end) as return_amount FROM `group_purchase` gp LEFT JOIN `branch` b ON gp.branchid=b.id WHERE yearweek(gp.createddate)=yearweek('2018-12-24') GROUP BY gp.branchid,yearweek(gp.createddate) ORDER BY yearweek(gp.createddate) DESC
That was worked fine and result was produced accurately.i was use yearweek() for grouping data in week wise.when i was implementing this query into codeigniter all the things was worked fine except yearweek().I attached my codeigniter model for above query below
function getPurchasereport($limit,$offset,$order_column,$order_type,$parameters)
{
$this->db->select("b.name as branchname,str_to_date(concat(yearweek(gp.createddate), 'sunday'), '%X%V %W') as week,sum(case when amount>0 then qty else 0 end) as purchase_quantity,sum(case when amount>0 then gp.amount else 0 end) as purchase_amount,sum(case when amount<0 then qty else 0 end) as return_quantity,sum(case when amount<0 then gp.amount else 0 end) as return_amount");
if($parameters['branch']!=NULL){
$this->db->like('gp.branchid',$parameters['branch']);
}
if($parameters['cdatefrom']!=NULL){
$this->db->where('yearweek(gp.createddate)',yearweek("$parameters['cdatefrom'])");
}
$this->db->group_by(array("gp.branchid", "yearweek(gp.createddate)"));
if(empty($order_column) || empty($order_type)){
$this->db->order_by('yearweek(gp.createddate)','asc');
}else{
$this->db->order_by($order_column,$order_type);
}
if($limit!=0){
$this->db->limit($limit,$offset);
}
$this->db->join('branch b','gp.branchid=b.id ','left');
$query = $this->db->get('group_purchase gp');
if($query->num_rows()>0){
return $query->result_array();
}else{
return FALSE;
}
}
it produced error like below
error
The error you have is because of this line
if($parameters['cdatefrom']!=NULL){
$this->db->where('yearweek(gp.createddate)',yearweek("$parameters['cdatefrom'])");
}
the function yearweek is not valid PHP function, therefore you cannot use it that way.
you need to replace it with the below:
if($parameters['cdatefrom']!=NULL){
$this->db->where('yearweek(gp.createddate)',date('YW',strtotime($parameters['cdatefrom'])));
}
I working on one complex listing from database and decide to do all possible requests via one query.
Here is working example:
"SELECT
`c`.`categories_id`,
`c`.`categories_status`,
IF(`c`.`categories_status` = 1, 'Active', 'Not Active') AS `categories_status_name`,
TRIM(`cd`.`categories_name`) AS `categories_name`,
TRIM(`cd`.`concert_date`) AS `concert_date`,
TRIM(`cd`.`concert_time`) AS `concert_time`,
TRIM((
SELECT
CONCAT(
'{\"total_quantity\":',
SUM(CASE WHEN `p`.`products_quantity` > 0 THEN `p`.`products_quantity` ELSE 0 END),
',\"total_price\":\"',
SUM(`p`.`products_price`),
'\"}'
)
FROM
`products_to_categories` `ptc`,
`products` `p`
WHERE
`ptc`.`section_id` = `cd`.`section_id`
AND
`p`.`products_id` = `ptc`.`products_id`
)) AS `products_available`,
TRIM((
SELECT
CONCAT(
'{\"total_quantity\":',
SUM(CASE WHEN `op`.`products_quantity` > 0 THEN `op`.`products_quantity` ELSE 0 END),
',\"total_price\":\"',
SUM(`op`.`final_price`),
'\"}'
)
FROM
`products_to_categories` `ptc`,
`orders_products` `op`
WHERE
`ptc`.`section_id` = `cd`.`section_id`
AND
`op`.`products_id` = `ptc`.`products_id`
AND
`op`.`orders_products_status` != 1
)) AS `products_sold`,
TRIM((
SELECT
CONCAT(
'{\"total_quantity\":',
SUM(CASE WHEN `op`.`products_quantity` > 0 THEN `op`.`products_quantity` ELSE 0 END),
',\"total_price\":\"',
SUM(`op`.`final_price`),
'\"}'
)
FROM
`products_to_categories` `ptc`,
`orders_products` `op`
WHERE
`ptc`.`section_id` = `cd`.`section_id`
AND
`op`.`products_id` = `ptc`.`products_id`
AND
`op`.`orders_products_status` = 1
)) AS `products_pending`
FROM
`categories` `c`,
`categories_description` `cd`
WHERE
`c`.`categories_id` = `c`.`section_id`
AND
`cd`.`categories_id` = `c`.`categories_id`
GROUP BY `c`.`categories_id`
ORDER BY `c`.`categories_status` DESC;"
This work great but my main problem is how to do check IF/ELSE or CASE WHEN for custom defined new fields: products_available, products_sold and products_pending?
Problem is that if products not exists inside table orders_products I not get my generated JSON and that's made a small conflict inside PHP.
I can do check in PHP but want to avoid that part and just print JSON like:
{"total_quantity":0,"total_price":"0.0000"}
FROM
`products_to_categories` `ptc`,
`orders_products` `op`
*
WHERE `products_id` =
(SELECT COALESCE(`products_id`,0))
*
AND
`ptc`.`section_id` = `cd`.`section_id`
I am trying to fetch Data from my sql Query but I am getting the wrong data So can anyone tell me how to do it correctly.
Here Is my code
$result = mysql_query("select
CONCAT_WS(',',
(case when Q1 is not null then 'Q1' else '' end),
(case when Q2 is not null then 'Q2' else '' end),
(case when Q3 is not null then 'Q3' else '' end),
(case when Q4 is not null then 'Q4' else '' end),
(case when Q5 is not null then 'Q5' else '' end),
(case when Q6 is not null then 'Q6' else '' end),
(case when Q7 is not null then 'Q7' else '' end),
(case when Q8 is not null then 'Q8' else '' end),
(case when Q9 is not null then 'Q9' else '' end)
) as NonNullColumns
from $day
where `user` = '$user'") or die(mysql_error());
//echo $result;
if (mysql_num_rows($result) > 1) {
// looping through all results
// products node
$response["Q"] = array();
while ($row = mysql_fetch_assoc($result)) {
// temp user array
$qnumber = array();
$qns= array();
$qnumber["Q".$i] = $row["$Q"];
$i = $i + 1;
// push single product into final response array
array_push($response["Q"], $qnumber);
}
}
I am getting the following Output from my SQl query
NonNullColumns
Q1,Q2,Q3,,,,,,
Q1,Q2,Q3,,,,,,
Q1,Q2,Q3,,,,,,
Q1,Q2,Q3,,,,,,
Q1,Q2,Q3,,,,,,
Q1,Q2,Q3,,,,,,
I need to fetch the values Q1, Q2,Q3 from my SQl query output but I am able to fetch
{"Q":[{"Q":null},{"Q1":null},{"Q2":null},{"Q3":null},{"Q4":null},{"Q5":null}],"success":1}
So Can any one tell me how to obtain the followig output
{"Q":[{Q1},{Q2},{Q3}],"success":1}
You have only one column as you've concatenated all data.
So instead of refering to $row["$Q"] you have to explode(',', $row['NonNullColumns']) and add non-empty string to the array
Currently for a football league, standings are calculated with their own table so I have to manually input/edit Wins, losses, ties, PF and PA. I want to generate this dynamically based on the results of the schedule table which is where scores are recorded.
So I have the following ugly code:
$teamidsql = mysql_query("select team_id, count(team_id) as numteams from team WHERE league_id=$currentleague AND team_div='$div'");
$teamid = mysql_result($teamidsql,0,"team_id");
$numteams = mysql_result($teamidsql,0,"numteams");
$endteams = $teamid+$numteams;
while($teamid < $endteams)
{
$result2=mysql_query("select team_name, team_div,
count(case when (schedule_team1_id = $teamid and schedule_team1_score > schedule_team2_score) or (schedule_team2_id = $teamid and schedule_team2_score > schedule_team1_score) then 1 else NULL end) as wins,
count(case when (schedule_team1_id = $teamid and schedule_team1_score < schedule_team2_score) or (schedule_team2_id = $teamid and schedule_team2_score < schedule_team1_score) then 1 else NULL end) as losses,
count(case when schedule_team1_score = schedule_team2_score then 1 else NULL end) as ties,
sum(case when schedule_team1_id = $teamid then schedule_team1_score else schedule_team2_score end) as pf,
sum(case when schedule_team1_id <> $teamid then schedule_team1_score else schedule_team2_score end) as pa
from schedule, team
where team_id = $teamid AND team.team_div='$div' AND schedule_week >= 1 and schedule_week <= 13 and schedule.league_id = $currentleague and (schedule_team1_id = $teamid or schedule_team2_id = $teamid)") or die(mysql_error());
$t_n = mysql_result($result2,0,"team_name");
$t_w = mysql_result($result2,0,"wins");
$t_l = mysql_result($result2,0,"losses");
$t_t = mysql_result($result2,0,"ties");
$t_pf = mysql_result($result2,0,"pf");
$t_pa = mysql_result($result2,0,"pa");
echo "<tr>";
echo "<td>$t_n</td><td>$t_w</td><td>$t_l</td><td>$t_t</td><td>$t_pf</td><td>$t_pa</td>";
echo "</tr>";
$teamid++;
}
Which currently generates the following html:
http://i.stack.imgur.com/sBVfM.png
My question is how can I sort it? The old table was easy, but I have no idea how I would take this individual data I'm calculating and then sort it via wins, losses, ties, pf then pa.
datatables is the fastest solution to this problem. Give the table a class, call datatabes in the onload, and it will render javascript sorting based on the contents of the html.
http://datatables.net/
guys i need to count new private messages and old one from a table
so first thing come to mind is using mysql_num_rows and easy thing to do
// check new pms
$user_id = $userinfo['user_id'];
$sql = "SELECT author_id FROM bb3privmsgs_to WHERE user_id='$user_id' AND (pm_new='1' OR pm_unread='1')";
$result = $db->sql_query($sql) ;
$new_pms = $db->sql_numrows($result);
$db->sql_freeresult($result);
// check old pms
$sql = "SELECT author_id FROM bb3privmsgs_to WHERE user_id='$user_id' AND (pm_new='0' OR pm_unread='0')";
$result = $db->sql_query($sql) ;
$old_pms = $db->sql_numrows($result);
$db->sql_freeresult($result);
but how can i count these two fields just in one statement and shorter lines ?~
Use this query instead:
SELECT SUM(CASE WHEN pm_new = '1' OR pm_unread = '1' THEN 1 ELSE 0 END) AS new_pms,
SUM(CASE WHEN pm_new = '0' OR pm_unread = '0' THEN 1 ELSE 0 END) AS old_pms
FROM bb3privmsgs_to
WHERE user_id='$user_id'
Here's a MySQL-specific version that reads more cleanly:
SELECT COUNT(IF(pm_new = '1' OR pm_unread = '1', 1, NULL)) AS new_pms,
COUNT(IF(pm_new = '0' OR pm_unread = '0', 1, NULL)) AS old_pms
FROM bb3privmsgs_to
WHERE user_id='$user_id'
MySQL will cast comparisons to 1 or 0. You can use SUM() to add up the portion of the WHERE clause you were trying to count results for.
This is a (MySQL specific) shorter alternative to the CASE WHEN examples.
SELECT
SUM(pm_new='1' OR pm_unread='1') as new_pms,
SUM(pm_new='0' OR pm_unread='0') as old_pms
FROM bb3privmsgs_to
WHERE user_id='$userid'
In SQL Server, you can do something like this:
SELECT
SUM(CASE WHEN pm_new='1' OR pm_unread='1' THEN 1 ELSE 0 END),
SUM(CASE WHEN pm_new='0' OR pm_unread='0' THEN 1 ELSE 0 END)
FROM
bb3privmsgs_to WHERE user_id='$user_id'
I'll suppose you can do about the same thing in mySql, let me get back to you on the details...
As a lazy alternative to some of the other suggestions:
SELECT SUM(newPMS) AS newPMS,
SUM(oldPMS) AS oldPMS
FROM ( SELECT COUNT(author_id) AS newPMS,
0 AS oldPMS
FROM bb3privmsgs_to
WHERE user_id='$user_id'
AND (pm_new='1' OR pm_unread='1')
UNION
SELECT 0 AS newPMS
COUNT(author_id) AS oldPMS
FROM bb3privmsgs_to
WHERE user_id='$user_id'
AND (pm_new='0' OR pm_unread='0')
)