PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
This code extracts a random line from a text file 'links.txt'
I'm looking for code that will pull each line, sequentially from start to finish, one by one. Only one line should come up each time this script is run via cron job.
How would I do this? Please, I need help with the code, as I'm a newb to PHP. So one word answers, variables, or otherwise won't be of much use to me.
You'll need a way to track what line has already been called. You can store this in a database or a file. Here's a quick hack, it could be shortened up a bit:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
You can write to the file with the last quote you used but using a database would be faster. Create a table with a auto-incrementing quoteID, the quoteText and a used boolean. Then you can do something like this:
<?php
//connect to your database first
//check to see if all the quotes have been displayed yet, if so, reset them
$total_left_unused = mysql_query("SELECT 1 FROM quotestable WHERE used=0")
or die (mysql_error());
//there are no unused quotes left, reset them all
if (!mysql_num_rows($result))
mysql_query("UPDATE quotestable SET used=0")
or die ('cannot reset used quotes ' . mysql_error());
//select one un-used quote from the database at random
$result = mysql_query("SELECT * FROM quotestable WHERE used=0 ORDER BY RAND() LIMIT 1")
or die ('cannot select an unused quote: ' . mysql_error());
$row = mysql_fetch_array($result);
echo $row['quoteText']; //here's your one random quote
//change the quote so the status is set to used
mysql_query("UPDATE quotestable SET used=1 WHERE quoteID = '" . $row['quoteID'] . "'")
or die ('cannot update quote as being used ' . mysql_error());
?>
any way to pull the line, use it in the script then just immediately remove it from the text file?
so, call line 1 from links.txt for use in script.php
one it's called, and used, delete the line 1 from links.txt
I think what I'm looking for, is to always pull the 1st line from the text file, but to delete that same line after it's been placed into an array for use in script.php
The problem is, I'm letting some of my subscribers use this - and explaining database setup to them would be over their heads.
I need this to all be contained within: links.txt and script.php
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Since we're using file(), each array element will end with a newline. That's why I removed the FILE_IGNORE_NEW_LINES.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.