How do I insert the round code in echo? - php

I want the number to have 2 decimal places because it shows all the decimal numbers. Here's my code:
<?php
$add=mysqli_query($conn,'SELECT SUM(fil), SUM(math), SUM(aralp),
SUM(mapeh), SUM(esp), SUM(mtb), (SUM(total)/5), SUM(mps) from `grade1`');
while($row1=mysqli_fetch_array($add))
{
$mark=$row1['SUM(fil)'];
$mark3=$row1['SUM(math)'];
$mark4=$row1['SUM(aralp)'];
$mark5=$row1['SUM(esp)'];
$mark6=$row1['SUM(mapeh)'];
$mark7=$row1['SUM(mtb)'];
$mark9=$row1['(SUM(total)/5)'];
$mark10=$row1['SUM(mps)'];
}
?>
How do I insert the round(2) here?
<?php
echo "<th></th><th>AVERAGE</th>";
echo "<th>$mark</th>";
echo "<th>$mark3</th>";
echo "<th>$mark4</th>";
echo "<th>$mark5</th>";
echo "<th>$mark6</th>";
echo "<th>$mark7</th>";
echo "<th>$mark9</th>";
echo "<th>$mark10</th>";
?>
Thank you in advance!

This is it actually
echo "<th>".round($mark, 2)."</th>";
Now, I'll have to face another problem.

Related

creating a space before a variable including htmlencode str_repeat

I have a variable given to me by my cms
I want to add one space before that but have tried many things lol and all dont work, below is what I have tried so far.
<?php echo str_repeat('', 1) htmlencode('$postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode('&nbsp$postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode('. $postcode_coveringsRecord['postcode'].) ?>
<?php echo '&nbsp''htmlencode('$postcode_coveringsRecord['postcode'])' ?>
<?php echo htmlencode('&nbsp''$postcode_coveringsRecord['postcode']) ?>
How can I minipulate
where as the variable gives me one blank space prior to the variable content.
cheers for any input
emma
These ones should work
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);
or
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);

How to use plurals correctly when using the sizeof function in php?

When the page display results, I want it to say say "We found X products" or "We found 1 Product" rather than "we found 1 products".
The relevant part of my current code is this:
<?php echo sizeof($ids); ?> Products</strong>
How could I extend it to behave more natural?
If count is more than one echo "s".
<?php echo sizeof($ids); ?> Product <?php If(count($ids)>1) echo "s"; ?></strong>
Or you make it easier to read like this:.
If(count($ids)>1) {
Echo sizeof($ids) . " Products.</strong>";
}Else{
Echo sizeof($ids) . " Product.</strong>";
}

how to access value which passed through url?

I have trying to access value of variable from one page to another by using following code:-
page1.php
<td>
echo $var12=$row['p_id'];
echo 'Received';
</td>
receive_branch_confirmation.php
<?php echo $_GET['prop_id']; ?>
My previous error solved but now it is just print ",$var12,"
please tell me where is my mistake
As you are using echo for displaying the a tag you no need to use echo or close and open php tags.
It is enough if you concatenate the variable that you are going to send.
$var12 = '2';
echo 'Received';
Then your URL will look like as follows
http://domain.com/receive_branch_confirmation.php?prop_id=2
And in the receive_branch_confirmation.php you can access the variable passed with the help of $_GET or $_REQUEST
<?php
echo 'Request Value: '.$prop_id = $_REQUEST['prop_id']; // this will result the output as 2
echo '<br>';
echo 'Get Value'.$prop_id = $_GET['prop_id']; // this will result the output as 2
?>
Output:
Request Value: 2
Get Value: 2
Another Example:
If the URL is http://domain.com/page.php?var1=apple
then if you need to access the variable as follows in page.php.
<?php
$u1 = $_GET['var1'];
$u2 = $_REQUEST['var1'];
echo $u1; //would output "apple"
echo $u2; //would output "apple"
?>
page1.php
<td>
<?php echo '<a href=receive_branch_confirmation.php?prop_id='.$var12.'>Received</a>'; ?>
</td>
receive_branch_confirmation.php
<?php
echo $_GET['prop_id'];
?>
It should be
Received
in html page and
<?php
echo $_GET['prop_id'];
?>
in php page
and always try to separate html from PHP
You need to update your page1.php as below
echo 'Received';
and get it like this
<?php
echo $_GET['prop_id'];
?>
Try this in html
<td>
<a href="receive_branch_confirmation.php?prop_id="<?php echo $var12;?>>Received</a>
</td>
in php side
<?php
echo $_GET['prop_id'];
?>
You need to change the $_GET value.
<?php
echo $_GET['prop_id'];
?>
Now it's looking for the prop_id instead of the value

PHP sorting 5 numbers per row

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 ", ";
}
}
?>

Concatenation within echo of array

Very simple but this echo isn't returning anything. I want to concatenate 2 values from an included PHP array file so I don't have to write the code twice. What am I writing incorrectly?
<?php echo $lang['work' . 'title']; ?>
Among others I have tried
<?php echo $lang['work', 'title']; ?>
<?php echo $lang['work' 'title']; ?>
<?php echo $lang['work'], $lang['title']; ?>
Check out php array documentation.

Categories