phatgreenbuds Posted September 20, 2008 Share Posted September 20, 2008 In the code below I thought I was pulling specific fields from a row in my query and sticking them into an array...then I thought I was shuffling that array to randomize the order...it all worked to his point. However now when I try to pull the shuffled info out of the array to use it in a string, all I get is a blank page. This should work. Anyone got any idea why its not? Am I on crack and missing something obvious? <?php $query = "SELECT * FROM $tblname"; $content = mysql_query($query); $num = mysql_num_rows($content); if ($num != 0) { $xml = "test\r\n"; $count = 0; while ($row = mysql_fetch_array($content) && $count < 10) { $con[$count] = $row["win_path"]; $count++; } shuffle($row); $count1 = 0; while ($count1 < 10) { $xml .="blah blah is" . $con[$count1] . "\r\n"; $count1++; } echo $xml; } //print_r($con); ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted September 20, 2008 Share Posted September 20, 2008 Haven't looked at your code yet, but why not just order by rand() in your query string so the results are already randomized? Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 20, 2008 Share Posted September 20, 2008 1. Are you meant to be 'shuffle($row);' and not '$con' 2. You keep adding and printing xml, which repeats itself. 3. Not that those will seem to be your solution. 4. I like to pre define variables before logic and loop sets. 5. CV, too true... Quote Link to comment Share on other sites More sharing options...
.josh Posted September 20, 2008 Share Posted September 20, 2008 ....And it looks like you are wanting to limit it to 10 results, another thing you could be doing in your query string: SELECT * FROM $tblname ORDER BY RAND() LIMIT 10 Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted September 20, 2008 Author Share Posted September 20, 2008 yes that was a typo on my part...it should have been shuffle($con) If there is an easier way to do this I am open to it but I do not understand how to order the results by rand() Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted September 20, 2008 Author Share Posted September 20, 2008 OMG if its really that simple I am going to shoot my PC...I will try that now. 10 is just a test limit for now so I don't overwhelm my test box. this will eventually be in the 1000's Quote Link to comment Share on other sites More sharing options...
.josh Posted September 20, 2008 Share Posted September 20, 2008 You just want 10 random rows right? <?php $query = "SELECT * FROM $tblname ORDER BY RAND() LIMIT 10"; $content = mysql_query($query); while ($list = mysql_fetch_array($content)) { echo "blah blah is" . $list['win_path'] . "\r\n"; } Also if the only column you are using is 'win_path' then you should only select that from your table: <?php $query = "SELECT win_path FROM $tblname ORDER BY RAND() LIMIT 10"; $content = mysql_query($query); while ($list = mysql_fetch_array($content)) { echo "blah blah is" . $list['win_path'] . "\r\n"; } Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted September 20, 2008 Author Share Posted September 20, 2008 Say bye bye to my PC...i cannot believe I was making this so hard... Thank you for showing this to me. One last question...when this gets into potentially thousands of rows will it cause a performance hit or is this still the optimal method? Quote Link to comment Share on other sites More sharing options...
.josh Posted September 20, 2008 Share Posted September 20, 2008 As far as...what? No matter how many rows are in your table, it's always more efficient to do everything you can in the query itself. That's what the db is there for. Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted September 20, 2008 Author Share Posted September 20, 2008 excellent...thanks again... Quote Link to comment 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.