Jump to content

Randomizing query result


phatgreenbuds

Recommended Posts

This is a tough one.

 

In the code below I am taking entries in a database and writing them to a playlist file. Problem is that there can be 100's of duplicate rows in the database so it writes the same media back to back in the playlist.  I was playing with shuffling an array but not sure how I would apply that principle here.

 

<?php
$query = "SELECT * FROM test_table";
$content = mysql_query($query); 

$num = mysql_num_rows($content);	

if ($num != 0) { 																					
$file = fopen("../test/playlistAM.wsx", "w");
$_xml = "<?wsx version=\"1.0\"?>\r\n";
$_xml .= "<smil repeatCount=\"indefinite\">\r\n";

while ($row = mysql_fetch_array($content)) { 															
if ($row["content_id"]) {			 
  $_xml .="\t<media src=\"" . $row["win_path"] . "\"/>\r\n";
} 																						

else { 																						 
$_xml .="\t<media src=\"" . "Nothing to see here" . "\"/>\r\n";
$_xml .="</smil>"; 
fwrite($file, $_xml);
fclose($file);
} 																						
}

$_xml .="</smil>";
fwrite($file, $_xml);
fclose($file);
}

?>

Link to comment
https://forums.phpfreaks.com/topic/124746-randomizing-query-result/
Share on other sites

check out this little nugget. it goes through a whole assortment of different kinds of  queries.  This thing is a huge help for me.  You might want the DISTINCT keyword thats why i send you that page but there are 29 more pages.

 

http://www.webdevelopersnotes.com/tutorials/sql/online_mysql_guide_the_distinct_keyword.php3

You're right that is an awsome little sight that I now have bookmarked. However, the issue is not suppressing the duplicates...but rather I need to shuffle duplicates randomly so they do not appear back to back. In other words when a picture is uploaded and its to be displayed 10 times, I want it displayed all 10 but in between those 10 occurances there will be other pics.

 

I am looking on this site you gave but so far I see nothing like this...

correct...assume I upload my song and I want it played 10 times within the hour...others are also uploading their songs for various repeat times as well. Somehow I need to take that hours worth of music out of the database and randomly insert it into the playlist so the same song does not play back to back.

 

For accounting purposes if I say play it X amount of times then when I submit the upload it inserts X amount of rows in the database...this will be key for when I create the automated billing portion of the site.

My first thought is I am going to have to drop the query results into another array and shuffle() that array then yank it back out of that array with another while loop to write it to the playlist...just not sure if there is a more optimal way to do this.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.