I have table of cities, birthday, and users.
Now, what I am trying to do is to query through database and filter range of ages and city of living (out of a list of cities in the selected region - that is why I am checking if count($city[]))>=1), and finely pull out the name of the user based on uid / entety_id.
First I am checking if the user match the range of age, and then if he lives in one of the cities from the selected list.
If no city was selected, I want to filter only the range of ages.
I don't know what is the problem but as far as I notice,
there is this loop, that I have notice that does not execute at all:
for ($d = 1; $d < (count($the_resaults_array)); $d++)
this is the code:
function toodate_query_db($loest_age, $highest_age, $city){
$sql_user_name = '';
if((count($city[0]))>=1){
for ($i = 0; $i < count($city); $i++) {
$city_item_for_check = $city[$i];
$sql = "SELECT field_data_field_birth_date.entity_id
FROM field_data_field_birth_date, field_data_field_city
WHERE(
(field_birth_date_value >= DATE_ADD(NOW(), INTERVAL -{$highest_age} YEAR))
AND
(field_birth_date_value <= DATE_ADD(NOW(), INTERVAL -{$loest_age} YEAR))
AND
(field_city_tid = {$city_item_for_check})
AND
(field_data_field_birth_date.entity_id = field_data_field_city.entity_id))";
if(db_query($sql)->fetchColumn()){
$the_resaults_array[$i] = db_query($sql)->fetchAll();
}
}
for ($d = 1; $d < (count($the_resaults_array)); $d++) {
$user_id = $the_resaults_array[$d];
$sql_user_name .= '<p><strong>'.
db_query("SELECT name FROM {users} WHERE uid = {$user_id}")->fetchColumn()
.'</p></strong>';
}
} else{
$sql = "SELECT field_data_field_birth_date.entity_id
FROM field_data_field_birth_date
WHERE
field_birth_date_value >= DATE_ADD(NOW(), INTERVAL -{$highest_age} YEAR)
AND field_birth_date_value <= DATE_ADD(NOW(), INTERVAL -{$loest_age} YEAR)";
foreach (db_query($sql) as $result) {
$entity_id = $result->entity_id;
$sql_user_id = db_query("SELECT uid FROM {profile} WHERE pid = {$entity_id}")->fetchColumn();
$sql_user_name .= '<p><strong>'.
db_query("SELECT name FROM {users} WHERE uid = {$sql_user_id}")->fetchColumn()
.'</p></strong>';
}}
return $sql_user_name;
}
Related
I have all records in sequence from 1 onwards for each record displayed but wish to update each one with that sequence in a field within the table.
Any help?
if(isset($_POST['setorder']))
{
$i = 1;
//$today = date('Y-m-d', strtotime('-1 day'));
$routesql = $mysqli->query("SELECT * FROM routes WHERE status = 1");
while ($routerows = $routesql->fetch_array())
{
$rname = $routerows['routename'];
$routejobs = $mysqli->query("select tracking.*, district.* from tracking left join district on tracking.street = district.street
where tracking.date = '$today' AND tracking.status = 2 and route = '$rname' order by district.sortorder asc");
while ($routejrows = $routejobs->fetch_array())
{
echo $i.' - '.$routejrows['joborder'].' - '.$routejrows['addressto'];
echo '<br/>';
$i++;
}
}
}
Below query works but it shows two different sum of different orders perfectly as i made GROUP BY ORDER_ID as (1000, 2000).
But i want this (1000, 2000) as (3000). If there are 2 orders in a week 1 has percentage based discount and one is without discount and each order has different delivery charge.
Each order has different discount percentage, and different delivery charge.
That's why i did group by order_id in my query and if i removes group by order_id it gives me a different total amount. But With the group by order_id it shows the correct amount of as much orders are in the current week or month or year.
Please help in this regard.
Below is my query what im trying to do.
$totalX = "select sum(price) as price, coupon, city, delivery_type, order_id
from orders where date between '2018-09-12' and '2018-09-13' group by order_id";
$totalXx = $dba2->query($totalX);
while ($total = $totalXx->fetch_assoc()) {
$couponX = "select coupon, price, percent from couponsAll where id = '".$total['coupon']."'";
$couponXx = $dba->query($couponX);
$coupon = $couponXx->fetch_assoc();
$areaBX = "select * from countries_citiesALL where id = '".$total['city']."' ";
$areaBXx = $dba->query($areaBX);
$areaBXxx = $areaBXx->fetch_assoc();
$area6X = "select * from delivery_typeALL where area = '".$total['city']."' and id = '".$total['delivery_type']."'";
$area6Xx = $dba->query($area6X);
$area6xXx = $area6Xx->fetch_assoc();
$dchargezQ = $area6xXx['price'];
if ($coupon['price'] >= 1 && $coupon['percent'] < 1) {
/// this condition is if price based discount
$priceAcoup = $total['price'] - $coupon['price'];
$gtotalx = $priceAcoup + $areaBXxx['price'] + $dchargezQ;
$gtotal = number_format($gtotalx, 3);
echo '<font color="black" style=""><b>'.$gtotal.'</b></font>';
} else {
if ($coupon['price'] < 1 && $coupon['percent'] >= 1) {
/// this condition is if percentage based discount
$priceAcoup = $total['price'] - (($total['price'] * $coupon['percent']) / 100);
$gtotalx = $priceAcoup + $areaBXxx['price'] + $dchargezQ;
$gtotal = number_format($gtotalx, 3);
echo '<font color="black" style=""><b>'.$gtotal.'</b></font>';
} else {
/// this condition is if there is no percentage or price based discount
$gtotalx = $total['price'] + $areaBXxx['price'] + $dchargezQ;
$gtotal = number_format($gtotalx, 3);
echo '<font color="black" style=""><b>'.$gtotal.'</b></font>';
}
}
}
Well issue is resolved as mentioned below.
I changed $weekTotal[] under every condition with $weekTotal1[], $weekTotal2[], $weekTotal3[] and put this $weekTotal1=array();, $weekTotal2=array();, $weekTotal3=array(); above the 2nd query loop and got it working.
Thanks for your kind assistance.
$__xz_rTorders1 = $dba2->query("SELECT count(distinct(order_id)) as count, order_id, date FROM orders_confirmed
group by WEEK(date), MONTH(date)
order by WEEK(date), MONTH(date)
");
while($__xz_rTordersa4 = $__xz_rTorders1->fetch_assoc()) {
$__xz_signupweek = $__xz_rTordersa4['date'];
/*start day*/
for($__xz_i = 0; $__xz_i <7 ; $__xz_i++) {
$__xz_date = date('Y-m-d', strtotime("-".$__xz_i."days", strtotime($__xz_signupweek)));
$__xz_dayName = date('D', strtotime($__xz_date));
if($__xz_dayName == "Sun") {
//echo "<b>From:</b> ". date( "d/m/Y", strtotime($__xz_date))." / ";
$mstart_date=date( "Y-m-d", strtotime($__xz_date));
}
}
/*end day*/
for($__xz_i = 0; $__xz_i <7 ; $__xz_i++) {
$__xz_date = date('Y-m-d', strtotime("+".$__xz_i."days", strtotime($__xz_signupweek)));
$__xz_dayName = date('D', strtotime($__xz_date));
if($__xz_dayName == "Sat") {
//echo "<b>To:</b> ". date( "d/m/Y", strtotime($__xz_date));
$mend_date=date( "Y-m-d", strtotime($__xz_date));
}
}
///////////////////***********///////////////////
$weekTotal1=array();
$weekTotal2=array();
$weekTotal3=array();
///////////////////***********///////////////////
$totalX= "select sum(price) as price, coupon, city, delivery_type, order_id
from orders_confirmed where date between '".$mstart_date."' and '".$mend_date."'
group by order_id
";
$totalXx= $dba2->query($totalX);
while ($total = $totalXx->fetch_assoc()){
$couponX= "select coupon, price, percent
from coupons
where id = '".$total['coupon']."'
";
$couponXx= $dba->query($couponX);
$coupon = $couponXx->fetch_assoc();
$areaBX= "select * from countries_cities
where id = '".$total['city']."' ";
$areaBXx= $dba->query($areaBX);
$areaBXxx= $areaBXx->fetch_assoc();
$area6X= "select * from delivery_type
where area = '".$total['city']."'
and id = '".$total['delivery_type']."'
";
$area6Xx= $dba->query($area6X);
$area6xXx= $area6Xx->fetch_assoc();
$dchargezQ= $area6xXx['price'];
if ($coupon['price'] >= 1 && $coupon['percent'] < 1) {
$priceAcoup1=$total['price']-$coupon['price'];
///////////////////***********///////////////////
$weekTotal1[] = $priceAcoup1+$areaBXxx['price']+$dchargezQ;
///////////////////***********///////////////////
}else if ($coupon['price'] < 1 && $coupon['percent'] >= 1) {
$priceAcoup2=$total['price']-(($total['price']*$coupon['percent'])/100);
///////////////////***********///////////////////
$weekTotal2[] = $priceAcoup2+$areaBXxx['price']+$dchargezQ;
///////////////////***********///////////////////
}else{
///////////////////***********///////////////////
$weekTotal3[] = $total['price']+$areaBXxx['price']+$dchargezQ;
///////////////////***********///////////////////
}
} // End Of While Loop (2nd)
$weekTotal_val3 = array_sum($weekTotal1);
$weekTotal_val4 = array_sum($weekTotal2);
$weekTotal_val5 = array_sum($weekTotal3);
$finalTOTAL=number_format($weekTotal_val3+$weekTotal_val4+$weekTotal_val5,3);
echo '<font color="red" style=""><b>'.$finalTOTAL.'</b></font>';
} // End Of While Loop (1st)
I want to update my database using constraints
for ($count = 0; $count <= $size; $count++) {
if($dayOfTheWeek[$count] == "Friday" or $dayOfTheWeek[$count] == "Saturday"){
$query = "UPDATE rota SET title='Guest' WHERE date = '$dateMonthYearArr[$count]' AND starttime = '22:00'";
$dayresult = mysql_query($query);}
}
I have multiple users with a $starttime of 22:00, but i only want the first users detail to be updated leaving the rest unchanged. how would i go about doing this?
If you only want one record to be changed you can append this to the end of your statement:
LIMIT 1
For example:
$query = "UPDATE rota SET title='Guest' WHERE date = '$dateMonthYearArr[$count]' AND starttime = '22:00' LIMIT 1";
On my blog I am have a function that counts how many blog posts have been made month by month and these can be displayed in a sub menu.
It is looped by year, and then another nested loop by month. Each time it runs this query to count how many articles were posted in each month.
This is the query:
Select * from DM_blog blog where blog.id IN
( SELECT entry_id FROM DM_tags_target tagstarget WHERE tagstarget.parent_id IN
( SELECT id FROM DM_tags tags WHERE tags.tag = '".$data['page']['id']."' AND tags.type = 'blog_target' )) AND blog.publish < '".date("Y-m-d H:i:s")."' and blog.status = '0' and YEAR(blog.publish) = '".$year."' and MONTH(blog.publish) = '".$month."'"
Is there a better way to do this? Can I do all of the months at once instead of having to do 12 queries a year? The full code is here:
$this->benchmark->mark('blog_submenu_start');
$dates = $this->blog_model->menu_dates($data);
if($dates['years']){
$i = 0;
foreach($dates['years'] as $year){
if($data['segments']['modal'] == $year['YEAR(blog.publish)']){
$data['date_submenu']['year'][$year['YEAR(blog.publish)']]['class'] = 'selected';
$selected = true;
}else if(date("Y") == $year['YEAR(blog.publish)'] && $selected == false){
$data['date_submenu']['year'][$year['YEAR(blog.publish)']]['class'] = 'selected';
$selected = true;
}
// find the start month
if($dates['first_entry'][0]['YEAR(blog.publish)'] == $year['YEAR(blog.publish)']){
if($dates['first_entry'][0]['MONTH(blog.publish)'] < 2){
$limit_s['start'] = '1';
$limit_s['end'] = 13;
}else{
$limit_s['start'] = $dates['first_entry'][0]['MONTH(blog.publish)'];
$limit_s['end'] = 12;
}
}else{
$limit_s['start'] = 1;
$limit_s['end'] = 13;
}
// run through the months
$this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_start');
for($x=$limit_s['start'];$x<=$limit_s['end'];$x++){
$this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_start');
$this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_mysql_start');
$count = $this->blog_model->month_count($year['YEAR(blog.publish)'],$x,$data);
$this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_mysql_end');
if($last == false){
$this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_selected_start');
if($data['segments']['modal'] == $year['YEAR(blog.publish)'] and $data['segments']['para_one'] == $x){
$data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['class'] = 'selected';
}
$this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_selected_end');
$this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_assign_start');
$data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['month'] = $x;
$data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['display_full'] = date('F',strtotime($year['YEAR(blog.publish)'].'-'.$x.'-01'));
$data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['display_short'] = date('M',strtotime($year['YEAR(blog.publish)'].'-'.$x.'-01'));
$data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['count'] = $count;
$data['date_submenu']['year'][$year['YEAR(blog.publish)']]['months'][$x]['sef'] = $year['YEAR(blog.publish)'].'/'.$x.'/';
$this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_assign_end');
if(date("Y") == $year['YEAR(blog.publish)'] and date("m") == $x || date("n") == $x){
$last = true;
} // end date
$i++;
$this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_'.$x.'_end');
} // end last false
} // end for
$this->benchmark->mark('blog_submenu_dates_'.$year['YEAR(blog.publish)'].'_end');
}
}
$this->benchmark->mark('blog_submenu_end');
$this->blog_model->month_count - this function is as follows:
function month_count($year,$month,$data=false){
$query = $this->db->query("Select * from DM_blog blog where blog.id IN
( SELECT entry_id FROM DM_tags_target tagstarget WHERE tagstarget.parent_id IN
( SELECT id FROM DM_tags tags WHERE tags.tag = '".$data['page']['id']."' AND tags.type = 'blog_target' )) AND blog.publish < '".date("Y-m-d H:i:s")."' and blog.status = '0' and YEAR(blog.publish) = '".$year."' and MONTH(blog.publish) = '".$month."'");
return $query->num_rows();
}
You can add group by clause to group the results by year and month.
Select * from DM_blog blog where blog.id IN
( SELECT entry_id FROM DM_tags_target tagstarget WHERE tagstarget.parent_id IN
( SELECT id FROM DM_tags tags WHERE tags.tag = '".$data['page']['id']."' AND tags.type = 'blog_target' )) AND blog.publish < '".date("Y-m-d H:i:s")."' and blog.status = '0' GROUP BY YEAR(blog.publish), MONTH(blog.publish)
You could use a GROUP BY expression:
SELECT id, YEAR(blog.publish), MONTH(blog.publish)
FROM DM_blog blog
WHERE blog.id IN
( SELECT entry_id
FROM DM_tags_target tagstarget
WHERE tagstarget.parent_id IN
( SELECT id
FROM DM_tags tags
WHERE tags.tag = '".$data['page']['id']."'
AND tags.type = 'blog_target'
)
)
AND blog.publish < '".date("Y-m-d H:i:s")."'
AND blog.status = '0'
GROUP BY YEAR(blog.publish), MONTH(blog.publish)
It will give you the results grouped by year and month
I have a database full of php time(); stored when a user logs in.
I know I should have created a value for their username and updated the login count but instead I have a database full of php time(); with their username
Here is how I find the amount of logins for individual users:
<?php
function to_date($timestamp) {
return date('Y-m-d', $timestamp);
}
$days = array();
$occurences = array();
$zero = array();
$query = mysql_query("SELECT `login_time` FROM `stats` WHERE `userid`='$someuserid' ORDER BY `login_time` ASC");
while($rows = mysql_fetch_array($query)) {
$days[] = to_date($rows['login_time']);
}
$days[] = to_date(time());
$occurences = array_count_values($days);
$days = array_unique($days);
sort($days);
for($i = 0; $i < count($days); $i++) {
$difference = isset($days[$i+1]) ? strtotime($days[$i+1]) - strtotime($days[$i]) : 0;
if($difference > 86400) {
$difference = ceil(abs($difference/86400));
$fill = $days[$i];
for($k = 0; $k < $difference-1; $k++) {
$fill += strtotime('+1 day', strtotime($fill));
$zero[] = to_date($fill);
}
}
}
echo "[";
for($i = 0; $i < count($zero); $i++) {
echo "[\"".$zero[$i]."\",0], ";
}
for($i = 0; $i < count($occurences); $i++) {
if($i == count($occurences)-1)
echo "[\"".$days[$i]."\",".($occurences[$days[$i]]-1)."]";
else {
echo "[\"".$days[$i]."\",".$occurences[$days[$i]]."], ";
}
}
echo "]";
?>
How could I apply this to all the users and find out who logged in the most for each day in the database? The output is encoded for jqplot.
SELECT
userid,
DATE(FROM_UNIXTIME(login_time-MOD(login_time,86400)+1)) as logindate,
count(*) as logincount
FROM stats
GROUP BY userid, logindate
Use the above query to get all the required data directly from the database and this will help you avoid doing the post processing using PHP
Additionally, if you would like to get only one record, which have maximum logins in a (any) day, the use the below query with an added SQL wrapper for the above query:
SELECT * FROM (
SELECT
userid,
DATE(FROM_UNIXTIME(login_time-MOD(login_time,86400)+1)) as logindate,
count(*) as logincount
FROM stats
GROUP BY userid, logindate
) tmptable
ORDER BY logincount DESC, logindate DESC LIMIT 1
Let me know if this helped you.
SELECT userid, COUNT(userid) AS 'login_count' FROM stats GROUP BY userid
All the answer may be correct or incorrect. But I know one thing that my answer is not correct.But in the case of Max logins , there is no need of such an answer. Thats all I know.