I'm working on something that calculates me total weight of my class and average weight of my class. In $T I entered weight of girls in class, in
Echo "SUM OF WHOLE CLASS IS : $s";
must be SUM of weight of whole class and
echo "<br/>AVERAGE WEIGHT IS $p";
is average weight of class.. I dont see where the problem is, it just says that is on line number 4..
<body>
<?PHP
$T=array (47,47,62,60,71,55,50,52,62,80,65);
$s=0; $BUB=0;
for ($BUB=0;$BUB<=11;$BUB++)
{
$s=$s+$T[$BUB];
$BUB++;
}
$p=$s/11;
echo "SUM OF WHOLE CLASS IS : $s";
echo "<br/>AVERAGE WEIGHT IS $p";
?>
</body>
Make it simple using the array functions of PHP !
<?php
$T=array (47,47,62,60,71,55,50,52,62,80,65);
echo "SUM OF WHOLE CLASS IS : ".array_sum($T); //"prints the sum
echo "<br/>AVERAGE WEIGHT IS ".array_sum($T)/count($T); //"prints" the average
Demo
Try replacing that: $BUB<=11 with $BUB<11.
Notice that $BUB start with the value 0. as well as the array. so $T[11] isn't exists.
Related
HI just found a solution about add and sustract two columns here but I need to make the php code to format the numbers.
Here is the solution I got here:
<?php
while ($row= mysqli_fetch_assoc($result)) {
echo $row['money'] - $row['cost'];
}
?>
Its working fine for what I need but here is the thing. I use to run the follow php code to all money and numbers
<?php echo number_format($product['cost'],0, ",", "."); ?>
That way the numbers will display without decimals and like this 100.000
How can I make the code I found here to work with number format?
Something like this I tried but does not work
<?php
while ($row= mysqli_fetch_assoc($result)) {
echo number_format$row['money'] - number_format$row['cost'];
}
?>
It must substract and then show the result with formated numbers
you need to put it in ()
echo number_format($row['money'] - $row['cost'],0,',','.');
More than 2 :
echo number_format($row['money'] - $row['cost'] + $row['whatever'] * $row['whatever'],0,',','.');
Hi I have made script that shows only even numbers and now i have to do that shows only 5 even numbers per row example:
numbers must be sorted like this
2,4,6,8,10
12,14,16,18,20
i must have numbers written like that current code that i am using for showing only even numbers is under. but if someone can help me how can i show only 5 per row i will be thankful. Thanks in advance.
<?php
$p=100;
for($p=100;$p>=0;)
{
echo "$p,";
$p=$p-2;
}
?>
Try something like this
<?PHP
$p=100;
$count=0;
for($p=100;$p>=0;)
{
echo "$p";
$p=$p-2;
$count+=1;
if($count==5){
$count=0;
echo "<br/>";
}
else echo " ,";
}
?>
One-liner:
echo implode("\n",array_map(function($a) {return implode(",",$a);},array_chunk(range(0,100,2),5)));
Result:
0,2,4,6,8
10,12,14,16,18
20,22,24,26,28
30,32,34,36,38
40,42,44,46,48
50,52,54,56,58
60,62,64,66,68
70,72,74,76,78
80,82,84,86,88
90,92,94,96,98
100
Function reference: implode, array_map, array_chunk, range.
For rendering in a browser, replace \n with <br />.
try the following
<?php for($p=100;$p>=0;$p-2){
echo "$p";
if($p%5==0){
echo "<br>";
}
else {
echo ", ";
}
}
?>
I have two pages index.php and currentmovies.php. I am trying to display movie name for a given movie in currentmovies.php, but I am unable to display the entire name, only the first word is being displayed. I have written my query in index.php and I am passing the values to currentmovies.php.
Eg: if the movie name is "Lord Of the Rings" only "Lord" is being displayed
//Code in index.php
echo "";
//code in currentmovies.php
<?php
echo "<b><font size='3'>Current Movie:",$_GET['movie_name']," </font></b>";
echo "<p> The movie ",$_GET['movie_name']," was released on ",$_GET['rdate']," and falls under the ",$_GET['genre']," category. </p>";
Thanks for the help
//code in currentmovies.php
<?php
echo "<b><font size='3'>Current Movie: ".$_GET['movie_name']."</font></b>";
echo "<p>The movie ".$_GET['movie_name']." was released on ".$_GET['rdate']." and falls under the ".$_GET['genre']." category.</p>";
Try this:
Pleae do it this way:
<?php
echo '<b><font size="3">Current Movie: '.$_GET['movie_name'].' </font></b>';
echo '<p> The movie '.$_GET['movie_name'].' was released on '.$_GET['rdate'].' and falls under the '.$_GET['genre'].' category. </p>';
Please remember, in this case dot instead of comma.
And one more thing:
If you don't use any variables inside quota, please use single quota, plus for html is much better and easier use single quota, in example:
<font size="3">
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 want to display price in my front page. But the format of the number required is,
Entered price: 10000 Display: 10,000
Entered price: 10000.10 Display: 10,000.1
Entered price: 10000.01 Display: 10,000.01
if I am using the following code
echo number_format ($price,2,'.',',');
But through this the result is displayed in this manner.
Entered price: 10000 Display: 10,000.00
Please help me to solve this problem
There's a function in PHP called money_format().
Have a look at it on http://php.net/manual/en/function.money-format.php
You have set number of decimial points to 2 so that is why you have 10,000.00. Try to user in this way:
echo number_format ($price,1,'.',',');
And also it is better to use money_format if you are working with money values.
Surely, from a clarity and consistency point of view, having 2 digits after the decimal point makes more sense especially when showing prices.
#barryhunter made a valid point and the following doesn't work.
echo rtrim(number_format($price,2,'.',','),'0.');
However, this does:
trim(trim(number_format($price,2,'.',','),'0'),'.');
Look:
<?php
$a=array('10000.00','10000.10','10000.01');
foreach ($a as $price)
{
echo $price.' - '.rtrim(rtrim(number_format($price,2,'.',','),'0'),'.')."\n";
}
?>
$> php -f t.php
10000.00 - 10,000
10000.10 - 10,000.1
10000.01 - 10,000.01
Personally I would do
echo number_format($price,floor($price)==$price?0:2,'.',',');
displaying a price as 10,000.1 just looks odd to me.
But if you really must
$bits = explode('.',$price);
echo number_format($price,strlen($bits[1]),'.',',');
(edit) In reply to the comment, it works for me...
<?php
$a=array(10000.00,10000.10,10000.01);
foreach ($a as $price)
{
$bits = explode('.',$price);
echo $price.' - '.number_format($price,strlen($bits[1]),'.',',')."\n";
}
?>
$ php t.php
10000 - 10,000
10000.1 - 10,000.1
10000.01 - 10,000.01