Results: Php code is doing an infinate loop 

Voters
0 You may not vote on this poll

  • php code
    0%
  • php problem
    0%
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Can someone help me find what is wrong with this code


    PHP Code:
    <?php 
            $count 
    mysql_query("SELECT COUNT(xx_ID) FROM table");
            
    $max ceil(mysql_result($count0));

            
    $x 1;

                            
            while (
    $x <= 8){
                
    $repeated false;
                
    $p 1;
                
    $random[$x] = rand(8001,8000 $max);
                    while (
    $p <= $x){
                        if (
    $random[$p]==$random[$x])
                            
    $repeated true;
                            
    $p++;
                    }
                if (!
    $repeated){        
                
    $query mysql_query("SELECT * FROM table WHERE xx_ID='$random[$x]'");
                
    $query_row mysql_fetch_assoc($query);
                if (
    $x == || $x == 5)
                echo 
    "<div>";

                    echo 
    "<div>";
                    echo 
    "<img alt='' src='ssl/Product_Images/".$query_row['pictureName'].".jpg'/>";
                    echo 
    "<a href='Products.php'><p>Item: ".$query_row['item']."<br/>Condition: Refurbished<br/>".$query_row['model']."</p></a>";
                    echo 
    "<p>$".$query_row['price']."</p></div>";
                    
                if (
    $x == || $x == 8)
                echo 
    "</div>";
                
    $x++;
                }
                
            }
                    
            
    ?>
    I keep getting like an infinite loop or something. The page never loads.
    Last edited by ManiacDan; October 4th, 2012 at 11:28 PM.
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    If $repeated is false then $x never increments. I think.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Php


    Originally Posted by requinix
    If $repeated is false then $x never increments. I think.
    The problem is with the $p = 1; at the beginning of the loop. I don't know why is messing everything up. I cant find the error i am happening you guys could spot it. If i place the $p = 1; outside the loop everything works but i get repeated items which is what i want to stop from happening.

    <?php
    $count = mysql_query("SELECT COUNT(xx_ID) FROM table");
    $max = ceil(mysql_result($count, 0));

    $x = 1;


    while ($x <= 8){
    $repeated = false;
    $p = 1;
    $random[$x] = rand(8001,8000 + $max);
    while ($p <= $x){
    if ($random[$p]==$random[$x])
    $repeated = true;
    $p++;
    }
    if (!$repeated){
    $query = mysql_query("SELECT * FROM table WHERE xx_ID='$random[$x]'");
    $query_row = mysql_fetch_assoc($query);
    if ($x == 1 || $x == 5)
    echo "<div>";

    echo "<div>";
    echo "<img alt='' src='ssl/Product_Images/".$query_row['pictureName'].".jpg'/>";
    echo "<a href='Products.php'><p>Item: ".$query_row['item']."<br/>Condition: Refurbished<br/>".$query_row['model']."</p></a>";
    echo "<p>$".$query_row['price']."</p></div>";

    if ($x == 4 || $x == 8)
    echo "</div>";
    $x++;
    }

    }

    ?>
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    This code is a bit...hard to follow. Especially since you rely on not putting brackets around your IF statemments.

    Like requinix said, if no repeat is found, $x isn't incremented, and that causes infinite loops.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Thank you for the help guys, i found out what the problem was.


    if ($random[$p]==$random[$x])

    This statement makes it be true always because random[1] is equal to random[1] which makes it a endless loop because x will never increase + 1. I fix it by adding this.


    if ($random[$p]==$random[$x] && $p !== $x)
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Is this entire code just trying to get 8 random rows from a table?

    SELECT * FROM theTable ORDER BY RAND() LIMIT 8;

    "ORDER BY RAND()" is generally considered inefficient and 'wrong,' but it's better than this.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo