Page 1 of 1
[SOLVED] Links of the day
Posted: Thu Oct 07, 2004 9:59 pm
by jabbaonthedais
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.
Answers
Posted: Thu Oct 07, 2004 10:14 pm
by neophyte
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.
Re: Answers
Posted: Thu Oct 07, 2004 10:37 pm
by jabbaonthedais
neophyte wrote:Question 2 -- I didn't really follow what you were getting at -- sorry.
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.
Hope that cleared that up.
Posted: Thu Oct 07, 2004 11:43 pm
by neophyte
How bout a cookie that has string of record numbers seperated with some character. You can test against it to see if they've visited the page before and store which records they've already seen.
$_COOKIE['links'] = "1/2/6/5/4/8";
Posted: Fri Oct 08, 2004 1:34 am
by feyd
I'd recommend
not storing which ones were selected in a cookie. I'd use a table in the database where each record has (at least) a timestamp column and a the link id used. You can then delete x days old records, then select all the records.

Posted: Fri Oct 08, 2004 3:10 am
by phpScott
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.
Posted: Fri Oct 08, 2004 8:34 am
by jabbaonthedais
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>";
?>
Posted: Fri Oct 08, 2004 8:37 am
by malcolmboston
do this
Code: Select all
for ($i = 1; $i <= 36; $i++)
{
// code to run 36 times here
}
not only is it better, but this is the standard
Hope that helps
Posted: Fri Oct 08, 2004 8:46 am
by jabbaonthedais
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>";
?>
Posted: Fri Oct 08, 2004 8:47 am
by jabbaonthedais
And just to clarify, I have 36 categories, each with 4-40 urls.
Posted: Fri Oct 08, 2004 8:49 am
by malcolmboston
remove $i++;
for loops auto increment it
Posted: Fri Oct 08, 2004 8:51 am
by jabbaonthedais
Sweet! Thanks everyone! Yeah, I see the double $i++ was cutting my results in half.
Thanks again, everything working now!