I wanted a Daily Random Encyclopedia. A lot of posts about something like that, but the answers were always along the line of use Random Quotes and make your fortune file. Not the same thing.
Anyway, since I couldn’t find one I decided to try to make one. I got one working fairly quick that shows a random entry on each page load. But I want one that changes by day. But I’m not a coder and I’m stuck. It took me all day to figure this much out, don’t laugh too hard this is my first attempt. I’m just going to put the section I’m having trouble with, not the whole block. It should be obvious where I don’t know what to do.
This is based on adding a field called dateshown to the existing encyclopedia_text table. The default value is 0. Anyone following this thread should note that the addition of a new field requires changes in any files that try to create a new entry in that table.
Code:
$today = date ("mdy");
$encyquery = $db->sql_query("SELECT * FROM " . $prefix . "_encyclopedia_text where dateshown = $today");
//***
//This section I can't get right- How do I check for no matches?
If ($encyquery == Null) // If there is nothing with today's date then do next
$query = $db->sql_query("SELECT * FROM " . $prefix . "_encyclopedia_text where dateshown = 0 order by rand() limit 1 ");
//If nothing satisfies this query, then everything has been used. We need to reset all Date fields to Zero. How can I update a whole column at once?
//***
$row = sql_fetch_row($query);
$content .= "row[2]<br>$row[3]";
// for reference only $row[0]= tid, $row[1]= eid, $row[2]= title, $$row[3]= text, $row[4]= counter, $row[5]= dateshown
$db->sql_query("update ".$prefix."_encyclopedia_text *** dateshown = $today where tid= $row[0] "); // Enters the date shown (today)
Note that the last line above I had to use *** instead of the word that updates fields to make this post. Took me half an hour to figure out why it wouldn't let me post, lol. Can't use that word at all I guess.
It’s possible that a split second page load could cause this to be shown a second day using this method. I originally had another field called shown that I was using 0 or 1 to check if it had been shown, and only updating a date field if it passed to that point, but it got too muddled for me at my skill level.
Input?
Evaders99 Site Admin
Joined: Aug 17, 2003
Posts: 12350
Posted:
Thu Mar 06, 2008 6:12 am
Use $db->sql_fetchrow rather than sql_fetch_row. If it returns a valid row, it will return true
OK, I've muddled thru and got it mostly working. The first thing I need to say is even a nube such as me can see this is very bloated. I have too many queries, but that was what I found that works thru trial and error and I still don’t fully understand how the query results are stored. The next part I still can’t figure out is how to reset all the showndate fields of the whole table to zero. I’m looking into how to make an array and use a While statement, but I’m still reading.
An experienced coder could probably hack this out in a few minutes compared to my two day crash course. So if anyone has the compassion, please help. I learn best by seeing what works and working in reverse.
The part in my code that is: $content .="Everything used.<br>"; is just my reference so I could see onscreen what is going on. That is the section I still don’t have doing what I want.
Here’s what I have so far, again I used *** for the word that updates fields.
Code:
$encyquery = $db->sql_query("SELECT * FROM " . $prefix . "_encyclopedia_text where dateshown = $today");
if (!($row = $db->sql_fetchrow($encyquery))) // If no date matches today
{
$encyquery2 = $db->sql_query("SELECT * FROM " . $prefix . "_encyclopedia_text where dateshown = 0 order by rand() limit 1 ");// Select a Random entry from ones left
if (!($row = $db->sql_fetchrow($encyquery2))) // If no date matches today and none left (no zero in date field)
{
$content .="Everything used.<br>"; // We need to put a zero in the date field for all entries
$db->sql_query("update ".$prefix."_encyclopedia_text *** dateshown = 0 where tid= 1 ");
}
$encyquery3 = $db->sql_query("SELECT * FROM " . $prefix . "_encyclopedia_text where dateshown = 0 order by rand() limit 1 ");
$row = sql_fetch_row($encyquery3);
$content .= "This is from line 112<br>$row[0]<br>$row[2]<br>$row[3]";
$db->sql_query("update ".$prefix."_encyclopedia_text *** dateshown = $today where tid= $row[0] "); // Enters the date shown (today)
}
else // If there is an entry with today's date
{
$encyquery = $db->sql_query("SELECT * FROM " . $prefix . "_encyclopedia_text where dateshown = $today");
$row = sql_fetch_row($encyquery);
$content .= "This is from line 120<br>$row[2]<br>$row[3]";
}
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum