This is a query which I tried below.
$sql = "SELECT id,due, datediff(date('Y/m/d'), date) FROM sales
I am writing a sales program for identifying the due payment.
If the due payment is more than 7 error displayed payment pending what I tried so far I attached below of the full coding. when I tried the below code error ouput displayed
Notice: Undefined index: datediff(pay_date, date)
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$today = date("Y/m/d");
$sql = "SELECT id,due, datediff(date('Y/m/d'), date) FROM sales where due != '0' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$paydate = $row["datediff(pay_date, date)"] . " " . "Days" . "</br>";
if ($paydate >= 7) {
$status = "Payment pending";
$color = "#47f50b";
} elseif ($paydate >= 3) {
$status = "Payment pending please settle down the amount";
$color = "yellow";
} elseif ($paydate >= 1) {
$status = "kindly pay the payment";
$color = "#00a8ff";
} elseif ($paydate == 0) {
$status = "Disable your Order";
$color = "red";
}
?>
Assuming your due date column is called date, then your query should be
$sql = "SELECT id, due, DATEDIFF(date, CURDATE()) AS paydate FROM sales WHERE due != '0' ";
Note that we use CURDATE() to get the current date in MySQL, and that I have aliased the DATEDIFF result to paydate.
You also have an issue that you are trying to set
$paydate = $row ["datediff(pay_date, date)"]
however in your query your expression does not match datediff(pay_date, date). This is why it's better to use an alias as I did above, then you can change that line to simply
$paydate = $row['paydate'];
and if you change the formula in your query this part of the code will still work.
Finally you are appending " Days" to the value of $paydate. This will make comparisons with numbers e.g. in if($paydate >= 7) problematic and it would be better if you left off the " Days" until you output the value of $paydate.
Try using datediff(pay_date, date) as datedif in the SELECT and print using $row["datedif"]
Related
I am trying to find the particular date data from database,
my concept is if 2018-11-30 is given to find in database if the date is present in database it should display the date in output, if not present it should add +1 date to 2018-11-30 and try to find the next date 2018-12-01 like wise it goes on.
but i am facing the problem the date is not checked in the database.i am not sure weather my code is correct or wrong can any one give me solution for it.
this is my code
$Firstdate = '2018-11-29';
$Lastdate = '2018-12-03';
$Sql2 = "SELECT ScheduleDate FROM `empdet`";
$result2 = mysqli_query($con, $Sql2);
while($row = mysqli_fetch_array($result2))
{
if($row["ScheduleDate"]==$Firstdate)
{
$DBfirstdate==$Firstdate;
echo "$DBfirstdatebb",$DBfirstdate;
break;
}
else {
$checkFDBdate=date('Y-m-d', strtotime("+1 day", strtotime($Firstdate)));
if($row["ScheduleDate"]==$checkFDBdate)
{
$DBfirstdate=$checkFDBdate;
echo "$DBfirstdate",$DBfirstdate;
break;
}
}
}
$Sql3 = "SELECT ScheduleDate FROM `empdet`";
$result3 = mysqli_query($con, $Sql3);
while($row = mysqli_fetch_array($result3))
{
if($row["ScheduleDate"] == $Lastdate) {
$DBlastdate==$Lastdate;
echo "$DBlastdateBB",$DBlastdate;
break;
}
else {
$checkLDBdate=date('Y-m-d', strtotime("-1 day", strtotime($Lastdate)));
if($row["ScheduleDate"]==$checkLDBdate)
{
$DBlastdate=$checkLDBdate;
echo "$DBlastdate",$DBlastdate;
break;
}
}
}
$sql ="select * from empdet where ScheduleDate between '".$DBfirstdate."' and '".$DBlastdate."'";
$result = $con->query($sql);
SELECT ScheduleDate FROM `empdet` where ScheduleDate > '$Firstdate' and ScheduleDate < '$Lastdate' order by ScheduleDate limit 1
Try this way. You will get the first raw from the date range given.
Select stuff from somewhere where something > 'value' order by something limit 1
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'm still learning PHP & MySQL and the following has confused me for a long time and I'm looking for some guidance as to how I'd be able to do this.
Basically, I have a HTML table which is displayed by an echo which grabs the values from a MySQL database, the table displays Position, User, Today. Weekly & Monthly. The rows that the table displays is dependent on how many users are logged in so if 10 people are logged in, 10 rows will be displayed, 5 users = 5 rows etc. To achieve this I created a loop which displays the information for each logged in user and this is where I think the issue starts with sorting the data. This is what is used to display the data:
<?php
$counter = 1;
include_once 'config.php';
$sql = "SELECT * FROM vicidial_live_agents";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
$dateNow = date('Y-m-d H:i:s');
$dateNow = strtotime($dateNow);
$dateOld = $row['last_state_change'];
$dateOld = strtotime($dateOld);
$dateDiff = $dateNow - $dateOld;
if ($row['status'] == "INCALL") {
$colour = "w3-green";
}
elseif ($row['status'] == "READY") {
$colour = "w3-yellow";
}
elseif ($row['status'] == "PAUSED") {
$colour = "w3-grey";
}
elseif ($row['status'] == "DEAD") {
$colour = "w3-red";
}
$user = $row['user'];
echo
'<tr class="'.$colour.'">
<td class="w3-xlarge">'.$counter.'</td>
<td class="w3-xlarge">'.$user.'</td>';
$sql2 = "SELECT count(status) AS sales FROM vicidial_agent_log WHERE user = '$user' AND DATE(event_time) = CURDATE() AND status = 'SALE'";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc())
{
echo
'<td class="w3-xlarge">'.$row2['sales'].'</td>';
$sql3 = "SELECT count(status) AS salesWeek FROM vicidial_agent_log WHERE user = '$user' AND WEEK(event_time) = WEEK(CURDATE()) AND YEAR(event_time) = YEAR(CURDATE()) AND status = 'SALE'";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc())
{
echo
'<td class="w3-xlarge">'.$row3['salesWeek'].'</td>';
$sql4 = "SELECT count(status) AS salesMonth FROM vicidial_agent_log WHERE user = '$user' AND MONTH(event_time) = MONTH(CURDATE()) AND YEAR(event_time) = YEAR(CURDATE()) AND status = 'SALE'";
$result4 = $conn->query($sql4);
while($row4 = $result4->fetch_assoc())
{
echo
'<td class="w3-xlarge">'.$row4['salesMonth'].'</td>
</tr>';
$counter++;
}
}
}
}
?>
What I want is for the table to be sorted by highest monthly sales with the highest being at the top and lowest at the bottom.
I have done some research and the solutions I've found I could not get to work with this situation for example, a JS sort from W3Schools wouldn't work and I think this is due to the values being called from the database.
I hope this is clear, any advice on how to tackle this is appreciated :)
I have a PHP Script which is used to write data to a MYSQL Database.The Script retrieves the data from an Android app and write it to the database table.What I want is : the PHP script will calculate the count of data in the database table according to the date of entry and pass the count to the Android device according to which I need to do some validations in the Android App.Is it possible to do so in the same script? I mean is it possible to write data to a database table from an Android app on a button click and also read the data from the same script into the App.Can anyone please help me with this?
My PHP Script is:
<?php
require "conn.php";
require "SalesLogin.php";
$enquiry = $_POST["enquiry"];
$retail = $_POST["retail"];
$collection = $_POST["collection"];
$booking = $_POST["booking"];
$evaluation = $_POST["evaluation"];
$test_drive = $_POST["test_drive"];
$home_visit = $_POST["home_visit"];
$user_name = $_POST["user_name"];
$update_date = $_POST["date"];
$absent = $_POST["absent"];
$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($timezone );
$time = $date->format( 'H:i:s A' );
$sql = "UPDATE employee_details SET
enquiry_sum = (SELECT SUM(enquiry) +'$enquiry' FROM (SELECT * FROM employee_details WHERE date = CURDATE() AND name = '$user_name') AS x)
WHERE date = CURDATE() AND name = '$user_name'";
$res = $conn->query($sql);
$check = "UPDATE employee_details SET enquiry_target_status = ( SELECT IF (MAX(enquiry_sum) = 52, 'ACHIEVED', 'NOT ACHIEVED') FROM (SELECT * from employee_details WHERE date = CURDATE() AND name = '$user_name') AS Y ) WHERE date = CURDATE() AND name = '$user_name'";
$insert_status = $conn->query($check);
$miss_count = "UPDATE employee_details SET enquiry_target_missed_by = (SELECT (50 - MAX(enquiry_sum)) FROM (SELECT * from employee_details WHERE date = CURDATE() AND name = '$user_name') AS Z ) WHERE date = CURDATE() AND name = '$user_name'";
$insert_status = $conn->query($miss_count);
$mysql_qry1 = "INSERT INTO employee_details(enquiry,retail,
collection,booking, evaluation, test_drive, home_visit, name, date,time,absent) values ('$enquiry','$retail','$collection','$booking','$evaluation','$test_drive',
'$home_visit','$user_name','$update_date','$time','$absent');";
if($conn->query($mysql_qry1) === TRUE)
echo "Your details has been successfully inserted.";
else
echo "Error: " .$mysql_qry1. "<br>" . $conn->error;
if($update_date != $date){
$mysql_qry2 = "UPDATE employee_data SET last_updated_date = (DATE_ADD('$update_date', INTERVAL 1 DAY)) WHERE name = '$user_name';";
$conn->query($mysql_qry2);
echo "Date changed," .$mysql_qry2;
}
$mysql_qry3 = "SELECT COUNT(*) from employee_details WHERE date = '$update_date' and name LIKE '$user_name';";
$conn->query($mysql_qry3);
if($mysl_qry3 <= 2)
{
echo "You can login.";
}
else
{
echo "You cannot login anymore for today.";
}
$conn->close();
?>
I want to check the condition and pass the result for the below part and make validations in Android App:
$mysql_qry3 = "SELECT COUNT(*) from employee_details WHERE date = '$update_date' and name LIKE '$user_name';";
$conn->query($mysql_qry3);
if($mysl_qry3 <= 2)
{
echo "You can login.";
}
else
{
echo "You cannot login anymore for today.";
}
The problem I was making is : I did not use "AS" keyword in the PHP Script.
The updated PHP Script is:
$mysql_qry3 = "SELECT COUNT(*) AS count from employee_details WHERE date = '$update_date' and name LIKE '$user_name';";
$result1 = mysqli_query($conn,$mysql_qry3);
$row = mysqli_fetch_assoc($result1);
$count = $row['count'];
echo "Count: " .$count;
if($count <= 2)
{
echo "You can login.";
}
else
{
echo "You cannot login anymore for today.";
}
This gives the desired result.
I have been staring at this code so long it is starting to bleed together. So let's get a couple things out of the way:
I am a low-moderate level SQL user and am still learning the best ways to accomplish queries and such
I have been building a PHP based quote-generator tool that ties in with an SQL database
Now, here is what I am trying to accomplish:
I would like to have pagination with the results of quotes that have been created with a status that is NOT "Dead", "Duplicate", or "Signed" and the ID does NOT equal 999 (because I wanted the quotes to jump to 1000 and I did not build the database correctly at first)
Here is what I have accomplished:
I was able to create the basic structure and it works just fine on page 1... then it all goes sideways. I was thinking that it was something to do with my count(*) query, but that seems fine - well functional, at least:
$countQry = "SELECT COUNT(*)
from needsassessment
WHERE ID <> 999
AND status <> 'Dead'
AND status <> 'Duplicate'
AND status <> 'Signed'";
$countResult = $mysqli->query($countQry);
while($rowQ = mysqli_fetch_array($countResult)) {
$totalRows = $rowQ[0];
$each = ceil($totalRows/$limit);
$where = "WHERE ID > 0";
if ($totalRows == 0) {
echo "You have not created any quotes.";
}
}
$limit = 15;
if (isset($_GET['q']) && $_GET['q'] !== "") {
$offset = $_GET['q'];
} else {
$offset = 0;
}
$query = "SELECT * FROM needsassessment $where
where $and
AND ID <> 999
AND NOT status = 'Dead'
AND NOT status = 'Duplicate'
AND NOT status = 'Signed'
ORDER BY ID DESC LIMIT $offset, $limit";
$result = $mysqli->query($query);
$row_cnt = mysqli_num_rows($result);
if ($row_cnt == 0) {
echo "You have not created any quotes."; die;
}
if (!isset($each)) {
$each = $row_cnt/$limit;
}
if ($each > 1) {
echo "<ul class='pagination'>";
for($i=1,$y=0;$i<=$each,$y<=($each-1);$i++,$y++) {
echo "<li><a";
if ($offset == ($y*15)) {echo ' class="active"';}
echo " href='?q=".($y*15)."'>$i</a></li>";
}
echo "</ul>";
}
Okay, so here are my questions:
I saw someone say that using PHP's mysqli_num_rows() is wrong and
that I should use SQL's COUNT(*). Why? Does it have a different result or take longer?
What is going on with page's
2 and 3? When I do a row count it says it sees 15 rows but only some
are showing. I have some records (i.e. those with a status of
"Signed" or "Dead") that are excluded from this list and I was
thinking that may have been the issue, but it doesn't seem to matter
on the first page.
What is the difference - if there is one -
between "NOT" and "<>" for checking records?
If you have any suggestions or if I have done anything blatantly wrong or inefficiently please let me know so I can correct it :)
Thanks to all that make Stack Overflow AWESOME!
EDIT:
There certainly was some code missing:
if (!isset($sst)) {
$where = "";
$sst = "";
$countQry = "SELECT COUNT(*) from needsassessment WHERE ID <> 999 AND status <> 'Dead' AND status <> 'Duplicate' AND status <> 'Signed'";
$countResult = $mysqli->query($countQry);
while($rowQ = mysqli_fetch_array($countResult)) {
$totalRows = $rowQ[0];
$each = ceil($totalRows/$limit);
$where = "WHERE ID > 0";
if ($totalRows == 0) {echo "You have not created any quotes.";}
}
}
else {
if ($option !== "*") {
$where = "WHERE ".$option." LIKE '%".$sst."%'";
}
if ($option == "*") {
$where = "WHERE status LIKE '%".$sst."%' OR title LIKE '%".$sst."%' OR propType LIKE '%".$sst."%' OR pSType LIKE '%".$sst."%' OR createDate LIKE '%".$sst."%'";
}
$offset = 0;
}
if ($_SESSION['admin'] >= 1) {
$and = "";
}
else {
$and = "AND creatorID = '".$_SESSION['user']."'";
}