[SOLVED] Links of the day
Moderator: General Moderators
-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
[SOLVED] Links of the day
I'm still fairly new to php (the dreaded first liner). I have a script to get a random link of the day. I just need some advice if you don't mind. Want to figure out the best way to do this before I invest a lot of time in it.
I'm wanting to have about 10 random links of the day, but only one from each category. Each link will have a category, image, url, and description. I also want to have the previous day's links, so I have to keep up with the random ones.
I have two questions:
1. Should I have one table, and have the columns (Category, IMAGE, URL, Description)? Or should I have a seperate table per category?
2. How do I keep up with the chosen links? Should I create a table, and use the datestamp to recall it? So I would start the script, if todaysdate=datestamp (last date in table), load info, else choose random links.
I'm wanting to have about 10 random links of the day, but only one from each category. Each link will have a category, image, url, and description. I also want to have the previous day's links, so I have to keep up with the random ones.
I have two questions:
1. Should I have one table, and have the columns (Category, IMAGE, URL, Description)? Or should I have a seperate table per category?
2. How do I keep up with the chosen links? Should I create a table, and use the datestamp to recall it? So I would start the script, if todaysdate=datestamp (last date in table), load info, else choose random links.
Answers
Question 1 --- If you have more than one category assigned to a single link than you need a link table and a category assignment table. (Ex. Link to news article on: Perl Coding on Linux category = Perl, category =Linux or category = Both) If their is only one category per link, I'd still use at least two; one code table with the various category names and a category id that you insert into the main links table that holds the links.
Question 2 -- I didn't really follow what you were getting at -- sorry.
Question 2 -- I didn't really follow what you were getting at -- sorry.
-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
Re: Answers
Sorry, what I meant is I know I don't want the script to run every time someone loads the page. Basically, it will check everytime someone loads the page to see if that day's links have already been chosen (by the script). If not, it runs the script. Also, I need to store at least the last few days' results, so I can have a "Previous day's links" page.neophyte wrote:Question 2 -- I didn't really follow what you were getting at -- sorry.
Hope that cleared that up.
create a table as feyd has said. linkId and timestamp.
Create a script that every one will use that checks to see if the current timestamp in the table is the same as today if so just get all the links, other wise create a new set of links.
Alternatively if you have access to the server and can use cron(assuming unix/linux based) then you can just create a script that will update the db when ever you want.
Create a script that every one will use that checks to see if the current timestamp in the table is the same as today if so just get all the links, other wise create a new set of links.
Alternatively if you have access to the server and can use cron(assuming unix/linux based) then you can just create a script that will update the db when ever you want.
-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
Thanks guys for all the help! I have a problem now. This code makes the random number like I want, but I can't figure out why it isn't looping. It should bring back the results 36 times, but it only does it once.
Code: Select all
<?
mysql_connect ('localhost', 'admin', 'password') ;
mysql_select_db ('phpbb');
$i = 1;
// 1-36
if ( $i < 37 ) {
$result = mysql_query("SELECT id,url,text FROM zlinks WHERE cat=$i");
$cnt = mysql_num_rows($result);
$rw = mysql_fetch_row($result);
$abb = ( $cnt + $rw[0] );
$add = ( $abb - $cnt );
// rand
srand ((double) microtime( )*1000000);
$z = rand($add,$abb);
$t = mysql_query("SELECT id,url,text FROM zlinks WHERE id=$z");
$row[$i] = mysql_fetch_row($t);
echo $row[$i][1] . " - " . $row[$i][2];
print "<br>";
$i++;
}
print "<p>Done.<p>";
?>-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
do this
not only is it better, but this is the standard
Hope that helps
Code: Select all
for ($i = 1; $i <= 36; $i++)
{
// code to run 36 times here
}Hope that helps
-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
Thanks malcolm! It looped, but it only brings back 18. But I don't see what could be doing that, unless it's some limitation in my code:
Code: Select all
<?
mysql_connect ('localhost', 'admin', 'password') ;
mysql_select_db ('phpbb');
// 1-36
for ($i = 1; $i <= 36; $i++){
$result = mysql_query("SELECT id,url,text FROM zlinks WHERE cat=$i");
$cnt = mysql_num_rows($result);
$rw = mysql_fetch_row($result);
$abb = ( $cnt + $rw[0] );
$add = ( $abb - $cnt );
// rand
srand ((double) microtime( )*1000000);
$z = rand($add,$abb);
$t = mysql_query("SELECT id,url,text FROM zlinks WHERE id=$z");
$row[$i] = mysql_fetch_row($t);
echo $row[$i][1] . " - " . $row[$i][2];
print "<br>";
$i++;
}
print "<p>Done.<p>";
?>-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm