Display check out detail group by Vendors Name - php

When I added (Random added) product to cart by different Vendors.
Ex :
Data Tables.
VENDORS TABLE
| vendorid | vendorname |
-------------------------
| 1 | A |
| 2 | B |
| 3 | C |
-------------------------
PRODUCTS TABLE
| productid | productname | postbyvendor(FK) |
----------------------------------------------
| 1 | Shirt1 | 1 |
| 2 | Shirt2 | 1 |
| 3 | Shirt3 | 2 |
| 4 | Shirt4 | 2 |
| 5 | Pants1 | 1 |
| 6 | Pants2 | 3 |
Clicked add to Cart for 6 times.
1st Add Shirt1 from Vendor A to cart
2nd Add Shirt3 from Vendor B to cart
3rd Add Pants2 from Vendor C to cart
4th Add Shirt2 from Vendor A to cart
5th Add Pants1 from Vendor A to cart
6th Add Shirt4 from Vendor B to cart
Cart view displayed like this.
<li>Shirt1</li>
<li>Shirt3</li>
<li>Pants2</li>
<li>Shirt2</li>
<li>Pants1</li>
<li>Shirt4</li>
I want to display check out detail like this.
<li>Vendor Name : A</li>
<li>Shirt1</li>
<li>Shirt2</li>
<li>Pants1</li>
<li>Vendor Name : B</li>
<li>Shirt3</li>
<li>Shirt4</li>
<li>Vendor Name : C</li>
<li>Pants2</li>
My query.
$statement = $mysqli_conn->prepare("SELECT productname, postbyvendor FROM products_tbl WHERE productname = ? order by postbyvendor asc");
Code that i've tried.
foreach($_SESSION["products"] as $product){
$product_name = $product["productname"];
$vendorsid = $product["postbyvendor"];
$cart_box = "<li>$vendorsid $product_name</li>";
}
Displayed like :
<li>1 Shirt1</li>
<li>2 Shirt3</li>
<li>3 Pants2</li>
<li>1 Shirt2</li>
<li>1 Pants1</li>
<li>2 Shirt4</li>
Appreciated.

I think if I were to do this, I would make a function to gather the vendors and their items, then loop through them to display, something like:
function getVendors()
{
foreach($_SESSION["products"] as $product) {
$org[$product["postbyvendor"]][] = $product["productname"];
}
return (!empty($org))? $org : array();
}
foreach(getVendors() as $vendor => $prods) {
echo "<li>{$vendor}</li>";
echo "<li>".implode("</li><li>",$prods)."</li>";
}

Related

Duplicate item record display in table

in my code below I am trying to display the Item that is near to the ReOrder-Point or insufficient Items.
all ReOrder-Point are plus 5. in book Item it should be 6+5=11 it should be in the list
in pencil Item it should be 5+5=10 since it has more quantity it doesn't belong in list of insufficient Items
sample table Item:
| ID | Item name | Quantity | ReOrder-Point |
| 1 | book | 11 | 6|
| 2 | pencil | 25 | 5|
| 3 | shoe | 4 | 5|
| 4 | watch | 20 | 10|
<?php
include("connection/mysqlconnect.php");
$sql="SELECT * FROM items";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)) {
$Item_Name = $row ['Item_Name'];
$Stock_No = $row ['Stock_No'];
$Description = $row ['Description'];
$Quantity = $row ['Quantity'];
$ReOrder_Point = $row ['ReOrder_Point'];
$false_ReOrderP = $ReOrder_Point + 5;
if ($Quantity <= $false_ReOrderP) {
$rItem_Name = $Item_Name;
$rStock_No = $Stock_No;
$rDescription = $Description;
$rQuantity = $Quantity;
$rReOrder_Point = $ReOrder_Point;
} else {
//nothing
}
?>
this is what it displays. it duplicates depends on how many rows i have in my Items table
| ID | Item name | Quantity | ReOrder-Point |
| 1 | book | 11 | 6|
| 1 | book | 11 | 6|
| 3 | shoe | 4 | 5|
| 3 | shoe | 4 | 5|
expected table result should this
| ID | Item name | Quantity | ReOrder-Point |
| 1 | book | 11 | 6|
| 3 | shoe | 4 | 5|
I solved my problem with this.
if anyone has the same query condition I have, feel free to analize :)
$sql1="SELECT * FROM items WHERE Quantity <= (ReOrder_Point + 5)"

Laravel hasManyThrough return nothing

Products:
-----------------------
|id | name | seller_id |
-----------------------
| 1 | bmw | 1 |
-----------------------
| 2 | benz | 1 |
-----------------------
| 2 | reno | 2 |
-----------------------
Buy:
------------------------------
|id | product_id | buyer_id |
------------------------------
| 1 | 1 | 1 |
------------------------------
| 2 | 1 | 2 |
------------------------------
| 3 | 2 | 22 |
------------------------------
Buyer:
------------------------------
|id | name | email |
------------------------------
| 1 | john | # |
------------------------------
| 2 | mike | # |
------------------------------
| 3 | dave | # |
------------------------------
Explaination:
Seller products store in products table, after buyer bought products, it store in buy table. there are buyer table and also seller table, what I want to do is, show those buyer data to seller who has bought seller products.
Consider, a seller with seller_id 1, made products, some buyers bought his/her products, right? now I want to show to seller, who bought your products. john and mike bought bmw, now seller with id 1 should see mike and john email and etc under each products.
What I tried:
ProductController.php:
$pro = Product::where('seller_id', $seller->id)->where('deleted', 0)->with('buyer')->get();
Product.php:
function buyer()
{
//return $this->belongsTo('App\Buy', 'id'); // this just show me buyer_id
return $this->hasManyThrough('App\Buyer', 'App\Buy', 'id', 'id', 'product_id', 'buyer_id');
}
This return me products but buyer is empty "buyer": [], I'm new to laravel, I don't know which localKey, firstKey, secondKey or secondLocalKey related to which table. So any idea what I have done wrong?
-- Edit --
public function userproducts()
{
$seller = Auth::seller();
$pro = Product::where('seller_id', $seller->id)->where('deleted', 0)->with('buyer')->get();
return response()->json($pro, 200 );
}
You're making the wrong relationship. As you explain your Product has many buyers and the buyer can purchase many Products.
So, It'll be a Many-to-Many Relationship.
Try to make relationship as below and then check it'll work for you.
Product.php Model
function buyers(){
return $this->belongsToMany('App\Buyer','Buy_table_name','product_id','buyer_id');
}
In Buyer.php Model
function products(){
return $this->belongsToMany('App\Product','Buy_table_name','buyer_id','product_id');
}
Now, use this query in controller file.
$pro = Product::where('seller_id', $seller->id)->where('deleted', 0)->with('buyers')->get();

Remove values in comma separated list from database on CodeIgniter

Edit :
I give another example,
products_table
| ID | product_name | images |
|---------------------------------------|
| 1 | Phone | img1.jpg,img2.jpg
| 2 | Canon | img1.jpg,img2.jpg,img3.jpg
| 3 | Cocac | img1.jpg,img2.jpg,img3.jpg,img4.jpg
| 4 | Lamps | img1.jpg,img2.jpg,img3.jpg,img4.jpg
Lats say I remove image "img3.jpg" from Lamps product without remove other images. What is the SQL call to this?
$this->db->query("UPDATE products_table SET images=replace(images, 'img3.jpg,',''),images=replace(images, ',img3.jpg','') WHERE product_name='Lamps'");

how to show multiple record in one table row

How to show multiple record in one table row ?
I'm not good explain using words, so i'll use an example :
Work No. | Product No. | Product Name | Qty | Item No. | Item Name | Qty |
--------------------------------------------------------------------------
W00001 | P0000001 | Product_1 | 6 | I000001 | Item_1 | 2 |
| | | | I000002 | Item_2 | 2 |
| | | | I000003 | Item_3 | 2 |
--------------------------------------------------------------------------
Total : 6 |
--------------------------------------------------------------------------
W00002 | P0000002 | Product_2 | 7 | I000001 | Item_1 | 3 |
| | | | I000004 | Item_4 | 4 |
--------------------------------------------------------------------------
Total : 7 |
--------------------------------------------------------------------------
as you can see, product is made from multiple items. So far, i can make more less like example above but my question is how to display item like example above. I want to show items in one row. How to show that using php ?
If you already have 2 tables: one for products and one for items you simply have to loop throw the products one and for each product get all the items needed for that product and display there.
Your code needs to look something like this:
$products = get_products_from_db(); //How you normal get products from db.
foreach($products as $product) {
$items = get_items_by_product_from_db($product['id']); //get from db all items for a product.
//foreach product we will need a new row.
echo '<tr>';
echo "<td>{$product['work_no']}</td>"; //and all the normal cells like this.
//for item no. we will create a cell and inside it we will foreach items.
echo "<td>";
foreach($items as $item) {
echo $item['item_no'] . '<br/>';//Or how it's called in db.
}
echo "</td>";
//Exact the same with name and quantity.
//For quantity also keep a counter so you can display the total
echo '</tr>';
//After this row create a new tr to display the total. I think you know how to do this.
}

PHP and MySql, join three tables and reading

I have 3 database tables, products, categories, and assignment
All three tables have a unique id.
The third table assignment, associates product IDs with the category IDs. An example:
products:
---------------------
| name | id |
---------------------
| white shirt | 234 |
| black pants | 0u2 |
categories:
---------------------
| name | id |
---------------------
| shirts | x23 |
| pants | 28x |
assignment:
------------------------
| catId | itemId | id |
------------------------
| x23 | 234 | 892 |
| 28x | 0u2 | 561 |
I want to create a list. An example:
<items>
<cats>shirts</cats>
<item>white shirt</item>
<cats>pants</cats>
<item>black pants</item>
</items>
It is important that the first category name and then products are listed in the category.
Sql:
select assignment.itemId, assignment.catId from assignment
left join categories
on categories.id = assignment.catId
where categories.active = '1'
My SQL query works fine. The problem is in a while loop to provide list correctly together.
PHP while loop:
if ($res != false && $res->count() > 0) {
$i = 0;
$html = '<items>';
while (!$res->EOF) {
$categories = $res->fields[1];
$html .= '<cats>'.$categories->title.'</cats>';
$products = $res->fields[0];
$html .= '<item>'.$products->title.'</item>';
$i++;
}
$html .= '</items>';
}
In a while loop I can read products fields[0] and categories fields[1], but I can't make a list like I want.
my result is as follows:
shirts
pants
white shirt
black pants
do you have any solutions for me?
thank you!

Categories