Smudly Posted June 10, 2010 Share Posted June 10, 2010 Working on a Traffic Exchange, and having issues when I'm trying to create my script to randomly choose a website from a list of sites in the database, and show one in the bottom frame. Once the 10 second timer in the top frame is up, the user can click Next, and it should show a new random website inside the second frame. If you need an idea of what I'm talking about, go to: www.daobux.com Login: phphelper Pass: 123456 Click on Surf Right now, I have about 10 websites inside my database that should be active. However, there are only 4 different websites being shown. Here is all the code I have. // surf.php // <?php session_start(); include('inc/connect.php'); // Select a Random Website $sitequery = "SELECT users.id, users.credits, users.username, websites.id, websites.url, websites.userid, websites.active FROM users JOIN websites ON websites.userid = users.id where websites.active='y' and users.credits > 0 and websites.userid != ".$_SESSION['userid']." order by rand() LIMIT 1"; $siteresult = mysql_query($sitequery) or die("Error in query: $sitequery. " . mysql_error()); if (mysql_num_rows($siteresult) != 0){ $siterow = mysql_fetch_array($siteresult); $url = $siterow["url"]; } else{ echo 'error'; } ?> <html> <head> <title>My Traffic Exchange</title> <script> top.surfbar.location = 'surfbar.php' top.Site.location = $url; </script> </head> <!-- 68 - 80 pixels high is a good size for surfbar. No more than 100!! --> <frameset rows="80,*" BORDERCOLOR="#222222" BORDER="3"> <frame src="surfbar.php" name="surfbar" marginwidth="O" marginheight="0" NORESIZE SCROLLING="no" /> <frame src="<?php echo $url ?>" name="Site" marginwidth="O" marginheight="0" noresize scrolling="auto" /> </frameset> <noframes> <body><p>Sorry, but your Browser does not support Frames. Please open this page in a new browser.</p></body> </noframes> </html> // surfbar.php // <?php session_start(); ?> <html> <head> <title>My Traffic Exchange</title> <link rel="stylesheet" type="text/css" href="styles/surfbar.css" /> <script type="text/javascript"> var time = 10; function startCountdown(){ var t = setTimeout("countdown()", 1000); } function countdown(){ --time; if(time == 0){ document.getElementById("countdown").innerHTML = "<a href='surf.php'>Next</a>"; }else{ document.getElementById("countdown").innerHTML = time; var t = setTimeout("countdown()", 1000); } } </script> </head> <body onload="startCountdown();" class="surfbar" bgcolor="#333333"> <!-- Surfing Container --> <div id="container"> <!-- Logo --> <div id="logo" align="center"> <img src="img/ad.jpg" alt="ptc" width="150" height="48"><br /> <a href="member.php">Members Area</a> </div> <!-- Exp --> <div id="exp" align="center"> <p><strong>Experience:</strong><p> </div> <!-- Timer --> <div id="timer" align="center"> <div id="countdown">10</div> <p><strong> Total Surfed: 78<br /> Credits Earned: 78<br /> </strong></p> </div> <!-- Bonus --> <div id="bonus" align="center"> <p><strong>Surf 25 More for <br>a 25 Credit Bonus!</strong></p> </div> <!-- Banner --> <div id="banner" align="center"> <img src="img/ptc.jpg" alt="ptc"> </div> </div> </body> </html> Inside my Database, in the table websites: id int(11) No auto_increment userid int(11) No url varchar(2083) utf8_general_ci No credits int(11) No 0 stats int(11) No 0 active tinytext utf8_general_ci No status text utf8_general_ci No title varchar(50) utf8_general_ci No And my rows inside my websites database: id userid url credits stats active status title 31 1 http://phpacademy.com 1823 0 y php 32 1 http://worldofwarcraft.com 1234567890 0 y World 29 2 http://dixie.edu 82749 0 y Dixie 24 2 http://nbc.com 6354 0 y nbc 23 2 http://abc.com 2 0 y My 25 2 http://youtube.com 1827942 0 y youtube 26 2 http://javaguy.com 0 0 y java 27 2 http://daobux.com 98798042 0 y sdfs 28 2 http://thottbot.com 1234 0 y youtube 33 1 http://bored.com 1823425123 0 y I 34 1 http://facebook.com 123842 0 y Facebook 35 2 http://jokes.com 244 0 y Jokes 36 2 http://godaddy.com 4444 0 y Great 37 0 0 0 y 38 0 http://abc.com 0 0 y 39 2 http://hbo.com 400 0 y HBO 41 2 http://devnetwork.net 456789 0 y dev 45 2 http://yahoo.com 1234 0 y yahoo Thanks for your time. I appreciate the help Quote Link to comment https://forums.phpfreaks.com/topic/204434-creating-script-that-selects-a-random-website-to-show-in-frame/ Share on other sites More sharing options...
lemmin Posted June 10, 2010 Share Posted June 10, 2010 If you are only trying to get one site from each query you should probably add "Limit 1." But before you do that, try echoing out the mysql_num_row to see how many rows it is returning. Since you are not limiting the result, it should return all the possible sites based on the criteria you are giving it. If it only returns four, then your query is either wrong, or the data in your database isn't what you expect. If it returns all 10 that you want, the problem must be in the php. Quote Link to comment https://forums.phpfreaks.com/topic/204434-creating-script-that-selects-a-random-website-to-show-in-frame/#findComment-1070553 Share on other sites More sharing options...
Smudly Posted June 10, 2010 Author Share Posted June 10, 2010 Actually, LIMIT 1 is there hehe. I tried echoing out this: echo mysql_num_rows($siteresult); and it returns 1 So that is working fine. What else could be the problem? Quote Link to comment https://forums.phpfreaks.com/topic/204434-creating-script-that-selects-a-random-website-to-show-in-frame/#findComment-1070562 Share on other sites More sharing options...
jcbones Posted June 11, 2010 Share Posted June 11, 2010 Let me get this straight. It is working, but the rows are limited to 1 of 4, instead of correctly being 1 of 10? Did you run the query through MySQL sans the LIMIT 1 and see what was returning? Quote Link to comment https://forums.phpfreaks.com/topic/204434-creating-script-that-selects-a-random-website-to-show-in-frame/#findComment-1070627 Share on other sites More sharing options...
Smudly Posted June 11, 2010 Author Share Posted June 11, 2010 Actually, after a bit more detective work, I realized that the websites being shown are the sites with the same id # as the user's id #. Should be an easy fix, but still can't figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/204434-creating-script-that-selects-a-random-website-to-show-in-frame/#findComment-1070629 Share on other sites More sharing options...
jcbones Posted June 11, 2010 Share Posted June 11, 2010 JOIN websites ON websites.userid = users.id You are joining the tables by selecting the website's userid (equal to) the users' userid. Now I'm not sure what the userid column in the website table is suppose to be used for. But, using it this way is limiting each website to only be used by one individual user. Quote Link to comment https://forums.phpfreaks.com/topic/204434-creating-script-that-selects-a-random-website-to-show-in-frame/#findComment-1070645 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.