i have two transaction on my cart.
while i click checkout, why $grand total only one inserted to my db.
and when i echo $grand, it appear two total.
this is my function
function cart(){
$username = $_SESSION['UserID'];
$cart = array();
$sql = mysql_query("SELECT * FROM cart WHERE Username='$username'");
while ($r=mysql_fetch_array($sql)) {
$cart[] = $r;
}
return $cart;
}
this is my process code
for ($i = 0; $i < $j; $i++){
echo $start= $cart[$i]['City_Saler'];
echo $end= $cart[$i]['City_Cust'];
$price= $cart[$i]['Price'];
$gram= $cart[$i]['gram'] / 1000;
echo $kg= ceil($gram);
$query= mysql_query("select * FROM service WHERE Start_Destination='$start' AND End_Destination='$end'");
while($query_row = mysql_fetch_assoc($query)){
$grand=$query_row['ServPriceKG'] + $price ;
}
}
for ($i = 0; $i < $j; $i++){
mysql_query("INSERT INTO ordering_detail(ID_Transaction, ID_Product, User_ID, Service_Name, Service_Package, Qty, gram, Price, Receiver_Name, Receiver_City, Receiver_Subdis, Receiver_Address, Receiver_Prov, Receiver_Phone, Grandtotal, Payment, Stat_Transfer_Item, ID_Saler)
VALUES('$idtransaction', '{$cart[$i]['ID_Product']}', '$cusid', '$service', '$paket', '{$cart[$i]['qty']}', '$kg', '{$cart[$i]['Price']}', '$name', '$reci', '$subdis', '$address', '$prov', '$phone', '$grand', '-', '-', '{$cart[$i]['ID_Saler']}')");
}
mysql_query("DELETE FROM preview_ordering WHERE ID_Preview= '$id'");
for ($i = 0; $i < $j; $i++) { mysql_query("DELETE FROM cart WHERE ID = {$cart[$i]['ID']}");}
header('Location:cart.php');
please help me out
Related
When i run this php code the counter variable only goes 1 and 2 but i want to make it go 5 what is wrong here anyone can help me ? why the counter dosent go up from 2 ?
<?php
session_start();
include "sql.php";
$date = $_POST['date'];
$email = $_SESSION[email];
$counter = 0;
$check = " select * from reservations where date = '$date'";
$result = mysqli_query($con, $check);
$num = mysqli_num_rows($result);
if($num = 1){
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
echo $counter;
if ($counter < 5){
$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
}
else{
echo "no tables available";
}
}
else{
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
}
I think you should put it on a looping.
Check code below:
<?php
session_start();
include "sql.php";
$date = $_POST['date'];
$email = $_SESSION[email];
$counter = 0;
$check = " select * from reservations where date = '$date'";
$result = mysqli_query($con, $check);
$num = mysqli_num_rows($result);
if($num = 1){
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
echo $counter;
if ($counter < 5){
while($counter <= 5){
//$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
$counter++;
}
}
else{
echo "no tables available";
}
}
else{
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
}
I have this code that should display each record in its own record inside Microsoft Excel.
However it only puts the first record into the excel, Even though I have a while loop so all the records will be inserted. However its not working, do i have any issues in my code? Or is the loop not going to work like this? I have done it before and it worked with no issue, but for some reason this is not working.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$filename = "Charge Report";
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: filename=".$filename.".csv");
if($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT
first_name,
orders.user_id AS 'user_id',
last_name,
email,
city,
line1,
line2,
delivery_zip,
orders.id AS 'orders_id',
charge_status,
subtotal,
charge_date,
delivery_fee,
tax,
tax_amount,
total,
stripe_customer_id,
discount_code_id,
discount
FROM
orders
INNER JOIN subscriptions ON orders.subscription_id = subscriptions.id
INNER JOIN users on subscriptions.user_id = users.id
LEFT JOIN addresses on users.id = addresses.user_id
ORDER BY users.id";
$result = mysqli_query($conn,$query);
?>
Client ID,First/Last Name,Email,Address,City,Zipcode,Order ID,Charge Date,Charge Status,Subtotal,Delivery Fee, Delivery Fee Discount,Discount Code, Discount Type,Promo Code Discount Amount,Tax Rate, Tax Amount,Net Charge Amount,Net Total Amount,Total Amount, Number of Meals,Client ID Stripe
<?php
while ($row = mysqli_fetch_assoc($result))
{
$name = $row['first_name']. " " . $row['last_name'];
$address = $row['line1']. " " . $row['line2'];
$subtotal = $row['subtotal'];
$delivery_fee = $row['delivery_fee'];
if($subtotal >= 30000)
{
$delivery_fee_discount = $delivery_fee + 1000;
}
elseif($subtotal >= 20000)
{
$delivery_fee_discount = $delivery_fee;
}
elseif($subtotal >= 10000)
{
$delivery_fee_discount = 1000;
}
else{
$delivery_fee_discount = 0;
}
$tax_amount = $row['tax_amount'];
$total = $row['total'];
$discount_code_id = $row['discount_code_id'];
$net_total_amount = $total - $tax_amount;
$discount = $row['discount'];
$total_amount = $subtotal + $tax_amount +$delivery_fee;
$query_discount="select * from discount_codes where id = '{$discount_code_id}'";
$result = mysqli_query($conn,$query_discount);
$row_discount = mysqli_fetch_assoc($result);
$discount_name = $row_discount['code'];
$discount_type = $row_discount['discount_type'];
$discount_amount = $discount - $delivery_fee_discount;
if($discount <= 0)
{
$discount_amount = 0;
}
if($discount_type == 3)
{
$discount_amount = $row['delivery_fee'];
}
$get_orders= "select SUM(num_meals) as `meal_total` from orders left join deliveries on orders.id = deliveries.order_id where orders.id = '{$row['orders_id']}'";
$r_get_orders = mysqli_query($conn, $get_orders);
$qty = mysqli_fetch_assoc($r_get_orders);
$meal_counter = $qty['meal_total'];
$user_id = $row['user_id'];
$user_id = (explode(" ",$user_id));
?>
<?=#$user_id[0] ?>,<?=$name ?>,<?=$row['email'] ?>,<?=$address?>,<?=$row['city']?>,<?=$row['delivery_zip']?>,<?=$row['orders_id']?>,<?=$row['charge_date']?>,<?=$row['charge_status']?>,<?=$subtotal?>,<?=$delivery_fee?>,<?=$delivery_fee_discount?>,<?=$discount_name?>,<?=$discount_type?>,<?=$discount_amount?>,<?=$row['tax']?>,<?=$row['tax_amount']?>,<?=$row['total']?>,<?=$net_total_amount?>,<?=$total_amount?>,<?=$meal_counter?>,<?=$row['stripe_customer_id']?>
<?php
}
?>
You overwrite $result inside while($row = mysqli_fetch_assoc($result)) {} at your second query:
$result = mysqli_query($conn,$query_discount);
So rename it here and in next line to something like $result_discount:
$result_discount = mysqli_query($conn,$query_discount);
$row_discount = mysqli_fetch_assoc($result_discount);
I have the following script:
<?php
ini_set('max_execution_time', 0);
include("../includes/mysqli.php");
$pointsvar = 50000;
$inserts = 0;
$locationvar = 32000006;
while($locationvar <= 32000260){
while($pointsvar >= 20000){
$url = "http://185.112.249.77:9999/Api/Search?search=&level=0&min=1&max=50&points=$pointsvar&loc=$locationvar";
$jsondata = file_get_contents($url);
$data = json_decode($jsondata, true);
$in=$data['clanList'];
$results = $data['results']; //max is 64
$i = 0;
while($i + 1 <= $results){
$clanid = $in[$i]['id'];
$clanname = mysqli_real_escape_string($con,$in[$i]['name']);
$clanplayerCount = $in[$i]['playerCount'];
$clanwarswon = $in[$i]['warsWon'];
$clanwarslost = $in[$i]['warsLost'];
$clanwarstied = $in[$i]['warsTied'];
$clanLocation = mysqli_real_escape_string($con,$in[$i]['clanLocation']);
$clanlevel = $in[$i]['level'];
$score = $in[$i]['score'];
$sql = "INSERT INTO activeclans(id, name, location, playercount, clanlevel, warswon, warslost, warstied, score)
VALUES('$clanid', '$clanname', '$clanLocation', '$clanplayerCount', '$clanlevel', '$clanwarswon', '$clanwarslost', '$clanwarstied', $score)";
mysqli_query($con, $sql);
$i++;
$inserts++;
}
$pointsvar = $pointsvar-500;
sleep(1);
}
$locationvar++;
sleep(1);
}
echo "Inserted $inserts";
?>
When I run it I am expecting it to go through each location and in each location I expect it to -500 from the $pointsvar until it reaches 20000. It was working until I made it go through each location in a while loop and now it just outputs Inserted 0
I have increased the max_execution_time as it could possible take a looong time to run. This script will be run on a cron around every day or week.
The expected output would be Inserted and a very very very big number..
Thanks for any help you can provide :D
Use for loops instead of while loops to ensure that your variables get initialized properly each time.
$inserts = 0;
for ($locationvar = 32000006; $locationvar <= 32000260; $locationvar++){
for ($pointsvar = 50000; $pointsvar >= 20000; $pointsvar -= 500){
$url = "http://185.112.249.77:9999/Api/Search?search=&level=0&min=1&max=50&points=$pointsvar&loc=$locationvar";
$jsondata = file_get_contents($url);
$data = json_decode($jsondata, true);
$in=$data['clanList'];
$results = $data['results']; //max is 64
for ($i = 0; $i < $results; $i++){
$clanid = $in[$i]['id'];
$clanname = mysqli_real_escape_string($con,$in[$i]['name']);
$clanplayerCount = $in[$i]['playerCount'];
$clanwarswon = $in[$i]['warsWon'];
$clanwarslost = $in[$i]['warsLost'];
$clanwarstied = $in[$i]['warsTied'];
$clanLocation = mysqli_real_escape_string($con,$in[$i]['clanLocation']);
$clanlevel = $in[$i]['level'];
$score = $in[$i]['score'];
$sql = "INSERT INTO activeclans(id, name, location, playercount, clanlevel, warswon, warslost, warstied, score)
VALUES('$clanid', '$clanname', '$clanLocation', '$clanplayerCount', '$clanlevel', '$clanwarswon', '$clanwarslost', '$clanwarstied', $score)";
mysqli_query($con, $sql);
$inserts++;
}
sleep(1);
}
sleep(1);
}
echo "Inserted $inserts";
Also, maybe the innermost loop should be foreach ($data['clanList'] as $clan) -- can the number of clans in the clanList array be different from $data['results']?
And you can speed up INSERT queries by inserting multiple rows with a single query:
INSERT INTO tablename (columns...) VALUES (...), (...), (...), ...
So in your script, you could concatenate all the values during the loop over clans, and then insert that batch at the end of that loop. So it would look like:
$values = array();
for ($i = 0; $i < $results; $i++){
$clanid = $in[$i]['id'];
$clanname = mysqli_real_escape_string($con,$in[$i]['name']);
$clanplayerCount = $in[$i]['playerCount'];
$clanwarswon = $in[$i]['warsWon'];
$clanwarslost = $in[$i]['warsLost'];
$clanwarstied = $in[$i]['warsTied'];
$clanLocation = mysqli_real_escape_string($con,$in[$i]['clanLocation']);
$clanlevel = $in[$i]['level'];
$score = $in[$i]['score'];
$values[] = "('$clanid', '$clanname', '$clanLocation', '$clanplayerCount', '$clanlevel', '$clanwarswon', '$clanwarslost', '$clanwarstied', $score)";
}
$sql = "INSERT INTO activeclans(id, name, location, playercount, clanlevel, warswon, warslost, warstied, score)
VALUES " . implode(', ', $values);
mysqli_query($con, $sql);
$inserts += count($values);
You are not resetting $pointsvar = 50000; when the second loop is completed. I would expect that the second loop would therefore only run on the first time round the outer loop. So
<?php
ini_set('max_execution_time', 0);
include("../includes/mysqli.php");
$pointsvar = 50000;
$inserts = 0;
$locationvar = 32000006;
while($locationvar <= 32000260){
while($pointsvar >= 20000){
$url = "http://185.112.249.77:9999/Api/Search?search=&level=0&min=1&max=50&points=$pointsvar&loc=$locationvar";
$jsondata = file_get_contents($url);
$data = json_decode($jsondata, true);
$in=$data['clanList'];
$results = $data['results']; //max is 64
$i = 0;
while($i + 1 <= $results){
$clanid = $in[$i]['id'];
$clanname = mysqli_real_escape_string($con,$in[$i]['name']);
$clanplayerCount = $in[$i]['playerCount'];
$clanwarswon = $in[$i]['warsWon'];
$clanwarslost = $in[$i]['warsLost'];
$clanwarstied = $in[$i]['warsTied'];
$clanLocation = mysqli_real_escape_string($con,$in[$i]['clanLocation']);
$clanlevel = $in[$i]['level'];
$score = $in[$i]['score'];
$sql = "INSERT INTO activeclans(id, name, location, playercount, clanlevel, warswon, warslost, warstied, score)
VALUES('$clanid', '$clanname', '$clanLocation', '$clanplayerCount', '$clanlevel', '$clanwarswon', '$clanwarslost', '$clanwarstied', $score)";
mysqli_query($con, $sql);
$i++;
$inserts++;
}
$pointsvar = $pointsvar-500;
sleep(1);
}
$locationvar++;
// RESET The pointsvar counter
$pointsvar = 50000;
sleep(1);
}
echo "Inserted $inserts";
?>
How to insert multiple data in the database? when I insert data that has multiple orders, only one item is inserted in the database. I need help putting this into a loop.
Here's the code I'm currently using:
foreach ($_SESSION["cart_array"] as $each_items){
$item_id = $each_items['item_id'];
$quantity = $each_items['quantity'] ;
$sql = mysql_query("SELECT * FROM product WHERE id = '$item_id'");
while($row = mysql_fetch_array($sql)){
$product_name = $row['name'];
$price = $row['price'];
$total_price = $price * $quantity;
mysql_query("INSERT INTO customer_order(
id,quantity,item_id,
total_price,shipping_address,
shipping_date,customer_id)
VALUES ('','$quantity','$item_id','$total_price',
'','',
'$lastId')") or die (mysql_error());
}
}
Here's what I tried but it's generating a syntax error:
foreach ($_SESSION["cart_array"] as $each_items){
$item_id = $each_items['item_id'];
$item_id_count = count($item_id) ;
$quantity = $each_items['quantity'] ;
$sql = mysql_query("SELECT * FROM product WHERE id = '$item_id'");
while($row = mysql_fetch_array($sql)){
$product_name = $row['name'];
$price = $row['price'];
$total_price = $price * $quantity;
foreach($i=0,$i < $item_id_count,$i++){
mysql_query("INSERT INTO customer_order(
id,quantity,item_id,
total_price,shipping_address,
shipping_date,customer_id)
VALUES ('','$quantity','$item_id','$total_price',
'','',
'$lastId')") or die (mysql_error());
}
}
}
How can I correctly write the loop?
You have written foreach($i=0,$i < $item_id_count,$i++) where I think you meant
for ($i=0 ; $i < $item_id_count ; $i++ )
I have a table as shown below
The output from said table
Green, is where a winner has been announced, whereas yellow means they have submitted a report, but a winner hasn't been decided yet. (winner field not populated)
$roundBound = Array();
$query = "SELECT MIN(id), MAX(id) FROM $tablename GROUP BY round";
if($result = $Login->mysqli->query($query)) {
while ($row = $result->fetch_row()) {
$roundBound[] = $row[0];
$roundBound[] = $row[1];
}
}
for($i = 0; $i < count($roundBound); $i = $i + 2) {
$match = 0;
for($j = $roundBound[$i]; $j < $roundBound[$i + 1]; $j = $j + 2) {
$id1 = $j;
$id2 = $j+1;
$query = "SELECT t1.winner, t2.winner FROM $tablename as t1, $tablename as t2 WHERE t1.id=$id1 AND t2.id=$id2";
$result = $Login->mysqli->query($query);
$row = $result->fetch_row();
$win1 = $row[0];
$win2 = $row[1];
if (isset($win1) && isset($win2)) {
if (isset($roundBound[$i + 2])) {
$id = $roundBound[$i + 2] + $match;
$query = "UPDATE $tablename SET
username = '$win1',
username2 = '$win2'
WHERE id = $id";
$Login->mysqli->query($query);
} else {
//Tourneydonneee yeeee
}
}
$match++;
}
}
Does anyone have a improvement, or an all-around different idea? This will be running quite often and could be potentially running on large record sets, so optimization would also be welcome.
as far as i understand you problem, i can suggest you to try
for ($i = 0; $i < count($arr); $i = $i + 2) {
$win1 = mysql_query("SELECT winner FROM tableName WHERE id=".(int)$i);
$win2 = mysql_query("SELECT winner FROM tableName WHERE id=".(int)$i+1);
$qry = "INSERT INTO tableName SET
username = $win1,
username2 =$win2,
....";
mysql_query($qry);
}