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.
}
Related
i want to fetch multiple columns of mysql table for each unique id and echoing the result in a single row of a HTML table for each unique id.I am getting the result in the following form from the table ..
userid | date | status
------------------------------------
1 | 28-06-17 | 1
------------------------------------
1 | 29-06-17 | 2
------------------------------------
1 | 30-06-17 | 0
------------------------------------
2 | 28-06-17 | 1
------------------------------------
2 | 29-06-17 | 2
------------------------------------
2 | 30-06-17 | 3
------------------------------------
Here is the code..just a basic php code to get the record from database..
if($con)
{
$query="SELECT * FROM `usersDetail` WHERE userid='$_POST["user_id"]'";
$result=mysqli_query($con, $query);
while($rowusersDetail=mysqli_fetch_assoc($result))
{
echo $rowusersDetail['userid']."<br>";
echo $rowusersDetail['date']."<br>";
echo $rowusersDetail['status']."<br>";
}
}
*what i want is to generate html table using PHP to display all the Records of a
particular user into a single row..
userid | 28-06-17 | 29-06-17 | 30-06-17
_________________________________________
1 | 1 | 2| 0
_________________________________________
2 | 1 | 2| 3
_________________________________________
kindly please Help..thanks
This is can be done with the help of foreach loop.
For that you need to retrieve all data at a time then you can manipulate the data with your requirement.
You can do like this
If your data is like
$data=[['userid'=>1,'date'=>'21-03-2017','status'=>2],
['userid'=>2,'date'=>'21-03-2017','status'=>1],
['userid'=>1,'date'=>'22-03-2017','status'=>6],
['userid'=>2,'date'=>'22-03-2017','status'=>7],
['userid'=>1,'date'=>'24-03-2017','status'=>2],
['userid'=>2,'date'=>'24-03-2017','status'=>6]];
Then
$finalData=[];
foreach ($data as $stats){
$finalData[$stats['userid']][$stats['date']]=$stats['status'];
}
foreach ($finalData as $id=>$value){
echo 'userId->'.$id;
echo '<br>';
foreach ($value as $date=>$status){
echo $date.' -> '.$status; echo '<br>';
}
echo '<br>';
}
It will give out put as
I thing it will work for you.
I have a stock table that has several items oderd by users and the item can be having different unit prices depending on the time they were ordered, so you can find an item has more than 5 entries with different prices like the below table:
+----+----------+----------+------------+-------------+-------+
| ID | unitCost | quantity | totalPrice | orderNumber | item |
+----+----------+----------+------------+-------------+-------+
| 1 | 20 | 5 | 100 | OD003 | Pen |
| 2 | 15 | 3 | 45 | OD004 | Pen |
| 3 | 22 | 10 | 220 | OD005 | Books |
| 4 | 13 | 7 | 91 | OD006 | Pen |
+----+----------+----------+------------+-------------+-------+
So am trying to get 10 pens from the table. And the logic should be like this
When I take the first and they are not enough from the first row, I will go to the next row and get the remaining quantity and if it will not be enough it will go to the next till I get the 10 pens that I needed.
Then the price of the 10 pens I get from the table will be the average of unit price of each pen i got.
Please help me solve this in mysql query.
I would get the result from mysql and put it inside array to loop it.
$total_order = 40; $price = 0;
foreach($result as $key => $value) {
if ($value['quantity'] < $total_order) {
$price += $value['quantity']*$value['unitCost'];
} else {
$price += $total_order*$value['unitCost'];
break; //exit loop and stop calculating
}
$total_order = $total_order-$value['quantity'];
}
You may need to tweak the code above a bit to meet your criteria. e.g: Pen price only.
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>";
}
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!
I have two tables. One for orders, and for their prefs. The tables look like this:
Order:
+----------+---------------+------------+
| orderID | orderNumber | clientID |
+----------+---------------+------------+
| 1 | abc123 | 2 |
| 2 | orderX | 7 |
| 3 | Joe9 | 2 |
| 4 | Order4 | 2 |
+----------+---------------+------------+
OrderPref
+----------+----------+-------------+
| orderID | prefID | prefValue |
+----------+----------+-------------+
| 1 | 1 | $100 |
| 1 | 2 | 123 |
| 1 | 3 | $35 |
| 2 | 1 | $600 |
| 2 | 2 | 876 |
| 2 | 3 | $44 |
+----------+----------+-------------+
What I want to is for each order, get the prefValue for a specific prefID. Currently, this is what I am doing:
$orders = OrdersQuery::create()->filterByClientID(2)->find();
foreach($orders as $o){
$prefs = $o->getOrderPrefs();
foreach($prefs as $p){
if($p->getPrefID() === 2){
echo $p->getPrefValue();
break;
}
}
}
This works, but there needs to be a better way to get the one row I want for each order without looping through all the prefs.
I know this doesn't work, but is there something like this?
$orders = OrdersQuery::create()->filterByClientID(2)->find();
foreach($orders as $o){
// This obviously doesn't work, so is there a short way to do this?
echo $o->getOrderPrefs()->filterByPrefID(2)->getPrefValue();
}
I was reading the docs and found a ->search() method, but I don't know how to use it.
$orders = OrdersQuery::create()->filterByClientID(2)->find();
foreach($orders as $o){
// How can I search for the row with the prefID I want?
echo $o->getOrderPrefs()->search()->getPrefValue();
}
Looking at some old Propel stuff I've written, I'm guessing something like:
$prefValue = OrdersQuery::create()->
joinOrderPref()->
where('OrderPref.prefID = ?', $prefId)->
filterByClientID($clientId)->
select('OrderPref.prefValue')->
find()
;
The issue is that you need to get a specific column (reference).
For these chainable queries, my view is that an auto-completing editor is pretty much mandatory - remembering the syntax is nigh-on impossible without it.
$prefValues = OrdersQuery::create()
->filterByClientId(2)
->joinOrderPref()
->withColumn('OrderPref.PrefValue', 'theValue')
// row above fetches ONLY the needed column from joined table and
// gives 'theValue' alias to it
->select('theValue')
->find();
With this you don't need to do any foreaches in PHP or do it in two steps.
About the ->select() part - it's there only to not get the fully bloated/hydrated object, but actually only an array (PropelArrayCollection to be precise) with the prefValue data you need.
I didn't test this live though, some syntax issues may arise.