PHP sorting 5 numbers per row - php

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

Related

How do I insert the round code in echo?

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.

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 solve my query with single for loop?

How can I write the code for this in PHP language?
*
**
***
I want to print this stars shape in php, but the condition is by using single for loop. How is this possible?
My code is this where I am confused here:
<?php
$a="*";
for($b=1;$b<=3;$b++)
{
echo "*".$a."<br/>";
}
echo "<br/>";
?>
You could use str_repeat() inside your loop like this
$a="*";
for($b=1;$b<=3;$b++)
{
echo str_repeat($a,$b).'<br>';
}
Result
*
**
***
To have this output * ** ***, you're may be looking for str_repeat
<?php
$a="*";
for($b=1;$b<=3;$b++)
{
echo str_repeat($a, $b)."<br/>";
}
echo "<br/>";
And the solution without any builtin function would be the following:
<?php
$a="*";
for($b=1;$b<=3;$b++)
{
echo $a."<br/>";
$a .='*';
}
echo "<br/>";

PHP Mysqli add subtract number format

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,',','.');

Stopping php returning the same result multiple times

I am trying to display a list of friends using PHP and SQL, and my code partially works. However, it is returning the same result on multiple occasions and I would like it not to.
The SQL:
$sql =
"SELECT ubuser.usr_firstname, ubuser.usr_lastname, ubuser.usr_DOB,
ubuser2_1.usr_firstname & \" \" & ubuser2_1.usr_lastname
AS UBFriend, ubFriendsLink.ub_lnkID1, ubFriendsLink.ub_lnkID2, ubuser.usr_ID,
ubuser2_1.usr_ID
FROM ubuser
AS ubuser2_1
INNER JOIN (ubFriendsLink INNER JOIN ubuser ON ubFriendsLink.ub_lnkID1 = ubuser.usr_ID)
ON ubuser2_1.usr_ID = ubFriendsLink.ub_lnkID2
WHERE (((ubFriendsLink.ub_lnkID1) = ".$_SESSION['usr_ID'] ."))
OR (((ubFriendsLink.ub_lnkID2) = ".$_SESSION['usr_ID'] ."))";
The SQL works (or seems to).
The code for displaying the result:
<?php
$nrecs=0;
while (!$FriendsRs->EOF) {
$nrecs++;
?>
<? php
if (.$SESSION['usr_ID'] == ['ub_lnkID1'])
{
echo <p>Name: <?php echo $FriendsRs->Fields['UBFriend']->Value ?><br/ >
<? php
else
echo <p>Name: <?php echo $FriendsRs->Fields['usr_firstname']->Value ?> <?php
echo $FriendsRs->Fields['usr_lastname']->Value ?><br />
<?php $FriendsRs->MoveNext() ?>
<?php } ?>
The result:
Name: Carl Smith
Name: Rob Sanderson
Name: Rob Sanderson
Name: Tony Jackson
The problem seems to be that what I am getting is a list of the names associated with the ub_lnkIDs, where i only want to display the individual names. (FYI, Rob Sanderson is not needed, but it can be returned once).
EDIT:
The desired output is
Name: Carl Smith
Name: Tony Jackson
From what you've described it sounds like you want to remove the duplicates from your list.
I'm not sure what you're trying to do with the session check but your original if/else condition didn't make sense so I changed it to what might be right based on your query.
<?php
$Arrnames = array();
// Produces array containing all the names in the correct format
while (!$FriendsRs->EOF)
{
if ($SESSION['usr_ID'] == $FriendsRs->Fields['ub_lnkID1'])
{
array_push($Arrnames, "<p>Name: ".$FriendsRs->Fields['UBFriend']->Value."<br />");
}
else
{
array_push($Arrnames, "<p>Name: ".$FriendsRs->Fields['usr_firstname']->Value ." ". $FriendsRs->Fields['usr_lastname']->Value . "<br />");
}
$FriendsRs->MoveNext();
}
// Removes the duplicates from the array
$ArrnamesDedupped = array_unique($Arrnames);
// Loops the de-duplicated array and echos the result
foreach ($ArrnamesDedupped as $value)
{
echo $value;
}
?>
if (.$SESSION['usr_ID'] == ['ub_lnkID1'])
My best guess is the single = is causing your if statement to fail
== means is the same as (and can return false) WHEREAS = will always return true! (= is used for setting values.)
Additionally you open <?php tags above the if statement - and then open them again in your <?php echo without closing them?
<? php
if (.$SESSION['usr_ID'] = ['ub_lnkID1'])
{
echo <p>Name: <?php echo //HERE
There are multiple problems.e.g. opening
php tags inside php tags, no closing } for the if clause, html paragraphs (p) opened and not closed etc.
what I think you want to do (not tested):
<?php
$nrecs = 0;
while (!$FriendsRs->EOF) {
$nrecs++;
if ($SESSION['usr_ID'] !='ub_lnkID1') {
echo "<p>Name:" . $FriendsRs->Fields['usr_firstname']->Value . " " . $FriendsRs->Fields['usr_lastname']->Value . "</p>";
}
$FriendsRs->MoveNext();
}
?>

Categories