How i can remove point from number using loop function [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have this codes. My question is How i can remove .1 one from rest of numbers in my output.
Its gives : The counter is 1464.1
I want it to give : The counter is 1464
<?php
$i = 1000;
$t = 10;
while ($i < 100000000) {
echo"<p>The counter is $i";
$i = $i + ($i / $t);
}
?>
And output is :
The counter is 1000
The counter is 1100
The counter is 1210
The counter is 1331
The counter is 1464.1
The counter is 1610.51

Use intval:
echo '<p>The counter is '.intval($i) .'</p>';
EDIT:
Display purposes only use the above code.
Data manipulation use:
$i = intval ($i + ($i / $t));

Use intval():
$i = intval( $i + ($i / $t) );

You can type cast it when you echo it.
echo "<p>The counter is " . (int)$i . "</p>";

You can use:
echo "<p>The counter is ".floor($i)."</p>";
or
echo "<p>The counter is ".intval($i)."</p>";
These will effectively 'cut off' the decimal (or give you the integer value)
or you can round to the nearest integer:
echo "<p>The counter is " . round($i) . "</p>";
OR if you are trying to subtract .1 from your answer (it is unclear in the OP without more examples) you can do this:
echo "<p>The counter is ".($i - .1)."</p>";

Related

Print a new <tr> each time & "N" number of elements inside it, for a total quantity of "Q" [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
i have a variable Q,
i want to print "cat" (or whatever) Q times in HTML table rows, only each row of the HTML table can contain maximum of 5 cats.
like if Q <= 5 then i want to print a single <tr>.
& if Q = 10;
then i want to print 2 rows each containing 5 cats.
can someone help me here?
I'm using php as the programming language and HTML.
thanks in advance.
You can also make use of array_chunk to decide the chunk(no. of times) for each row and repeat a string using str_repeat the chunk size times to achieve your goal.
<?php
$times = 12;
$limit_for_each_row = 5;
$string = "Cat";
echo "<table>";
foreach(array_chunk(range(1,$times),$limit_for_each_row) as $chunk){
echo "<tr>" . str_repeat("<td>$string</td>",count($chunk)) . "</tr>";
}
echo "</table>";
Demo: https://3v4l.org/EWgHh
May following code will help you to achieve requested output:
<?php
$print_label = "meow";
$q = 15;
$steps = 5;
echo "<table>";
for($i=1;$i<=ceil($q/$steps);$i++) :
echo "<tr>";
for($j=1;$j<=$steps;$j++) :
echo "<td>".$print_label."</td>";
endfor;
echo "</tr>";
endfor;
echo "</table>";
One way to solve this problem is by using array_fill to make an array of $q copies of the word; array_chunk to split that into the maximum number of words per row; and implode to put all the words in each chunk together into a row:
$word = "cat";
$q = 13;
$row_max = 5;
$words = array_fill(0, $q, $word);
$chunks = array_chunk($words, $row_max);
foreach ($chunks as $chunk) {
echo '<tr><td>' . implode('</td><td>', $chunk) . '</td></tr>' . "\n";
}
Output:
<tr><td>cat</td><td>cat</td><td>cat</td><td>cat</td><td>cat</td></tr>
<tr><td>cat</td><td>cat</td><td>cat</td><td>cat</td><td>cat</td></tr>
<tr><td>cat</td><td>cat</td><td>cat</td></tr>
Demo on 3v4l.org

is it possible in php loop without nested loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to print like this using loop without nested loop:
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
first line start from 1 and print only one integer
second line start from 2 and print two integer
third line start from 4 and print four integer
fourth line start from 8 and print eight integer
(same condition in other line if exist)
A pair of counters should do the trick;
$threshold = 1;
$x = 1;
for($i = 1; $i <= 15; $i++) {
echo $i . ' ';
if($x == $threshold) {
echo "<br>\n";
$threshold = $threshold * 2;
$x = 0;
}
$x++;
}
Will keep on working till infinity, or your script runs out of memory. Whatever comes first :)
You can achieve with single while and some array functions. Try this.
<?php
$start=1;
while($start<=10){
$array = range($start,($start+$start-1));
echo implode(' ',$array)."<br>";
$start=$start*2;
}
?>
something like this?
$i=1;
$t=1;
while($i<1000)
{
if($i==$t)
{
if($t!=1) echo "<br />";
$t=$t*2;
}
echo $i." ";
$i++;
}
same thing but with log($i,2).
<?php
$i=1;
while($i<20)
{
echo $i;
$i++;
if(filter_var(log($i,2), FILTER_VALIDATE_INT))
echo PHP_EOL;
else
echo "\t";
}
You can even do it without any for and some recursion ;-)
<?php
$elements = range(1,15);
display($elements);
function display(array $parts, $step = 1) {
if(! count($parts)) {
return;
}
$elements = array_splice($parts, 0, $step);
echo implode("\t", $elements)."\n";
display($parts, $step * 2);
}
See the working code on https://eval.in/755277

How to display total number from start to a range in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to display total number from start to end in a range in php .Each time number will increase by 10 .
For example total number is 35
Then number will display this way :
1 to 10
11 to 20
21 to 30
31 to 35
$num=35;
for($i=1;$i<$num;$i=$i+10)
{
$j=$i+9;
if($j>$num)
$j=$num;
echo $i.' to '. $j;
}
try
$num=35;
for($i=1;$i<=$num;$i++) {
if($i %10 == 0)
echo $i.'<br>';
else
echo $i;
}
will output :-
12345678910
11121314151617181920
21222324252627282930
3132333435
Use a for loop like this
for($x = 1; $x <= 35, $x += 10){
echo $x . " to " . $x+9;
}
This will work i hope.
Thanks

How to sum a set of values from a form in PHP? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a simple form and a submit button. After pressing the submit button, get the values (they will be numbers) and calculate the sum. And again calculate the sum of individual digits stored in sum.
A better explanation: 1+2+3+4=10 where 1,2,3 and 4 are user inputs from the form. And the sum 10 has to be split-ted and summed again as 1+0=1. And this would be my final result. So far I did the task where it gives me the first sum. I don't know what to do to display the second result which I want to be the finale.
<?php
$a='';
$sum='';
$total='';
if (!isset($_POST['submit'])) {
echo " ";
}
else {
$a = array($_POST['textfield'],$_POST['textfield1'],$_POST['textfield2'],$_POST['textfield3'],$_POST['textfield4'],$_POST['textfield5'],$_POST['textfield6'],$_POST['textfield7'],$_POST['textfield8'],$_POST['textfield9'],$_POST['textfield10'],$_POST['textfield11'],);
for ($i=0; $i<=12; $i++) {
$sum= array_sum($a);
$total= ;
}
}
echo "sbora e: $total ";
?>
$total = array_sum(str_split($sum));
Using Artefacto's method.
On another note,
$a = array($_POST['textfield'],$_POST['textfield1'],$_POST['textfield2'],$_POST['textfield3'],$_POST['textfield4'],$_POST['textfield5'],$_POST['textfield6'],$_POST['textfield7'],$_POST['textfield8'],$_POST['textfield9'],$_POST['textfield10'],$_POST['textfield11'],);
can be written as,
$a = array();
for ($i = 0; $i <= 11; $i++){
if (isset($_POST["textfield_$i"]))
array_push($a, $_POST["textfield_$i"]);
}
if the names of the fields are:
textfield_0, textfield_1, textfield_2...
So you want to build the cross sum of your first result:
$result2 = 0;
//cast integer to string
$strTotal = (string) $total;
//loop over "chars" and add them to your second result
for($i=0; $i < strlen($strTotal); $i++)
{
$result2 += $strTotal{$i};
}

How to increment a for loop by more than 1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I print a series like this?
6,15,24,33,42.....1000
I have tried using a for loop like this, but can't work out how to get it to increment by 9 on each iteration.
for($i = 6; $i <= 1000; $i = 9)
{
echo $i . ', ';
}
Quite simple really:-
for($i = 6; $i <= 1000; $i += 9){
echo $i . ', ';
}
//If you have to finish the series with 1000, although it is not a part of the series:-
echo '1000';
See it working
just for fun:
echo implode(', ', range(6, 1000, 9));
echo ", 1000";
<?php
for($i=6; $i<1000; $i+=9)
echo $i."<br>";
?>
Seeing the question this can be an answer.

Categories