ash992 Posted July 24, 2012 Share Posted July 24, 2012 Hey guys, So I posted earlier and someone managed to help me with my problem, but my problem has slightly changed.. See basically I'm trying to create something that takes all values from a column called "UID", but only were the "Type" Column is set to the value "AD", and then choose one of them at random and echo an image with that number. Here's how far I got <?php include ("connect.php"); $query = "SELECT MIN(UID), MAX(UID) FROM Users WHERE Type='AD'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo '<img src="' . rand($row['MIN(UID)'], $row ['MAX(UID)']) . '.png>" <br />'; } ?> Now that fully works however the problem is that despite getting the highest and lowest values, this means it will also getting all value in between, meaning some of them will not have the type AD, so I need to somehow figure out a way to just select the ones with the type AD and then pick one of them at random, Now I've done a lot of googling but I really can't figure out how I'm going to do this... Any help at all would be much appreciated! Thank-you in advance guys! Ps. I also don't think I need the while command.. But I'm not quite sure how I'd do that without it... Thanks again guys! Quote Link to comment https://forums.phpfreaks.com/topic/266194-possibly-storing-values-in-a-variable/ Share on other sites More sharing options...
Pikachu2000 Posted July 24, 2012 Share Posted July 24, 2012 I think this is what you're asking for. Try it and see. <?php include ("connect.php"); $query = "SELECT UID FROM Users WHERE Type='AD'"; $result = mysql_query($query) or die(mysql_error()); while( $row = mysql_fetch_row($result) ) { $array[] = $row[0]; } $rand = array_rand($array); echo "<img src=\"$array[$rand].png>\"<br />\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266194-possibly-storing-values-in-a-variable/#findComment-1364113 Share on other sites More sharing options...
ash992 Posted July 24, 2012 Author Share Posted July 24, 2012 Dude you're awesome... XD This is the second time you've helped me with something, Thank-you very much! really appreciate it. Ps. Awesome name. Quote Link to comment https://forums.phpfreaks.com/topic/266194-possibly-storing-values-in-a-variable/#findComment-1364118 Share on other sites More sharing options...
DavidAM Posted July 24, 2012 Share Posted July 24, 2012 While that solution will work, I wonder how it will scale. You will be processing a lot of data from the database and in PHP that you don't really need. As the number of entries in the table increases, the process will take longer. What about this: <?php include ("connect.php"); $query = "SELECT MIN(UID), MAX(UID) FROM Users WHERE Type='AD'"; $result = mysql_query($query) or die(mysql_error()); # ADD THIS $row = mysql_fetch_array($result) $min = $row['MIN(UID)']; $max = $row ['MAX(UID)']; $rnd = rand($min, $max); $query = "SELECT UID FROM Users WHERE Type = 'AD' AND UID >= $rnd ORDER BY UID LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_array($result) $TheRandomID = $row['UID']; # REMOVE THE WHILE (Since we only have/need one row) #while($row = mysql_fetch_array($result)){ echo '<img src="' . $TheRandomID . '.png>" <br />'; #} ?> The second query will fetch the generated random ID or the next greatest UID (of type "AD"). Since we know that MAX(UID) is a valid selection, and rand() will not return anything greater, the query should always return a valid ID. It is one extra trip to the database, but we reduced the overall amount of data being transferred and processed. Quote Link to comment https://forums.phpfreaks.com/topic/266194-possibly-storing-values-in-a-variable/#findComment-1364121 Share on other sites More sharing options...
ash992 Posted July 24, 2012 Author Share Posted July 24, 2012 That works perfectly as well, I didn't really take processing time into account but thank-you for fixing it for me, Works great, Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/266194-possibly-storing-values-in-a-variable/#findComment-1364124 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.