PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development
View Poll Results: Php code is doing an infinate loop
php code 0 0%
php problem 0 0%
Voters: 0. You may not vote on this poll


Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 4th, 2012, 09:07 PM
willman01 willman01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 willman01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 39 m 38 sec
Reputation 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 10:28 PM.

Reply With Quote
  #2  
Old October 4th, 2012, 10:11 PM
requinix's Avatar
requinix requinix is online now
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,697 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 50 m
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
If $repeated is false then $x never increments. I think.

Reply With Quote
  #3  
Old October 4th, 2012, 10:22 PM
willman01 willman01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 willman01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 39 m 38 sec
Reputation Power: 0
Php

Quote:
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++;
}

}

?>

Reply With Quote
  #4  
Old October 4th, 2012, 10:31 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,804 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 17 h 28 m 32 sec
Reputation Power: 6112
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.

Reply With Quote
  #5  
Old October 4th, 2012, 10:47 PM
willman01 willman01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 willman01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 39 m 38 sec
Reputation 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)

Reply With Quote
  #6  
Old October 4th, 2012, 11:08 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,804 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 17 h 28 m 32 sec
Reputation Power: 6112
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Can someone help me find what is wrong with this code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap