plznty Posted October 29, 2010 Share Posted October 29, 2010 I'm trying to make it so that if the $row data is more than just "" (empty) then echo if not then run the scan on the next number. It just keeps repeating the values all the way through? Help? Thanks. function scan($random) { if ($random == 1) { $query = "SELECT * FROM users WHERE clicks > 20 AND status='1' ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); if (isset($row['id'])) { echo '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>'; } else { scan(2); } } if ($random == 2) { $query = "SELECT * FROM users WHERE clicks > 10 AND status='1' ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); if (isset($row['id'])) { echo '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>'; } else { scan(3); } } if ($random == 3 OR 4 OR 5) { $query = "SELECT * FROM users WHERE clicks > 0 AND status='1' ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>'; } } $random = rand(1, 5); scan($random); Quote Link to comment https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/ Share on other sites More sharing options...
Adam Posted October 29, 2010 Share Posted October 29, 2010 I'm not sure what this function is meant to do, but personally I'd have it return the HTML if it's successful, or increment the value of $random and move to the next IF condition if not: function scan($random) { if ($random == 1) { $query = "SELECT * FROM users WHERE clicks > 20 AND status='1' ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); if (isset($row['id'])) { return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>'; } $random++; } if ($random == 2) { $query = "SELECT * FROM users WHERE clicks > 10 AND status='1' ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); if (isset($row['id'])) { return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>'; } $random++; } if (in_array($random, array(3,4,5)) { $query = "SELECT * FROM users WHERE clicks > 0 AND status='1' ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>'; } } Then to call it: echo scan($random); Also you can't have an expression like: $random == 3 OR 4 OR 5 .. The best alternative here is to use in_array like I have done in the code above. Although I'm sure this function could be improved a lot more if I understood what it was meant to do. Quote Link to comment https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/#findComment-1128054 Share on other sites More sharing options...
plznty Posted October 29, 2010 Author Share Posted October 29, 2010 I'm pretty sure there is just something wrong with using isset to find out if the value is blank. It only repeats multiple times if $random is 1 or 2 since it doesnt seem to shut off after 1 or 2 has been executed and just carrys on Quote Link to comment https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/#findComment-1128057 Share on other sites More sharing options...
Adam Posted October 29, 2010 Share Posted October 29, 2010 empty Quote Link to comment https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/#findComment-1128063 Share on other sites More sharing options...
plznty Posted October 29, 2010 Author Share Posted October 29, 2010 can you put it in. I just did and its the same outcome. I've tried about 10 possible solutions to this now and they all had the same outcome. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/#findComment-1128069 Share on other sites More sharing options...
Adam Posted October 29, 2010 Share Posted October 29, 2010 Did you use !empty() or empty()? Problem being with your code is that the problem could lie in a number of places. Have you debugged each query result to ensure they're what you expect? e.g.. if ($random == 1) { $query = "SELECT * FROM users WHERE clicks > 20 AND status='1' ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); // DEBUGGING var_dump($row); // -------- if (isset($row['id'])) { return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>'; } $random++; } Have you added echo statements within each IF block to flag which are actually being evaluated as true? function scan($random) { if ($random == 1) { echo 'random = 1'; // DEBUG $query = "SELECT * FROM users WHERE clicks > 20 AND status='1' ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); if (isset($row['id'])) { return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>'; } $random++; } if ($random == 2) { echo 'random = 2'; // DEBUG $query = "SELECT * FROM users WHERE clicks > 10 AND status='1' ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); if (isset($row['id'])) { return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>'; } $random++; } if (in_array($random, array(3,4,5)) { echo 'random = 3 or 4 or 5'; // DEBUG $query = "SELECT * FROM users WHERE clicks > 0 AND status='1' ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>'; } } Debugging is really an essential skill you'll need to learn, but try those for now and see if they highlight the problem. Quote Link to comment https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/#findComment-1128074 Share on other sites More sharing options...
plznty Posted October 29, 2010 Author Share Posted October 29, 2010 Its outputting the values apart from random = 1random = 2 when $random = 1; It needs to be able to skip $random = 1 if there is no result given. What a headache, ty for debugging etc. Must be to do with what values its getting on the $row[blah] Quote Link to comment https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/#findComment-1128087 Share on other sites More sharing options...
plznty Posted October 29, 2010 Author Share Posted October 29, 2010 got it to work perfectly based on the code you provided. Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/#findComment-1128090 Share on other sites More sharing options...
plznty Posted October 29, 2010 Author Share Posted October 29, 2010 basically if it does get a value from either the $random == 1 or the $random == 2 it will just add another 1 or two on $random == 1 true then $random == 2 true then $random == 3 When I want it to be if $random == 1 do that and then stop if the $random == 1 and then a row is unset then go to $random == 2. Hope you can try help me again. Quote Link to comment https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/#findComment-1128094 Share on other sites More sharing options...
plznty Posted October 29, 2010 Author Share Posted October 29, 2010 fixed. Quote Link to comment https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/#findComment-1128110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.