I am trying to set boundary for my autoIncrement fuction and everything looks right but nothing is showing on my screen. I also tried to use var_dump() function but its not showing any result. Please, what am I missing?
I invoke the function and it is not showing any result. I also tried to dump the data using var_dump() function but its still the same error.
below is the snippet
<?php
function autoIncrement(){
$num = 0;
if($num > 10){
echo "Maximum number selected";
}else{
for ($num; $num <=10; $num++){
if($num > 10){
echo "Maximum number selected";
}else{
echo $num . ", ";
}
//var_dump($num);
}
}
autoIncrement();
}
?>
You made a mistake when you invoke your function "autoIncrement()". You dont invoke a function inside your function but outside of it.
see below code
<?php
function autoIncrement(){
$num = 0;
if($num > 10){
echo "Maximum number selected";
}else{
for ($num; $num <=10; $num++){
if($num > 10){
echo "Maximum number selected";
}else{
echo $num . ", ";
}
//var_dump($num);
}
}
}
autoIncrement();
?>
Related
Here is my Code
<?php
for ($x = 0; $x <= $n; $x++)
{
//Counting of rows
$countrow = get_rows($getvids);
if($offline == "Offline")
{
$apsys = "APSYS";
$offlined = "";
if ($getvids == $apsys || $getvids == $offlined )
{
echo "<tr>
<th>$x</th>
<th>$accntid[$x]</th>
<th>$fname[$x]</th>
<th>$lname[$x]</th>
<th>$vid[$x]</th>
<th>$vplatenum[$x]</th>
<th>$imei[$x]</th>
<th>$datas[1]</th>
<th>$datas[0]</th>
<th>$offline</th>";
echo "<th>$getvids</th>";
echo "<th><button type = 'button' class = 'btn btn-success viewbtn'> View Troubleshoot Report History</button></th>";
echo "</tr> ";
echo "total number of Rows:";
echo str_word_count($countrow);
}
else
{
}
}
else
{
}
}
?>
and here is my Function
<?php
function get_rows($getrowscount)
{
require ('db_connection.php');
$getrowscounts = $getrowscount;
//echo $getrowscounts;
return $getrowscounts;
}
//get_rows(); // call the function
?>
the table shows Two Rows with the value of = "APSYS"
I want to count the rows and show the output like this:
Total number of Rows: 2
but the output I can only get is like This:
Total number of Rows:1
Total number of Rows:1
Total number of Rows:0
can someone help me, I am a little bit confused about what will I use to count the rows?
Thanks.
How about this? Does this satifsfy your requirement?
<?php
$countrow = 0;
for ($x = 0; $x <= $n; $x++)
{
//Counting of rows
$countrow = $countrow + get_rows($getvids);
if($offline == "Offline")
{
//omitted
}
else
{
}
}
}
echo "total number of Rows:";
echo str_word_count($countrow);
?>
If you are trying to count them after fetching from the database, try to write a SQL query instead of your current approach.
i have this code .
echo "<br>";
$start = 1;
$angka = $_POST[angka];
$a = $angka;
for($i=$start; $i<=$angka; $i++) {
for($j=$start;$j<=$angka;$j=$j+2){
echo $i;
if($j < $angka) echo $a;
}
$a--;
echo '<br>';
}
Reference: Print Looping for dynamic row php
This is not my expecting result .
First i want the result like this .
-2-4-
1-3-5
-2-4-
1-3-5
-2-4-
The rule is Number of rows and columns follow the number of numbers declared.If the declaration of the number 5, then the results will be displayed are 5 rows and 5 columns like the example above.
i think this code is working
<?php
$_POST['angka'] = 5;
$angka = $_POST['angka'];
for($i=1; $i<=$angka; $i++) {
for($j=1;$j<=$angka;$j++){
if($i%2 == 1) {
if($j%2 == 0) {
echo $j;
} else {
echo '-';
}
} else {
if($j%2 == 0) {
echo '-';
} else {
echo $j;
}
}
}
echo '<br>';
}
Result:
-2-4-
1-3-5
-2-4-
1-3-5
-2-4-
Ok, so I need this number 3.55687428096E+14 to actually look like a proper whole number. I am not sure what to do in order to make it look like a whole number so that I can go about with my coding. Basically, without resolving this number 3.55687428096E+14, I cant proceed properly. Below is my code, please help!
<?php
$num = 17;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo "$num X ";
}
else{
echo "$num <b>equals</b> ";
}
$fact = $fact * $num;
}
echo $fact ."<br/>";
?>
By the way, the number 3.55687428096E+14 is the result I am getting after running the program. It comes ok till factorial 16 but once it turns to 17, things become 3.55687428096E+14!
You can user number_format() function if your number is goes very large.
number_format():
The number_format() function formats a number with grouped thousands.
Your code
<?php
$num = 17;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo "$num X ";
}
else{
echo "$num <b>equals</b> ";
}
$fact = $fact * $num;
}
echo number_format($fact) ."<br/>";
?>
Change the last line with :
echo number_format($fact,0) ."<br/>";
#Mark Baker: - my code below which was executed successfully thanks to your info and support:
<?php
echo "<h2>Find the sum of the digits in the number 100!</h2>"."<br/>";
$num = 100;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo " $num X ";
}
else{
echo " $num";
}
$fact = bcmul($fact,$num); //right over here!
}
echo " which equals" ."<br/>". "<h3>$fact</h3>";
//proceeding now to calculate the sum of the digits of the factorial!
$_array = str_split($fact);
$sum = array_sum($_array);
echo "The sum of the digits of the factorial is " ."<br/>";
echo "<h3>$sum</h3>";
?>
I have the following bit of code and for some reason in the first WHILE loop the first value of $actor_list (when $i = 0) does not display. If I simply echo $actor_list[0] then it displays fine, but in the loop it will not display. I merely get [td][/td] as the output. The remaining values of the array display fine.
Also the line
echo '</tr><tr> </br>';
is not displaying.
What am I missing here? The value of $num_actors is an even number in my test scenario so there doesn't seem to be a reason for the above echo line to be skipped.
$actor_list = explode(" ", $actors);
$num_actors = count($actor_list);
if ($num_actors <= 6) {
foreach ($actor_list as $actor) {
echo '[td]'.$actor.'[/td] </br>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
echo '</tr><tr> </br>';
while ($i <= $num_actors) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
}
}
You're not initialising the variable $i to 0, which means it will be set to 'null' and thus not reference the 0th index of the array; but when it's then incremented its value will become 1.
Note: [..] Decrementing NULL values has no effect too, but incrementing them results in 1.
See: http://php.net/manual/en/language.operators.increment.php
Try adding:
$i = 0;
before the if statement.
Made a few changes.
This is working for me.
Try it out.
<?php
$actor_list = 'act1 act2 act3 act4 act5 act6';
$actors = explode(" ", $actor_list);
$num_actors = count($actor_list);
//debug
//echo "<pre>";
//print_r($actor_list);
//echo "</pre>";
echo "<table>";
if ($num_actors <= 6) {
foreach ($actors as $actor) {
echo '<tr><td>'.$actor.'</td></tr>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
while ($i <= $num_actors) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
}
}
echo "</table>";
?>
can any body help me how I can delete the white space at the end of the output.
This might be easy question but to be honest it took me more than 45 minutes and still nothing. Assignment
Write a PHP script that prints the numbers from the number inputted on form to zero. The numbers should be separated with a single space but the last number, zero, should not have a space after it. If the user inputs a number smaller than zero, print “The number should be at least zero!” The used form looks like this:
Luku:
Example output
5 4 3 2 1 0
my code:
<?php
$number=$_GET["number"];
if ($number < 0){
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
echo $i." ";
}
}
?>
You can use trim()
<?php
$number=$_GET["number"];
if ($number < 0){
echo "The number should be at least zero!";
} else {
$num = '';
for($i=$number; $i>=0; $i=$i-1)
{
$num .= $i." ";
}
echo trim($num);
}
?>
Try this:
echo implode(" ",range($number,0));
Magic and trickery ;)
You can use rtrim instead of trim to delete white spaces at the end of the string:
<?php
$number=$_GET["number"];
if ($number < 0){
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
$num .= $i." ";
}
}
$num = rtrim($num);
?>
You could use an if-statement:
<?php
$number=$_GET["number"];
if ($number < 0) {
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
echo $i . ($i != 0 ? " " : "");
}
}
Or you use trim:
<?php
$number=$_GET["number"];
$output = "";
if ($number < 0) {
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
$output .= $i . " ";
}
}
echo trim($output);
try this:
<?php
$number=$_GET["number"];
if ($number < 0) {
echo "The number should be at least zero!";
} else {
$string = $number;
for($i=$number-1; $i>=0; $i=$i-1)
{
$string .= " $i";
}
}
echo $string;
?>