if($_REQUEST['action'] == 'addToCart' && !empty($_REQUEST['id'])){
$productID = $_REQUEST['id'];
// get product details
$sql = "SELECT * FROM products WHERE id = ".$productID;
$stmt = $db->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$itemData = array(
'id' => $row['id'],
'name' => $row['name'],
'price' => $row['price'],
'qty' => 1
);
$insertItem = $cart->insert($itemData);
$redirectLoc = $insertItem?'viewCart.php':'index.php';
header("Location: ".$redirectLoc);
I am trying to get the the prod name, qty and price - to be placed on viewCart.php. I am new please help me with this.
Related
I am getting no error while batch update of array data. The submit and show me no error. Could you please help me to figure this problem out.
$invoice_id = $this->input->post('id');
$desc = $this->input->post('desc[]');
$size = $this->input->post('size[]');
$stock = $this->input->post('stock[]');
$color = $this->input->post('color[]');
$qty = $this->input->post('qty[]');
$price = $this->input->post('price[]');
$linetotal = $this->input->post('linetotal[]');
$invoice_id = $this->input->post('id');
$input_data = array();
for ($i = 0; $i < count($desc); $i++ )
{
$input_data[] = array(
'desc' => $desc[$i],
'size' => $size[$i],
'stock' => $stock[$i],
'color' => $color[$i],
'qty' => $qty[$i],
'price' => $price[$i],
'linetotal' => $linetotal[$i],
);
}
$this->db->where('invoice_id', $invoice_id);
$this->db->update_batch('invoice_data',$input_data, 'invoice_id');
$this->session->set_flashdata('success', 'Data added successfully.');
redirect('invoices');
I am using id field in my view too. But can't get the data updated.
I am trying to sum up the value. I have stored in the database price and qty. The total = price * qty for that particular item.
I would like to sum all of the items for a sub total.
$sql = "SELECT * FROM parts WHERE jobno = '$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$results['entrants'][] = array(
'partno' => $row['partno'],
'description' => $row['description'],
'price' => $row['price'],
'qty' => $row['qty'],
'total' => $row['price'] * $row['qty'],
);
}
} else {
// Create an empty placeholder record if no entrants were found
$results['entrants'][] = array(
'partno' => NULL,
'description' => NULL,
'price' => NULL,
'qty' => NULL,
'total' => NULL,
);
}
I only know SELECT sum - which I don't think will work because I am not getting the information from the database but from the above.
Just keep track as you go:
$subTotal = 0;
while ($row = $result->fetch_assoc()) {
$lineTotal = $row['price'] * $row['qty'];
$subTotal += $lineTotal;
$results['entrants'][] = array(
//...
'total' => $lineTotal;
);
}
$results['subtotal'] = $subTotal;
I am trying to retrieve the data from mySQL and generate as JSON file.
But the output is just showing only 1 row.
There are many rows inside the database table.
What is wrong with this code?
$sql = "SELECT * FROM form_element";
$result = mysqli_query($conn, $sql);
$response = array();
$data_array = array();
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($data = mysqli_fetch_assoc($result)) {
$id = $data['id'];
$name = $data['name'];
$email = $data['email'];
$phone = $data['phone'];
$address = $data['address'];
$data_array = array(
'name' => $name,
'email' => $email,
'phone' => $phone,
'address' => $address
);
}
} else {
echo "0 results";
}
$response['data_array'] = $data_array;
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
You're overwriting $data_array every time in the loop.
So, this part:
$data_array = array(
'name' => $name,
'email' => $email,
'phone' => $phone,
'address' => $address
);
should be changed to:
$data_array[] = array(
'name' => $name,
'email' => $email,
'phone' => $phone,
'address' => $address
);
Then each row is added to the $data_array.
Ok so I have two tables Category and Dishes. Each Category has an id. And each dish belongs to a particular category. Now what I want is for all dishes belonging to a particular category to be grouped together. So All the Soups will be in the Soups Array [{Cabbage,Price,Description,...}, {Tomato,Price,Description,...}]. All the chicken in the chickens array and so forth. What I'm Currently doing is this:
$query = "Select id from Category";
$Id = $mysqli->query($query);
while($row = $Id->fetch_assoc()) {
$idlist[] = $row["id"];
}
foreach ($idlist as $id){
$query = "SELECT * from Dishes where Dishes.cat_id = '$id'";
$Listings = $mysqli->query($query);
while($row = $Listings->fetch_assoc()) {
$AllDishes[] = array(
'id' => $row['id'],
'Name' => $row['Name'],
'Description' => $row['Description'],
'cat_id' => $row['cat_id'],
'Price' => $row['Price'],
'ImagePath' => $row ['ImagePath']
);
}
}
But this results in all the dishes being grouped together. How do I separate the dishes based on their category? Also is this the right way to structure JSON Data?
1.Use JOIN to do that in single query (best solution):-
$query = "Select Dishes.*,Category.id as cat_id from Category LEFT JOIN Dishes on Dishes.cat_id = Category.id";
$Listings = $mysqli->query($query);
$AllDishes = array();
while($row = $Listings->fetch_assoc()) {
$AllDishes[$row['cat_id']][] = array(
'id' => $row['id'],
'Name' => $row['Name'],
'Description' => $row['Description'],
'cat_id' => $row['cat_id'],
'Price' => $row['Price'],
'ImagePath' => $row ['ImagePath']
);
}
print_r($AllDishes);
If you want to do in your coding way then:-
2.Distinguish them based on id of the category:-
$query = "Select id from Category";//change column name
$Id = $mysqli->query($query);
while($row = $Id->fetch_assoc()) {
$idlist[] = $row['id'];
}
foreach ($idlist as $id){
$query = "SELECT * from Dishes where Dishes.cat_id = '$id'";
$Listings = $mysqli->query($query);
while($row = $Listings->fetch_assoc()) {
$AllDishes[$id][] = array(
'id' => $row['id'],
'Name' => $row['Name'],
'Description' => $row['Description'],
'cat_id' => $row['cat_id'],
'Price' => $row['Price'],
'ImagePath' => $row ['ImagePath']
);
}
}
3.Distinguish them based on name of the category:-
$query = "Select id,category_name from Category";//change column name
$Id = $mysqli->query($query);
while($row = $Id->fetch_assoc()) {
$idlist[] = $row;
}
foreach ($idlist as $id){
$id = $id['id'];
$category_name = $id['category_name']; //change column-name
$query = "SELECT * from Dishes where Dishes.cat_id = '$id'";
$Listings = $mysqli->query($query);
while($row = $Listings->fetch_assoc()) {
$AllDishes[$category_name][] = array(
'id' => $row['id'],
'Name' => $row['Name'],
'Description' => $row['Description'],
'cat_id' => $row['cat_id'],
'Price' => $row['Price'],
'ImagePath' => $row ['ImagePath']
);
}
}
Note:- Please change the column-name in 3rd solution(as i don't know what column-name you have to store category names.)
I am not sure but i thinks here will be your while loop for expected results
$AllDishes = array();
while($row = $Listings->fetch_assoc()) {
$AllDishes[$row['Name']][] = array(
'id' => $row['id'],
'Name' => $row['Name'],
'Description' => $row['Description'],
'cat_id' => $row['cat_id'],
'Price' => $row['Price'],
'ImagePath' => $row ['ImagePath']
);
}
You can use SQL joins instead of two separate SQL queries
I hope the following may help you.
$query = "SELECT * FROM Dishes INNER JOIN Category
ON Dishes.id = Category.id ORDER BY Dishes.id";
$res = $mysqli->query($query);
while($row = $res->fetch_assoc())
{
$AllDishes[$$row['category_name']][] = array(
'id' => $row['id'],
'Name' => $row['Name'],
'Description' =>
$row['Description'],
'cat_id' =>
$row['cat_id'],
'Price' => $row['Price'],
'ImagePath' =>
$row['ImagePath']
);//Array Close
}
I can only add one item to my cart at a time.
The previous item I add to the cart will just get replaced here is my method I use:
public function addcart(){
if(isset($this->session->userdata)){
$type = $this->session->userdata('type');
$username = $this->session->userdata('username');
$this->db->select('id_product ,price');
$query = $this->db->get('product', array('title'=> $this->input->post('title')));
$cart['product'] = $this->cart->contents();
if($query->num_rows() >0){
$row = $query->row();
$id = $row->id_product;
$cart['product'][$id] = array(
'id' => $row->id_product,
'qty' => $this->input->post('quantity'),
'price' => $row->price,
'name' => $this->input->post('title'),
//'options' => array('Size' => 'L', 'Color' => 'Red')
);
$this->cart->insert($cart['product'][$id]);
}
}
}
check what $cart['product'] = $this->cart->contents(); returns to you
if there is a problem there it probably reset your array of products and then you insert the second product and get a total cart of one product
I think you are using $this->db->get() badly, you are searching product by string, which is not "correct". What happens if you have same (title) identical products? You get the first one/second one?
please adjust your table as follows
id, title, price... 'custom fields', active
make id as autoincrement, active as boolean (so you can later turn on/off product)
Make your method accept parameters:
public function addcart( $id = FALSE ) {
if ($id === FALSE || !is_numeric($id)) {
//wrong ID do nothing or throw alert or inform user about it
redirect('');
}
if(isset($this->session->userdata)) {
$type = $this->session->userdata('type');
$username = $this->session->userdata('username');
$this->db->select('id_product ,price');
$query = $this->db->get('product', array('id' => $id, 'active' => '1'));
$cart['product'] = $this->cart->contents();
if($query->num_rows() >0) {
$row = $query->row();
$id = $row->id_product;
$cart['product'][$id] = array(
'id' => $row->id_product,
'qty' => $this->input->post('quantity'),
'price' => $row->price,
'name' => $this->input->post('title'),
//'options' => array('Size' => 'L', 'Color' => 'Red')
);
$this->cart->insert($cart['product'][$id]);
}
}
}
Happy cod(e)igniting.