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!
Could someone please tell me what I need to change or add to allow this script to display a new image weekly instead of every time the page is refreshed?
<?
// Connect to the database
mysql_connect ('localhost', 'username', 'password') ;
mysql_select_db ('database_name');
// Edit this number to however many links you want displaying
$num_displayed = 3 ;
// Select random rows from the database
$result = mysql_query ("SELECT * FROM links ORDER BY RAND() LIMIT $num_displayed");
// For all the rows that you selected
while ($row = mysql_fetch_array($result))
{
// Display them to the screen...
echo "<a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" ;
}
?>
$today=date("w"); //current day of week: 0,1,2,3,4,5,6
$secs_so_far=mktime()-mktime(0,0,1); //seconds so far today
$days_before_today=$today;
if ($days_before_today>0)$secs_so_far+=$days_before_today*24*60*60; //if today isn't Sunday, add seconds for each day to the running total
$week_start=mktime()-($secs_so_far); //first second of week = (time now)-(seconds since the beginning)
// Edit this number to however many links you want displaying
$num_displayed = 1 ;
// Select random rows from the database
$result = mysql_query ("SELECT * FROM links ORDER BY RAND() where date='$week_start' LIMIT $num_displayed");
//if no links picked yet
if(@mysql_num_rows($result)<1){
//redo the query, finding $num_displayed more rows without dates
$result = mysql_query ("SELECT * FROM links ORDER BY RAND() where date='' LIMIT $num_displayed");
//update the 'date' field to match $week_start
$result = mysql_query ("UPDATE links SET date='$week_start' WHERE date='' limit 3");
// Select random rows from the database
$result = mysql_query ("SELECT * FROM links ORDER BY RAND() where date='$week_start' LIMIT $num_displayed");
//show the links that were just selected
while ($row = @mysql_fetch_array($result)){
echo "<a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" ;}
}
Last edited by outdoorxtreme1 on Wed Jul 25, 2007 10:47 am, edited 2 times in total.
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where date='1185076801' LIMIT 1' at line 1
I changed that and I get no errors but now I just get a blank screen. If I view the source code I don't see the echoed line <a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>
$today=date("w"); //current day of week: 0,1,2,3,4,5,6
$secs_so_far=mktime()-mktime(0,0,1); //seconds so far today
$days_before_today=$today;
if ($days_before_today>0)$secs_so_far+=$days_before_today*24*60*60; //if today isn't Sunday, add seconds for each day to the running total
$week_start=mktime()-($secs_so_far); //first second of week = (time now)-(seconds since the beginning)
// Edit this number to however many links you want displaying
$num_displayed = 1 ;
// Select random rows from the database
$result = mysql_query ("SELECT * FROM links where date='$week_start' ORDER BY RAND() LIMIT $num_displayed")or die ('Error: '.mysql_error ());
;//if no links picked yet
if(@mysql_num_rows($result)<1){
//redo the query, finding $num_displayed more rows without dates
$result = mysql_query ("SELECT * FROM links ORDER BY RAND() where date='' LIMIT $num_displayed")or die ('Error: '.mysql_error ());
;//update the 'date' field to match $week_start
$result = mysql_query ("UPDATE links SET date='$week_start' WHERE date='' limit $num_displayed")or die ('Error: '.mysql_error ());
;// Select random rows from the database
$result = mysql_query ("SELECT * FROM links ORDER BY RAND() where date='$week_start' LIMIT $num_displayed")or die ('Error: '.mysql_error ());
;//show the links that were just selected
echo mysql_num_rows();
while ($row = mysql_fetch_array($result)){
echo "<a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" ;}
}
$today=date("w"); //current day of week: 0,1,2,3,4,5,6
$secs_so_far=mktime()-mktime(0,0,1); //seconds so far today
$days_before_today=$today;
if ($days_before_today>0)$secs_so_far+=$days_before_today*24*60*60; //if today isn't Sunday, add seconds for each day to the running total
$week_start=mktime()-($secs_so_far); //first second of week = (time now)-(seconds since the beginning)
// Edit this number to however many links you want displaying
$num_displayed = 1 ;
// Select random rows from the database
$result = mysql_query ("SELECT * FROM links WHERE date='$week_start' ORDER BY RAND() LIMIT $num_displayed")or die ('Error: '.mysql_error ());
//if no links picked yet
if(@mysql_num_rows($result)<1){
//redo the query, finding $num_displayed more rows without dates
$result = mysql_query ("SELECT * FROM links WHERE date='' ORDER BY RAND() LIMIT $num_displayed")or die ('Error: '.mysql_error ());
//update the 'date' field to match $week_start
$result = mysql_query ("UPDATE links SET date='$week_start' WHERE date='' LIMIT $num_displayed")or die ('Error: '.mysql_error ());
// Select random rows from the database
$result = mysql_query ("SELECT * FROM links WHERE date='$week_start' ORDER BY RAND() LIMIT $num_displayed")or die ('Error: '.mysql_error ());
//show the links that were just selected
echo mysql_num_rows();
while ($row = mysql_fetch_array($result)){
echo "<a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" ;}
}