PHP Infinite While Loop, Unknown Cause - php

<?php
$i=1;
while ($i<=$totalpages) {
if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i==1 || ($i+1>$totalpages)) {
echo "<td><a href=\"search.php?page=$i\">";
if ($i=$page) {
echo "<strong> $i </strong>";
}
if ($i!=$page) {
echo " $i ";
}
echo "</a></td>";
}
$i++;
}
?>
Trying to build a webpage that ouputs some search values neat the bottom of the page, however I keep getting an infinite loop. I've no Idea about what's causing it, and would like someone elses insight into the problem at hand.

Your if condition has a problem
Change
$i =1
with
$i==1

You have a number of places that you reset $i, inadvertently I assume
$i=1
and
$i=$page
replace them with ==

<?php
$i=1;
while ($i<=$totalpages) {
if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i == 1|| ($i+1>$totalpages)) {
echo "<td><a href=\"search.php?page=$i\">";
if ($i == $page) {
echo "<strong> $i </strong>";
}
if ($i!=$page) {
echo " $i ";
}
echo "</a></td>";
}
$i++;
}
?>
Changes in 1st and second if condition.
1) $i == 1
2) $i == $page

is there a reason why this can not be accomplished using a for loop ? While loops are always risky and easy to cause problems if there is a scenario to cause to loop for ever.
eg.
for ($i = 0; $i<=$totalpages; $i++){
// do something
}

There is some issues in the conditions
first: the
|| $i=1||
allways return true.
Change for
|| $i==1||
Second: The same case of adobe
if ($i=$page) //Allways return try is assignation operation
Change to:
if ($i==$page)

Related

Star rating with half star support

I am running into a problem rendering a star rating and was hoping I can get some extra eyeballs on my problem. I have the normal rating working just fine with whole numbers but I am struggling to display the half star. For example I created a service that provides me with a rating 0-5 so I get a value like 2.5, 3 or 5 etc...
Before I go and create a switch case and create an svg for each variation I was hoping to get a little a pointer. Below is what I have currently, any tips would be greatly appreciated.
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
}
else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
Ideally I would like to add a condition at the end of the loop and check for the half and echo "";
There is probably an easier way to do it but this works, checks if $starRating is a float and then rounds it up and checks against $i to place the half star in the correct position.
<?php
$totalRating = 5;
$starRating = 2.5;
for ($i = 1; $i <= $totalRating; $i++) {
if($starRating < $i ) {
if(is_float($starRating) && (round($starRating) == $i)){
echo "<img src=\"/icons/star-half.svg\">";
}else{
echo "<img src=\"/icons/star-empty.svg\">";
}
}else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
You can verify if the value of $starRating is an integer, doing something like this (considering only half values):
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if ($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
} elseif(is_int($starRating) === false) {
echo "<img src=\"/icons/star_half.svg\">";
} else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
If you want to show stars with a more precise value you can create images with the float values in the name, like "star-3.svg" (representing a .3 float value, and do something like this:
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if ($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
} elseif(is_int($starRating) === false) {
echo "<img src=\"/icons/star-" . floatval($starRating)) . ".svg\">";
} else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
But in this case you need to take care to only receive float values with one number (2.2, 3.4, etc.).
I hope it helps...

How to execute php if condition once time only

How to execute php if condition once time only and didn't check it again, i put if condition in for loop and i want to check it one time only:
<?php
for($x=0;$x<3;$x++){
if($x == $x){
echo "";
}
else{
echo "hidden";
}
}
I need to execute first if one time only in for loop
I need to execute first if one time only in for loop. Well that's not how you do it, you need to compare with a variable and upon success just use break to come out of the loop. Something like:
<?php
$y = 0;
for($x=0; $x<3; $x++){
if($x === $y){
echo "X is Equal to Y";
break;
}
else {echo "hidden";}
}
echo "<br>". "You're outside the loop!";
?>
Set a flag, if flag is true don't execute if condition.
<?php
$flag = 0;
for($x=0;$x<3;$x++) {
if($x == $x && $flag == 0){
echo "";
$flag = 1;
}
else{
echo "hidden";
}
}

php First value of array not displaying

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

table printing one more cell instead of new line

I am trying to limit the printing of table cells to 3 per row. This worked in one example, but clearly not working when I tried to use the same code somewhere else in the site. This is the code:
$n=3;
echo "<table cellpadding='10' cellspacing='10' style='margin-right:-70px;'><tr>";
$users_count = count($users);
for($i=0; $i<$users_count;$i++)
{
$temp = array();
$temp = $users[$i];
echo "<td>";
echo "<div id='kitchen_box'>";
echo "<div id='kitchen_box_details'>";
echo "<h4>".$temp->fullname . "</h4><br>";
if(strcmp($temp->address, '') == 0)
echo $temp->city;
else
echo $temp->address.", ".$temp->city;
echo "</div>";
echo "<div id='kitchen_box_pic'><img id='kitchen_image' src='".$temp->profilepic."' /></div>";
echo "</div>";
echo "</td>";
if($i != 0){
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
}
echo "</table>";
I can't see why this wouldn't work! I would really appreciate support on the matter :)
There are several issues with your code. But your main issue is that you're using a zero-based increment, but doing a 1-based check. So, a table of your the results of an $i!=0 && $i%$n==0 goes like this:
$i $result
0 false
1 false
2 false
3 true
So, you see, the result closes the row after the fourth, not the third cell. To fix this, change the line to:
if($i % $n == $n-1 && $i != $users_count-1){
You should also include a closing </tr> tag with your closing </table> tag.
Incidentally, you shouldn't give the same ID to multiple elements on a page. Each of your kitchen_box and kitchen_box_div DIV tags will have the same ID. If you want this for CSS, use classes. Otherwise, you might try adding the value of $i to each ID.
Nitpicking on request:
The line $temp = array(); seems a little pointless, especially since you don't want $temp to be an array, but an object.
The else{ echo ""; } lines are also redundant.
You don't need the if($i != 0) check now because that case will not pass the next test anymore.
Otherwise the code seems fine to me.
I think the problem is that your $i starts at zero.
Let's look at your conditions before creating a new row :
if($i != 0){
if($i % $n == 0 && $i != $users_count-1)
If $i = 0, the first condition isn't matched. Then the second isn't for $i = 1 and $i = 2. And then your script creates another ... in your first row before matching your if conditions for the first time.
I guess you could move this part of the code right after the beginning of the for instructions :
for($i=0; $i<$users_count;$i++)
{
if($i != 0)
{
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
// Echo your <td> ... </td>
}
echo "</tr></table>";

error in php program

I am just a beginner in PHP. I have tried to write a program for prime numbers, but the result is not correct. I couldn't find the error. How can I correct this? Here is my code:
<?php
$n=15;
for($i=2; $i<=$n; $i++)
{
echo "<br />";
for($j=2; $j<=$i-1; $j++)
{
$k=$i%$j;
if($k==0)
{
break;
}
else echo $i."is prime";
break;
}
}
?>
Try this:
<?php
$n=15;
for($i=2; $i<=$n; $i++)
{
$k = 1; //assume that it is prime
for($j=2; $j<$i; $j++) //if $i is 2, then it won't enter the loop as it will not match the condition ($j<$i)
{
$k=$i%$j;
if($k==0)
break; //if not prime, $k will be set as 0. So, break.
}
if($k!=0) // if $k <> 0, then it is prime
echo "<br />" . $i." is prime";
}
?>
Edit
Updated the code to take care of the "2"
Try this:(whitout using loop)
function is_prime($p) {
return ($p > 1) && (($p%2 >= 1) && ($p%3 >= 1) && ($p%5 >= 1)) || in_array($p, [2,3,5]);
}
echo is_prime(15);
You are breaking the loop the first time it's run by calling this basically:
if (something) {
break;
} else {
break;
}
it will break no matter what. you need to take out the last break.
Well, your code is kind of confusing to understand; at first glance it seems as if it's trying to determine primality by checking every divisor up to N, but you've got that outer-loop going on which I didn't see at first... Oh boy.
If you're just trying to figure out if some number N is prime, the following should work:
$n = 15
$prime = true;
for ($i = 2; $i < sqrt($n); $i++) {
if ($n % $i == 0) {
$prime = false;
break;
}
}
echo $n . " is " . ($prime ? "" : "not") . " prime.";
<?php
echo "TEST\r\n";
$n=15;
for($i=2; $i<=$n; $i++)
{
echo "I= $i \r\n";
for($j=2; $j<=$i-1; $j++)
{
$k = $i%$j;
if($k==0)
{
break;
} else {
echo $i."is prime \r\n";
}
break;
}
}
I think your secod for loop is incorrect.
for($i=2; $i<=$n; $i++) {
echo "<br />";
for($j=2; $j<=$i-1; $j++) {
...
The first value for $j is 2. And for the first time, the first value for $i is 2 again. Now, look at your second for loop code.
It will be like this at the first time:
for($j=2; $j<=2-1; $j++) ... // for($j=2; $j<=1; $j++)
And this condition is not valid at all: 2<=1

Categories