HTML table grid in PHP - php

I am currently doing some basic PHP and I am getting products from mysql and displaying each product with the corresponding details in a table.
Currently they are one underneath each other
Product1 Name
Product1 Price
Product1 Description
Product2 Name
Product2 Price
Product2 Description
Now I want to display them in a grid of 3 columns.
which means displaying 3 products side by side, then the 4th product is displayed underneath the 1st product and so on.
$displayAllProducts.=
"
<tr><td>Product Name : </td><td>$productName</td></tr>
<tr><td>Product Price : </td><td>$productPrice</td></tr>
<tr><td>Product Qty : </td><td>$productQty</td></tr>
<tr><td colspan =\"2\"><img src=\"$imagePath\" width = \"100\" height = \"100\"></td><td></td></tr>
<tr><td colspan =\"2\"><a href=\"singleProduct.php?pid=$productID&uid=$uid\">View Product<br/><br/><br/></td><td></td></tr>
";
<table>
<?php
echo $displayAllProducts;
?>
</table>

Without seeing your actual code that loops through your db rows, here is a general idea. This will make each $displayAllProducts its own table, nested in the main table <td>'s
$i=1; // start a general counter
while($i<$number_of_db_rows){
if($i%3 = 1) { // If number is 1,4,7,etc start a new row
$displayAllProducts.= "<tr>";
}
$displayAllProducts.=
"
<td> // put each db row inside a cell
<table> // create a bounding table
<tr><td>Product Name : </td><td>$productName</td></tr>
<tr><td>Product Price : </td><td>$productPrice</td></tr>
<tr><td>Product Qty : </td><td>$productQty</td></tr>
<tr><td colspan =\"2\"><img src=\"$imagePath\" width = \"100\" height = \"100\"></td><td></td></tr>
<tr><td colspan =\"2\"><a href=\"singleProduct.php?pid=$productID&uid=$uid\">View Product<br/><br/><br/></td><td></td></tr>
</table>
</td>
";
if($i%3 = 0) { // If number is 3,6,9,etc close the row
$displayAllProducts.= "</tr>";
}
$i++ // increase the counter to start again
} // ends the loop
<table>
<?php
echo $displayAllProducts;
?>
</table>

Related

How do we cumulative sum if product has same id in CodeIgniter foreach loop

I have a sales table with the product name, number of sales, and product id. I need to add the number of each sale if the product id is the same; else, leave it as is. I have attached one image for more understanding. On the first table T-shirt, three times sold and the key holder sold for only one. My result should be like the second table. T-shirt first row's daily sale 3, T-shirt 3rd row's everyday sale should be 5 (3+2). T-shirt 4th row's everyday sale should be 8 (3+2+3). How can I get a cumulative sum like this if it has the same ID?
View
<?php if (!empty($orders)){foreach($orders as $order) { ?>
<tr>
<td><?php echo $order['item_name']; ?></td>
<td><?php echo $order['txt_stock_Quantity']; ?></td> <!-- DAILY SALE -->
<td> <?php echo $order['items_id']; ?> </td>
</tr>
<?php $slNo++; }} else{ ?>
<tr>
<td colspan="18">No Data Found</td>
</tr>
<?php } ?>
Model
$this->db->order_by("order_id", "DESC");
$this->db->where('MONTH(created_at)', date('m'));
$this->db->where('YEAR(created_at)', date('Y'));
$this->db->join('centralized_item',
'centralized_item.item_id = live_sale_report.items_id');
$this->db->join('stock_shop', 'stock_shop.shop_id = live_sale_report.shop_id');
$query = $this->db->get("live_sale_report");
return $query->result_array();

PHP MySQLi - Display Product Based On Product Category

I have around 5 meal category, for example 'M01' ('M' stands for Meal), 'B01' ('B' stands for Baverages) and so on. In every category page on front end, instead of showing everything in 1 page, I want to sort the product based on its category, for example in Baverages page, there will be a list of baverages under 'B01' category code. How can I achieve that with PHP and MySQLi? Sorry for the simple code, I'm still new to this. Thanks in advance!
Here's my code:
<table class="table-list">
<tbody id="mealTable">
<?php
$result=mysqli_query($conn,"SELECT * FROM meal");
$row_count = mysqli_num_rows($result);
if ($row_count == 0) { ?>
<tr>
<td colspan = "6">
<?php echo "No data found"; ?>
</td>
</tr>
<?php } else {
while($row = mysqli_fetch_array($result)) { ?>
<tr>
<td><?php echo $row['meal_name']?></td>
<td>RM <?php echo $row['meal_price']?></td>
<td >Add To Cart
</td>
</tr>
<?php } }
?>
</div>
This is the List of Meal page on Admin side
This is the database table for Meal Category
This is the database table for Meal

Hiding an item if product name doesn't exist

As the title says, I want to hide an item is the product name doesn't exist.
The problem here is.. When it show the products for the user, it actually show nothing where the //show nothing is placed instead of showing a new product that has a product name.
So sometime it shows 10 products, sometime it shows 5 products, which is not what i want.
I want it to show a new product if a product name is not there.
Any clues on what i change in my code?
my current code is:
Picks 12 random product from the database:
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY RAND() LIMIT 12");
Shows the products for the user:
if($product_name == ''){
//Show nothing
}else{
$dynamicList .=
'<table border="0" id="indexproducts" style="margin-left:22px; margin-bottom:20px;">
<tr>
<td id="indexproductfoto" align="center">
<a href="http://www.mysite.com/id/'.$id.'/'.$seo8.'/">
<img src="http://www.mysite.com/inventoryimages/'.$id.'.jpg" onerror="this.src=\'http://www.mysite.com/inventoryimages/x.jpg\';" />
</a>
</td>
</tr>
<tr>
<td id="indexproducttext2"><strong>'.$product_name.'</strong><br />
</td>
</tr>
<tr>
<td style="color:#F00" id="indexproducttext"><strong>Pris: '.round($price).':-</strong></td>
</tr>
<tr>
<td style="color:#F00" id="indexproducttext"><strong>(Exkl.moms: '.round($price1).':-)</strong></td>
</tr>
<tr>
<td id="indexproducttext">Produktinformation</td>
</tr>
</table>';
}
}
As I commented
try this SQL query
SELECT * FROM products WHERE IFNULL(productname, "") <> "" ORDER BY RAND() LIMIT 12
where the operator <> means "not equal" that is same as != So you look for all the products whose name is not empty and whose name is not NULL.
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_not-equal
I would suggest to simply rewrite the query that only products with a productname are returned.
SELECT * FROM products WHERE productname NOT NULL ORDER BY RAND() LIMIT 12

How to categorized results from SQL, display 6 columns and set colorize on every category

I would like to ask for help on how to achieve this. I have this code below that pull records from DB and display it in 6 columns.
What I want to achieve is that, I want to display results on 6 columns but I want to categorize and set different color on every category.
let say i want to display the whole set of fruits starting from letter A with 6 columns with colors of gray, then below all letters starting with B with 6 columns with white color on background then below is C with gray colors in 6 columns. thanks.
<?php
fruits = $stmt->prepare("SELECT * FROM fruits ORDER by fruit_id ASC");
$fruits->execute();
$cols = 6;
do {
echo "<tr>";
for ($i = 1; $i <= $cols; $i++) {
$row = $fruits->fetch(PDO::FETCH_ASSOC);
if ($row) {
$fruit_id = $row['fruit_id'];
$fruit_name = $row['fruit_name'];
?>
<td>
<table>
<tr valign="top">
<td>
<?php echo '<input type="checkbox" id="fruit_id[]" name="fruit_id[]" value="' . $fruit_id . '"/>' . $fruit_name . "\n"; ?>
</td>
<td width="30"> </td>
</tr>
</table>
</td>
<?php
} else {
echo "<td> </td>";
}
}
} while ($row);
?>
Do it in the database:
SELECT fruits.*,if(#evenodd>0,'grey','white') AS color, #evenodd:=-1*evenodd AS dummy FROM (#evenodd:=1) as init, fruits ORDER by fruit_id ASC

How to display list of high scores on a game in two tables?

I want to display a list of top ten players on the high scores list, in a table from database i want it in a format that one half of the div element it should display top 5 players and other half of player ranking 6-10.Currently my code is as given below any one help me out...
echo('<table id="pattern-style-a" summary="Meeting Results">');
echo('<thead><tr><th scope="col">Profile</th><th scope="col">Score</th></tr>
</thead><tbody>');
while ($row = mysql_fetch_assoc($result)) {
$count++;
?>
<tr><td><?=image($row['user_id']);?><br /><?=$row['name']?></td><td><?=$row['result']?></td></tr>
<?
}
?>
</tbody>
</table>
You could try structuring the table like this:
<table>
<tr><td>
<table [1-5]>
</td><td>
<table [6-10]>
</td></tr>
</table>

Categories