I am writing a email application which has a graphical statistic page, The graph shows how many e-mails a user has send per day, I can get the data for the graph correctly for each day only if I hard code it, but I would like to know how to make a automatic mechanism to.
this is a query I use to get data from database:
Query:
$today = "SELECT COUNT(tbl_template_log.user_id) FROM tbl_template_log ";
$today .= "WHERE tbl_template_log.user_id='$id'";
//$today .= " AND tbl_template_log.send_date > '2014-05-21' ";
//$today .= "AND tbl_template_log.send_date < '2014-05-21'";
But I would like to have variables instead of the dates like $start and $finish that are readjusted for everyday day, So user can se an actual daily data without me manually changing the data....?
my PHP class:
class statsToday{
function con()
{
require_once('connect.php');
$DB = new dbConnect();
return $DB->connect();
}
public $start;
public $finish;
public function getStats($user){
$user = "SELECT user_id FROM tbl_user WHERE ott_email='$user'";
$query = mysqli_query($this->con(), $user);
$count = mysqli_num_rows($query);
if($count == 1){
while($row = mysqli_fetch_assoc($query)){
$id = $row['user_id'];
}
$today = "SELECT COUNT(tbl_template_log.user_id) FROM tbl_template_log ";
$today .= "WHERE tbl_template_log.user_id='$id'";
//$today .= " AND tbl_template_log.send_date > '$start' ";
//$today .= "AND tbl_template_log.send_date < '$finihs''";
$query_today = mysqli_query($this->con(), $today);
$today = mysqli_fetch_row($query_today);
echo $today[0];
}
}
}
Could somebody suggest a solution..?
If you only want "today" you can use the built in mysql date function in the form of:
$today = "SELECT COUNT(tbl_template_log.user_id) FROM tbl_template_log ";
$today .= "WHERE tbl_template_log.user_id='$id'";
$today .= " AND DATE(send_date) = DATE(NOW())";
Or to do all in the last week:
$today = "SELECT COUNT(tbl_template_log.user_id) FROM tbl_template_log ";
$today .= "WHERE tbl_template_log.user_id='$id'";
$today .= " AND tbl_template_log.send_date >= '" . date('Y-m-d', strtotime('-1 week')) . "' ";
$today .= "AND tbl_template_log.send_date =< '" . date('Y-m-d', strtotime('today')) . "'";
Another way, a little easier to use if you are indexing the database on the send_date field:
$today = "SELECT COUNT(*)
FROM tbl_template_log
WHERE user_id='$id'
AND send_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 1 DAY";