i have the following table with values from a sql query:
now depending on the hour of the day, i want to be able to change the values.
for example, its late in the afternoon, i choose the option 3 dagen EUR 20.00. Since right now this day is already almost over, i should pay a smaller amount for 1 dag (example EUR 4), and then the price for 2 dagen EUR 14.00, so at this hour of the day the box 3 dagen should show EUR 18.00
So i know how to get the current hour, but how can i change my query to reflect this?
This is the query i used to get this table:
<?php
echo "<h2>".$lang['pagina3_dagen']."</h2>";
$number=="0";
echo "<table class='prijs'>";
echo "<tr>";
$query="SELECT * FROM $tabel WHERE fietstype='$fietskeuze'";
$result=mysql_query($query) or die ("Ophalen prijzen mislukt: ".mysql_error());
while ($row=mysql_fetch_array($result)) {
$days=$row[days];
$price=$row[price];
if ($days=="1") $writeday=$lang['pagina3_1dag'];
if ($days >="2") $writeday=$lang['pagina3_meerderedagen'];
echo "<td class='prijs'><h2><a href='framemaat.php?lang=".$_SESSION['lang']."&naam=".$naam."&postcodehuisnummer=".$postcodehuisnummer."&fietskeuze=".$fietskeuze."&opties=".$opties."&optieid=".$optieid."&days=".$days."'>".$days." ".$writeday."<br>EUR ".$price."</a><br>";
$number++;
if ($number=="5") {echo "<tr>"; $number="0";}
}
echo "</tr>";
echo "</table>";
?>
i was thinking in pseudo-like code something like this:
$currenthour = date("H", time());
if (($currenthour >="13") && ($currenthour <"15"))
{
$pricedag1=($pricedag1-3eurodiscount);
price in table result for 3 dagen=($pricedag1+$priceofdag2)
}
but how to get the price of the previous day? so the second to last value coming from the query?
I'm not entirely sure I understand the details of your table, but here's a shot:
Why not have a separate table that holds discounts for your products (dagens?) ? This way, you can query your products and join it against the discount table.
// you can decide the data type
CREATE TABLE Discounts (discount_id, product_id, start, end, discount);
then you could left join with your product table
SELECT p.*, d.discount
FROM products p
LEFT JOIN discounts d ON d.product_id = p.product_id
AND start <= ? AND end >= ?;
So after you've received your result set, you can check whether discount is empty or not. If it's not, apply the necessary discount.
if (!empty($row['discount']) {
$price = $price - $row['discount'];
}
Of course, you could take it a step further and determine types of discounts - percentage off, dollar off, etc.
Related
I'm trying to create a quick-fix script in order to delete some records and get the data back on track.
Originally I had sales records inserting and using a quantity value to create an equal number of records. So I if I inserted a record with a quantity value of 12, then I would insert 12 of that record. This gave each SKU it's own record so that individual ones could have dates updated.
However, this has changed and I now need to do it based on store locations rather than quantity so I have too many records in some cases.
If I run the script with the current query (for dealer number 145) it selects all skus they have ordered this year, as expected. I need to now run a delete statement that takes a row count of each sku and compares it to the storefront value from the join.
So, if storefronts = 10 and there are 25 records for that sku/dealer combo, then I need to delete the oldest 15. The logic here is for each sku/dealer combo where curdate() < expire_date there should only be x number of rows equal to storefronts
I'm trying to test this with dealer 145, who has 10 stores. So for all records this year where curdate() is before the expiration, there should be no more than 10 records for each sku, if that makes sense.
$selectRecords = "
SELECT p.sku_id, p.dealer_id, p.start_date, p.expire_date, p.quantity, s.storefronts, s.dealer_id
FROM placements p
inner join storefronts s on p.dealer_id = s.dealer_id
WHERE p.dealer_id = 145
and start_date > '2018-01-01'
order by start_date asc;
";
try{
$currentRecords = $MysqlConn->prepare($selectRecords);
$currentResult = $currentRecords->execute();
$currentCount = $currentRecords->rowCount();
echo "Open Orders: " . $currentCount . "\n"; //returns correct count
}catch(PDOException $ex){
echo "QUERY FAILED!: " .$ex->getMessage();
}
$currentArray = [];
while($row = $selectRecords->fetch(PDO::FETCH_ASSOC)){
//pseudo code
foreach(sku_id){
if(rowCount > storefronts){
delete starting with oldest start_date
until row count == storefronts
}
}
}
I feel like it shouldn't be too complicated but I'm really stuck on how to execute this exactly.
Here's a fiddle with data examples:
http://sqlfiddle.com/#!9/c0dd6
I have a database with many records (major columns are Date, Company Names, Closing Price. What i want to achieve is to use php to display all unique company names in the first column, most recent price in the 2nd (using date), Highest Price of all unique companies in database in the 3rd column and lowest price of all unique companies in the database. Please, how can i achieve this?
$query = "SELECT date, company, MIN(close), MAX (Close) FROM pricelist GROUP BY company";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
for ($i=0;$i<$num_rows;$i++){
$id = mysql_result($result,$i,"id");
$date = mysql_result($result,$i,"date");
$company = mysql_result($result,$i,"company");
$close = mysql_result($result,$i,"close");
echo "<tr bgcolor=\"#d7dde3\"><td align=right class=no_text>".$company."</td><td align=right class=norm_text>".number_format($close, 2, '.', ',')." </td><td align=right class=norm_text>".$date." </td></tr>";
}
Here is what i am trying to achieve:
Company Name Year High Date Year Low Date
Google $20 02/17/2014 $10 05/13/2014
Apple $40 01/22/2014 $34 04/05/2014
Select all records by date, then use php arrays, asort() - low->high, arsort() - high->low.
Find more about php array here: PHP Array Sorting
You can try it, but not tested
SELECT DISTINCT company, price, MAX (price),MIN(price) FROM pricelist
ORDER BY date DESC
I am trying to produce a shopping site where I am selling items that fluctuate in price daily (precious metals). I have a table (products) that will contain a multiplier (something like "1.1") for each product. I basically don't want to have to go into my tables and change the prices of hundreds of items every day. My idea is to create another table where I will simply change the price field with the daily value each day. How can I make the final product price total the multiplier from one table multiplied by the entered daily price from another table. Or, is there a better way than using two tables? Here's the coding so far using just one table with a defined price :
if (isset($_GET['id'])) {
//Connect To Mysql Database
include"storescripts/connect_to_mysql.php";
$id = preg_replace('#[^0-9]#i','',$_GET['id']);
//Use This VAR To Check To See If This ID Exists, If Yes Then Get Product
//Details, If No Then Exit Script and Give Message Why
$sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");
$productCount = mysql_num_rows($sql);
if ($productCount > 0) {
//Get All The Product Details
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$date_added = strftime("%b %d, %Y",strtotime($row["date_added"]));
}
} else {
echo "That Item Does Not Exist";
exit();
}
} else {
echo "Data To Render This Page Is Missing";
exit();
}
mysql_close();
So, how about a response that isn't the usual mysql_* related pedantry?
In the following schema I've broken the materials table away from the listed price so that they can be stored according to date. You might find this useful for records and/or invoicing.
TABLE products
prod_id INT PK
prod_name VARCHAR
prod_price DECIMAL
mat_id INT FK
...
TABLE materials
mat_id INT PK
mat_name VARCHAR
...
TABLE material_pricing
mat_id INT FK PK
mat_price_date DATE PK
mat_price_factor DECIMAL
SELECT
p.prod_name,
p.prod_price * pr.mat_price_factor AS 'cur_price'
FROM products p INNER JOIN materials m
ON p.mat_id = m.mat_id
INNER JOIN material_pricing pr
ON m.mat_id = pr.mat_id
WHERE mat_price_date = TODAY()
I'm trying to think of a way to change the query to grab the last defined material_pricing entry for the relevant material, but I'm having a tough time lining up the data for a subquery...
edit: this should do the trick
SELECT
p.prod_name,
p.prod_price * pr.mat_price_factor AS 'cur_price'
FROM products p INNER JOIN materials m
ON p.mat_id = m.mat_id
INNER JOIN (
SELECT p1.*
FROM material_pricing p1 INNER JOIN (
SELECT mat_id, MAX(mat_price_date) 'mat_price_date'
FROM material_pricing
WHERE mat_price_date <= $target_date
GROUP BY mat_id
) p2
ON p1.mat_id = p2.mat_id
AND p1.mat_price_date = p2.mat_price_date
) pr
ON p.mat_id = pr.mat_id
Where $target_date in the innermost subquery would be replaced by either today's date, the TODAY() mySQL function, or the date of the invoice being displayed.
I'm working on a job at the minute where all the products within the store need to be brought in and show how many orders have been put through in-between that time period which was easy to do, but it also in the same table needs to split the quantity of the orders of that product into the separate months that it was ordered in, is there any way to expand the data from one cell that's been group and do a count on it and then separate it into sub months?
$this->obDb->query ="SELECT vSku,fOrdertime,iOrderStatus,fPrice,SUM(iQty) as iQty,tShortDescription,fBuyprice as totalBuyPrice, vTitle FROM ".ORDERS.",".ORDERPRODUCTS." WHERE iOrderid_FK=iOrderid_PK AND ";
$this->obDb->query.=$statusquery;
if(isset($this->request['start_date']) & $this->request['start_date']>0){
$this->obDb->query.=" AND tmOrderDate >='".$this->request['start_date']."'";
}else{
$this->err=1;
$this->errMsg=INVALID_START_DATE."<br>";
}
if(isset($this->request['end_date']) & $this->request['end_date']>0){
$this->obDb->query.=" AND tmOrderDate <='".$this->request['end_date']."' GROUP BY vTitle";
}else{
$this->err=1;
$this->errMsg.=INVALID_END_DATE;
}
if($this->err==0){
$queryRs = $this->obDb->fetchQuery();
$recordCount=$this->obDb->record_count;
if($recordCount>0){
$this->ObTpl->set_var("TPL_VAR_FROMDATE", $this->libFunc->dateFormat2($this->request['start_date']));
$this->ObTpl->set_var("TPL_VAR_TODATE", $this->libFunc->dateFormat2($this->request['end_date']));
for($i=0;$i<$recordCount;$i++)
{
$this->ObTpl->set_var("TPL_VAR_STATUS","Incomplete");
if($queryRs[$i]->iOrderStatus==1){
$this->ObTpl->set_var("TPL_VAR_STATUS","Complete");
}
If I understand what you're asking you could try
SELECT datecol, COUNT(id)
FROM your_table
WHERE datecol BETWEEN date1 AND date2
GROUP BY productId, YEAR(datecol), MONTH(datecol)
ORDER BY datecol
I have a table called expenses. There are numerous columns but the ones involved in my php page are date, spplierinv, amount. I have created a page that lists all the expenses in a given month and totals it at the end. However, each row has a value, but many rows might be on the same supplier invoice.This means adding each row with the same supplierinv to get a total as per my bank statement. Is there anyway I can get a total for the rows based on the supplierinv. I mean say I have 10 rows. 5 on supplierinv 4, two on supplierinv 5 and 3 on supplierinv 12, how can a get 3 figures (inv 4, 5 and 12) and the grand total at the bottom. Many thanks
Do you need to show the individual expense rows as well as all the totals? If not, you can group by the supplierinv column and sum the amounts:
select supplierinv, sum(amount) as invoice_amount
from expenses group by supplierinv
then you can simply add all the invoice_amount values client-side to get the grand total.
if you do need each expense row, you can do the invoice aggregation client-side fairly easily:
$invtotals = array();
$grand_total = 0;
foreach (getexpenses() as $row) {
$supplierinv = $row['supplierinv'];
if (!array_key_exists($supplierinv, $invtotals)) {
$invtotals[$supplierinv] = 0;
}
$invtotals[$supplierinv] += $row['amount'];
$grand_total += $row['amount'];
}
another approach, if you want to show the table split up into invoices, is to just ensure the results are ordered by supplierinv and then:
$grand_total = 0;
$invoice_total = 0;
$current_invoice = -1;
foreach (getexpenses() as $row) {
if ($current_invoice > 0 && $current_invoice != $row['supplierinv']) {
show_invoice_total($current_invoice, $invoice_total);
$grand_total += $invoice_amount;
$current_invoice = $row['supplierinv'];
$invoice_total = 0;
}
$invoice_total += $row['amount'];
show_expense($row);
}
if ($current_invoice > 0) {
show_invoice_total($current_invoice, $invoice_total);
$grand_total += $invoice_amount;
}
show_grand_total($grand_total);
You can have the database do the aggregation for each invoice and the grand total like so:
select date, supplierinv, amount,
sum(amount) over(partition by supplierinv) as invoice_amount,
sum(amount) over()
from expenses
although you still have to do some post-processing to display this sensibly, so this doesn't really gain anything imho.