I'm having issues displaying total quantity of all items at the bottom of my PDF export script I'm using with my Wordpress site.
I've been able to display total quantity of each item using
<td class="qty"><?php echo esc_html( $item->get_quantity() ) ?></td>
Then I want to calculate the sum of all item quantities and show that as a single figure at the bottom of the table
Would I use
echo sum_array ( $item->get_quantity() )
I hope you are showing each quantity via some loop, if yes then before loop define a variable like
$sum = 0;
Now change <td> code like this (inside loop):
<td class="qty">
<?php
$sum += (int)$item->get_quantity();
echo esc_html( $item->get_quantity() ) ?>
</td>
And now in the end use this $sum variable to show total quantity.
<?php echo $sum;?>
Related
I have a table that is full of percentages, my values are pulled from an SQL database. I want to change a css element (bg color, color, etc.) depending on the value drawn from SQL. E.g. 0-10% = red, 20-20% = orange, etc. I saw this Changing colour of text in table cell with php and js but I think my insertion of the % prevents it from working. This is my table code, I have no other code yet.
<tr>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
</tr>
<tr>
<td class="rating"><?php echo $wor['rjan'];?>%</td>
<td><?php echo $wor['rfeb'];?>%/td>
<td><?php echo $wor['rmar'];?>%</td>
<td><?php echo $wor['rapr'];?>%</td>
<td><?php echo $wor['rjun'];?>%</td>
<td><?php echo $wor['rjul'];?>%</td>
<td><?php echo $wor['raug'];?>%</td>
<td><?php echo $wor['rsep'];?>%</td>
<td><?php echo $wor['roct'];?>%</td>
<td><?php echo $wor['rnov'];?>%</td>
<td><?php echo $wor['rdec'];?>%</td>
</tr>
Rather then a solution to a specific problem I'm asking for a suggestion of a method.One way I thought to do it is to have a long php ifelse statement for each sql field that changes the style of class of each cell. However this would require writing the statement out 12 times, which seems like a long way around.
Create a PHP function that will accept the percentage as an argument, put it through several if-statements and return the desired color.
The function can look like:
function get_color($num_str) {
$num = intval($num_str);
if ($num < 10) return 'red';
if ($num < 20) return 'orange';
// ...
}
And use of this function can then be:
<td class="<?php get_color($wor['rfeb']); ?>">
<?php echo $wor['rfeb']; ?>%
</td>
You could always create a function to do it. (Also the < is missing on the closing td on the rfeb row.)
For the html, make each row (obvious css classes could be used here too):
<td style="backbround-color: <?= color($wor['rmar']) ?>;"><?php echo $wor['rmar'];?>%</td>
the add a function to the php
<?php
function color($amt) {
switch (floor($amt/10)) {
case 0: return 'red';
case 1: return 'orange';
// ...
default: return 'transparent';
}
}
First of all, you don't have to use so many if-else or switch-case blocks to get the right percentage range and the corresponding color. Use a simple a function like this:
function getColor($percentage){
$perArray = array(10 => 'red', 20 => 'orange', 30 => 'green', ...);
if ($percentage % 10)
$percentage = $percentage + (10 - $percentage % 10);
return $perArray[$percentage];
}
In the above function, $perArray array would be interpreted like this:
10 => 'red' means a percentage value of 0-10 would be assigned red color
20 => 'orange' means a percentage value of 11-20 would be assigned orange color
30 => 'green' means a percentage value of 21-30 would be assigned green
and so on
And this is how you should call the getColor() function for each table row:
<tr>
<th>Jan</th>
...
<th>Dec</th>
</tr>
<tr>
<td style="color:<?php echo getColor($wor['rjan']); ?>;"><?php echo $wor['rjan'];?>%</td>
<td style="color:<?php echo getColor($wor['rfeb']); ?>;"><?php echo $wor['rfeb'];?>%</td>
...
<td style="color:<?php echo getColor($wor['rdec']); ?>;"><?php echo $wor['rdec'];?>%</td>
</tr>
You'd only have to write it out twelve times because you're explicitly echoing the <td> for each month. If you put it into a loop instead, you can write the logic for the formatting once.
The other answers that suggest creating a function to determine the formatting you'll use can be combined with this. (Putting that logic into a function seems like a good idea to me, but you'll still need to call the function 12 times.)
I suggest having that function return the name of a predefined (by you, or some CSS framework, e.g. bootstrap, etc.) CSS class that you can apply to the cells rather than using it to explicitly set background color, etc. in an inline style. I just made up a name (format_percentage_cell) for example, but whatever you want to call it, you know. And the other answers have some good ideas for starting points for your implementation.
<tr>
<?php foreach ($wor as $month): ?>
<td class="<?= format_percentage_cell($month) ?>"><?= $month ?>%</td>
<?php endforeach ?>
</tr>
As you only want a sugestion, I would do the following:
You only have to calculate the bytes. 255 is a maximum on each (3 in total excluding the alpha channel) color range this would be your 100% in percentages. This can easily be applied if you add a style attribute to the tr element with the rgb() function.
So if you want red (lowbound) to green (highbound) you only have to do the following:
function yourfunction($percentage){
return 'rgb('. round((255 / 100) * $percentage) .', '. round((255 / 100) * (100 - $percentage)).', 0)';
}
So adjust the function to your color likings for example red to green and directly print the value out to the <tr style="color: <?= yourfunction($percentage)?>" >value</tr>
I need help with a loop in opencart.
I want to display 2 - 3 product next to each other other on the product page. similar to how products are displayed in on the compare page. I've achieved the product limit via code already and I've managed to display everything else, pictures, price etc. My issue is the attributes.
I know that i need to reach into this array
$this->data['products'][] = array(
......
'attribute' => $attribute_data,
I've also included this line of code
$attribute_groups = $this->model_catalog_product->getProductAttributes($product_id);
my loop is
<?php foreach ($attribute_group['attribute'] as $key => $attribute) { ?>
<tbody>
<tr>
<td><?php echo $attribute['name']; ?></td>
<td><?php echo $attribute['text']; ?></td>
<?php } ?>
<?php } ?>
This gets me all attributes of the current product but displays errors for any attributes not filled in if there are any related products.
Then I have no idea on how to reach into the array and display the related product attribute details.
please help
<?php if isset($attribute['name'];); {?>
<td><?php echo $attribute['name']; ?></td>
<?php } ?>
Try this; the error will not be shown.
Firstly I would like to apologise if this question has been answered somewhere else but I am unable to find what I am looking for as I am new to PHP and assume I need this to solve my problem.
I have built a website and am using Mals-e shopping cart. I have everything up and running but I would like to show how many products are still in stock under the product description and if there are no items in stock. For example:
Available stock: 2
or
Sold Out
I have read that I need a text file with product name, price and quantity, a PHP file to read and rewrite the quantity available and out put the results on the product page and a mypage.php page but I really don't know where to start. I've spent days trying to sort this out.
I have Mysql database with some items in table called (items) with available quantity but don't know how to go about sorting it out. Any help would be most appreciated.
Thank you.
Without seeing the actual code you're using to display the product, it's hard to say buy all you should need is something like:
<?php
// get the product and stock level
if($product->numberInStock > 0) {
echo 'Available: ' . $product->numberInStock;
} else {
echo 'Out of stock';
}
If you're editing a phtml type template (HTML with embedded PHP), you might display it like:
<? if($product->numberInStock > 0): ?>
<p>Available: <?= $product->numberInStock; ?></p>
<? else ?>
<p>Out of stock</p>
<? endif; ?>
Had same issue, found out following.
Inside your catalog/product type template you can use this:
<?php
$_product = $this->getProduct();
$_qty = $_product->getStockItem()->getQty();
?>
<p>
<?php if($_qty > 0): ?>
Available: <?php echo $_qty; ?>
<?php else: ?>
Out of stock
<?php endif; ?>
</p>
session_start($_POST['quantity']);
if(isset($_POST['quantity']))
{
$postedquantity=$_POST['quantity'];
$productQuantity="20";
if($postedquantity<$productQuantity){
echo "In stock";
echo "<br/>";
}
$productQuantity=$productQuantity-$postedquantity;
echo $productQuantity."Remaining";
}
You can even do like this,storing quantity value in session and everytime it's posted it will check whether it is in stock or not and it will show how much quantity is remaining.
I have a product called customized gift box with different sizes like 5,9,12 etc.
When the users select items to the gift box and add to cart, I am fetching those chocolate names too with the Quantity of the chocolate.
In the code below, I have written if the product name includes customized then fetch the related chocolate using session in a for loop.
And the For loop is working properly but, sometimes it doesn't.
How can this be resolved?
<?php
if(stristr($this->getProductUrl(),"customized")){
?>
<div>
<?php
echo "<br/>";
$itid = $_item->getId();
echo $itid;
$strrep = str_replace(' ','_',$this->htmlEscape($this->getProductName()));
for($k=1;$k<=sizeof($_SESSION[$strrep."item".$itid]);$k++){
if($_SESSION[$strrep."item".$itid][$k]!=""){
echo " <font size='1px'>".$_SESSION[$strrep."qty".$itid][$k]." x ".$_SESSION[$strrep."item".$itid][$k]."</font><br/>";
}
}
?>
</div>
Your for loop starts with 1 and finishes with the sizeof's element. Array indices are zero based in PHP so you should do
for ($k=0;$k<count($_SESSION[$strrep."item".$itid]);$k++){
...
}
instead. I also replaced sizeof() by count() but that is a matter of preference ...
I am wondering how to change this :
This code is using a table and I instead trying to use a div, but I wanna define the div the same way it's done with enter <td>. In <td class= '".$class." thum'> here
Like enter <div class='".$class." thumb'> here
enter <td ' class='".$class." thumb'><div align='center'><a href='show-".$row['id'].".php'><img src='".$row['image']."' width='80' height='60' /></a></div></td> here
I am trying to change color on each iteration of loop. That's what $class is there.
I don't think this is clear at all. It's hard to explain it
Thanks in advance
You could define a new value within each loop prior to echoing the output. For instance, if you wanted "zebra stripes," you could check if the current iteration is even or odd:
for ( $i = 0; $i < count( $items ); $i++ ) {
$style = ( $i % 2 == 0 ) ? $styleA : $styleB ;
echo "<td class='{$style} thumb'>...</td>";
}
Sample Output: http://codepad.org/8cYyiBBH