sparkbark Posted October 8, 2007 Share Posted October 8, 2007 hello, i want to use a script that will call up random text files for insertion in a webpage, which is easy enough, but the important thing is that it needs to cycle through all of the text files in the directory first before it repeats any of them. is this possible? how would i go about modifying a regular 'random text file' script into one that will behave this way? Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/ Share on other sites More sharing options...
sKunKbad Posted October 8, 2007 Share Posted October 8, 2007 You would have to take your standard text randomizer, and use something like cookies or a session to hold the values of the text that has already been output. I believe this would only work if the page refreshed each time, because I do not know if cookies/sessions can be used with ajax. I had a nice javascript text randomizer on my site, and I always wanted this functionality you desire, but never got around to implementing it. Please post your final code when you figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-364363 Share on other sites More sharing options...
cooldude832 Posted October 8, 2007 Share Posted October 8, 2007 yes cookies/sessions would be helpful, however ajax can have a solution  The ajax solution:  1) Set up a mysql table with the list of lines and a primary key of textID 2) Set up a table called temp_viewed with a primary key of SessionID, and fields of all the textIDs from the first table 3) Set an intial session on loading, store this sessionID in the mysql table in part 2 4) In your ajax area of the page run a query that says get all the textIDs from 2 that this sessionID id has not viewed, put them in ($array) array and count 4b) after that generate a random number from 0 to the count-1 of the above mentioned array call it $random_id 5) query table in part 1 for $array($random_id) and display it 5b) Update the table in 2 to reflect that this ID has been viewed. 6) Enjoy  and for clean up you can add an IP address to that table on the initial session creation say, if the same ip has another row delete it cause its pointless. Also you can probably get a cron job to delete old entries once a week, or just do a full flush on the table as its not terribly crucial data.  Make sense?  Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-364367 Share on other sites More sharing options...
sKunKbad Posted October 8, 2007 Share Posted October 8, 2007 I found this: http://www.savdesign.com/articles.php?article_id=23 which is a javascript random image generator with no repeat. It can probably be easily adapted to text. Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-364378 Share on other sites More sharing options...
sKunKbad Posted October 8, 2007 Share Posted October 8, 2007 I was bored, so I made this, and it does work, but can probably be refined or changed to suit your needs: Â <?php session_start(); echo count($_SESSION['randomNums']) . "<br>"; print_r($_SESSION['randomNums']); function randomNoRepeat(){ $max= 6; $num = Rand (1,$max); if(isset($_SESSION['randomNums']) && in_array($num,$_SESSION['randomNums'])){ if (count($_SESSION['randomNums']) == 6){ switch ($num) { case 1: echo "Time is money"; break; case 2: echo "An apple a day keeps the doctor away"; break; case 3: echo "Elmo loves dorthy"; break; case 4: echo "Off to see the wizard"; break; case 5: echo "Tomorrow is another day"; break; case 6: echo "PHP is cool!"; } unset($_SESSION['randomNums']); }else{ randomNoRepeat(); } }else{ switch ($num) { case 1: echo "Time is money"; $_SESSION[randomNums][] = $num; break; case 2: echo "An apple a day keeps the doctor away"; $_SESSION[randomNums][] = $num; break; case 3: echo "Elmo loves dorthy"; $_SESSION[randomNums][] = $num; break; case 4: echo "Off to see the wizard"; $_SESSION[randomNums][] = $num; break; case 5: echo "Tomorrow is another day"; $_SESSION[randomNums][] = $num; break; case 6: echo "PHP is cool!"; $_SESSION[randomNums][] = $num; } } } randomNoRepeat(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-364468 Share on other sites More sharing options...
cooldude832 Posted October 8, 2007 Share Posted October 8, 2007 Yeah I think the better idea instead of running a possible infintinte loop would be to first have the full set of all possible things, then subtract from that the list of ones used, put those in a single array and then generate a random value from that and that is your given string key. such as (note I am using mysql vs sessions to store as if you get a lot of strigns it will bog down your server <?php session_start(); $strings = array("Text1", "Text2", "Text3", "Text 4"); $num_items = count($strings); //Should return 4 in this case connectSQL(); //Connect to sql and pick your db $s_id = session_id(); $q = "Select String_ID From `temp_data` Where Session_id = '".$s_id."' Order by String_ID"; $r = mysql_query($q) or die(mysql_error()); $used_strings = array(); while($row = mysql_fetch_assoc($r)){ $used_strings[] = $row['String_ID']; } //Now we need to use what is used to what is not used $unused_strings = array(); for($i=0; $i<=$num_items; $i++){ //If that counting number $i is not equal to that given array value, then we know that string is open for use if($i != $used_strings[$i]){ $unused_strings[] = $used_strings[$i]; } } $picked_string = array_rand($unused_strings); echo $strings[$picked_string]; //Now we need mysql to reflect this so we say $q = "Insert into `temp_data` fields(Session_ID, String_id) Value ('".$s_id."', '".$picked_string."')"; $r = mysql_query($q) or die(mysql_error()); ?>  You could use the above in ajaxs, you just need ajaxs to do all of the above, but echo it out in a different fashion.  Also note how I don't care if the mysql result is 0 rows, this is because the array $used_strings will result in 0 items and you will have any string open to use, yes you could test for it, but what is the point. Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-364605 Share on other sites More sharing options...
sKunKbad Posted October 8, 2007 Share Posted October 8, 2007 There is no possible infinite loop with my code, run it and see. Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-364642 Share on other sites More sharing options...
sKunKbad Posted October 8, 2007 Share Posted October 8, 2007 Here is another way you can do it, which is more convenient, because you can store all the text to be randomized in an array: <?php session_start(); function randomNoRepeat(){ include ('randomText.inc'); $max = count($arr); $num = Rand (0,($max-1)); if(isset($_SESSION['randomNums']) && in_array($num,$_SESSION['randomNums'])){ if (count($_SESSION['randomNums']) == $max){ echo $arr[$num]; unset($_SESSION['randomNums']); }else{ randomNoRepeat(); } }else{ echo $arr[$num]; $_SESSION[randomNums][] = $num; } } randomNoRepeat(); ?> Â Â Here is the include file I used that has the array in it: <?php $arr = array( 'I like tacos.', 'Becky loves to work at my store.', 'My brother is finally moving out!', 'I gotta get back to work.', 'Old ladies always have problems.' ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365013 Share on other sites More sharing options...
sparkbark Posted October 9, 2007 Author Share Posted October 9, 2007 hi all, thanks for the answers. however, i should have specified that these need to be text FILES, not just sentences. they will be multiple paragraphs each. so the script needs to call seperate files for display. Â i also am just starting with php and i don't understand half of this... sorry Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365077 Share on other sites More sharing options...
cooldude832 Posted October 9, 2007 Share Posted October 9, 2007 it don't matte cause just make your array the name of the files and use fopen vs an echo Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365085 Share on other sites More sharing options...
sparkbark Posted October 9, 2007 Author Share Posted October 9, 2007 could somebody explain it in detail to me, because i am stupid Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365089 Share on other sites More sharing options...
sKunKbad Posted October 9, 2007 Share Posted October 9, 2007 You are going to have to learn php for an explaination, but here is some code that will do what you want with files: Â <?php session_start(); function randomNoRepeat(){ include ('randomTextFiles.inc'); $max = count($arr); $num = Rand (0,($max-1)); if(isset($_SESSION['randomNums']) && in_array($num,$_SESSION['randomNums'])){ if (count($_SESSION['randomNums']) == $max){ $fileNum = $arr[$num]; $handle = file($fileNum); foreach ($handle as $line_num => $line) { echo $line; } unset($_SESSION['randomNums']); }else{ randomNoRepeat(); } }else{ $fileNum = $arr[$num]; $handle = file($fileNum); foreach ($handle as $line_num => $line) { echo $line; } $_SESSION[randomNums][] = $num; } } randomNoRepeat(); ?> Â And here is the include file with the names of the files you want to output. You didn't say what kind of files these are, so they may require some formatting to output the way you want. Â <?php $arr = array( 'file_a.txt', 'file_b.txt', 'file_c.txt', 'file_d.txt', 'file_e.txt' ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365108 Share on other sites More sharing options...
sparkbark Posted October 9, 2007 Author Share Posted October 9, 2007 thank you that does exactly what i want it to! Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365130 Share on other sites More sharing options...
sparkbark Posted October 9, 2007 Author Share Posted October 9, 2007 wait, now it has started repeating entries before it has gone through them all. i cleared my cache, cookies, etc and tried again and it still is broken Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365137 Share on other sites More sharing options...
prime Posted October 9, 2007 Share Posted October 9, 2007 you could have a counter for execution and keep the actualy number stores in a database or flat file, and have it read that and basicaly add one each time.  and if you have say 6 files you want to cycle through, you simply add an if scenario that recycles back to 1 after the 6 have been shown  similair to below (not actual code)  retreive database number execute database number I++ database number if number equals 7 (if you have 6 files assign value of 1) store back into database  you'd need a database table or flat file as I said but that wouldn't be too hard if you've worked with them at all, I'll the actual code if needed but it's not really that complicated Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365170 Share on other sites More sharing options...
sparkbark Posted October 9, 2007 Author Share Posted October 9, 2007 how do i do that? Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365371 Share on other sites More sharing options...
sKunKbad Posted October 9, 2007 Share Posted October 9, 2007 wait, now it has started repeating entries before it has gone through them all. i cleared my cache, cookies, etc and tried again and it still is broken  Maybe somebody can point out what is wrong with my script. It may be easier to fix it at this point, but I'm at work and can't spend time on it right now. Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365636 Share on other sites More sharing options...
sasa Posted October 9, 2007 Share Posted October 9, 2007 try <?php session_start(); //1st generat $array_text_files with all text files in it $a_text_files = array('one','two' ,'sasa','angelina', 75, 'is it works?'); if (!array_key_exists('rand_text_files', $_SESSION)) $_SESSION['rand_text_files'] = $a_text_files; if (count($_SESSION['rand_text_files']) == 0) $_SESSION['rand_text_files'] = $a_text_files; shuffle($_SESSION['rand_text_files']); $rand_text = $_SESSION['rand_text_files'][0]; // unset($_SESSION['rand_text_files'][0]); //prevent repeats // do what you want with $rand_text echo $rand_text; ?> Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365659 Share on other sites More sharing options...
sKunKbad Posted October 9, 2007 Share Posted October 9, 2007 Nice code sasa! It's amazing the way that different people will try to solve the same problem. Â Check this out sparkbark: Â <?php session_start(); include('sasa_array.inc'); if (!array_key_exists('rand_text_files', $_SESSION)) $_SESSION['rand_text_files'] = $a_text_files; if (count($_SESSION['rand_text_files']) == 0) $_SESSION['rand_text_files'] = $a_text_files; shuffle($_SESSION['rand_text_files']); $rand_text = $_SESSION['rand_text_files'][0]; unset($_SESSION['rand_text_files'][0]); $handle = file($rand_text); foreach ($handle as $line_num => $line) { echo $line; } ?> Â The array of files in sasa_array.inc: <?php $a_text_files = array( 'file_a.txt', Â Â Â Â 'file_b.txt' , Â Â Â Â 'file_c.txt', Â Â Â Â 'file_d.txt', Â Â Â Â 'file_e.txt' Â Â Â Â ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365725 Share on other sites More sharing options...
sparkbark Posted October 10, 2007 Author Share Posted October 10, 2007 thanks that seems to work! Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365844 Share on other sites More sharing options...
sparkbark Posted October 10, 2007 Author Share Posted October 10, 2007 it stopped working again! Â it will work right at first, even through many sessions, and opening and closing of the window. but then suddenly it stops working the next time i check it... Â why is it behaving this way? Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365886 Share on other sites More sharing options...
sKunKbad Posted October 10, 2007 Share Posted October 10, 2007 Post your actual code. Â it stopped working again! Â it will work right at first, even through many sessions, and opening and closing of the window. but then suddenly it stops working the next time i check it... Â why is it behaving this way? Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-365899 Share on other sites More sharing options...
prime Posted October 10, 2007 Share Posted October 10, 2007 I have to agree nice code sasa. Â you are remember to put the top block of code at the top right, session does work with cookies, so it'll have to go before any output is sent to the browser unless your using output buffering, which is a great thing to use anyhow. Â sasa's code is fine, I've actually tested it myself, it must be a bug in your code somewhere, or an incompatibility somewhere Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-366008 Share on other sites More sharing options...
sKunKbad Posted October 10, 2007 Share Posted October 10, 2007 Actually, sparkbark, what prime is saying is true. You just need to know that the <?php session_start(); ?> needs to go at the very top of the code, even before your doctype or html tag, and the code should work. Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-366371 Share on other sites More sharing options...
sparkbark Posted October 10, 2007 Author Share Posted October 10, 2007 if it all goes at the top then how is it supposed to know which div i want it to go in? Quote Link to comment https://forums.phpfreaks.com/topic/72252-need-help-with-random-text-script/#findComment-366473 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.