[SOLVED] Links of the day

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!

Moderator: General Moderators

Post Reply
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

[SOLVED] Links of the day

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Answers

Post 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.
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Re: Answers

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post 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>";
?>
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post 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>";
?>
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

And just to clarify, I have 36 categories, each with 4-40 urls.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

remove $i++;

for loops auto increment it
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

Sweet! Thanks everyone! Yeah, I see the double $i++ was cutting my results in half.

Thanks again, everything working now!
Post Reply