Page 1 of 3
Need help with random image code
Posted: Thu Jul 19, 2007 9:09 am
by outdoorxtreme1
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?
Thanks.
Code: Select all
<?
// 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>" ;
}
?>
Posted: Thu Jul 19, 2007 12:57 pm
by Gente
Add 'where' condition
Posted: Tue Jul 24, 2007 2:21 pm
by outdoorxtreme1
I changed the code a little bit and am still having problems. Any ideas anyone?
Code: Select all
$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>" ;}
}
Posted: Wed Jul 25, 2007 10:35 am
by outdoorxtreme1
I forgot to post the error I am getting. Sorry about that. Here it is.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 26
Posted: Wed Jul 25, 2007 10:44 am
by superdezign
outdoorxtreme1 wrote:I forgot to post the error I am getting. Sorry about that. Here it is.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 26
Likely, you have an invalid query. Try adding 'mysql_query(whatever)
or die(mysql_error());' at the end of your queries to see what's going wrong.
Posted: Wed Jul 25, 2007 10:52 am
by outdoorxtreme1
Ok. I did that and here is the error :
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'm not exaclty sure what is wrong.
Posted: Wed Jul 25, 2007 11:03 am
by miro_igov
ORDER BY RAND() where date='$week_start'
first is WHERE [condition] , then ORDER BY, then LIMIT
Posted: Wed Jul 25, 2007 11:14 am
by outdoorxtreme1
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>
Posted: Wed Jul 25, 2007 11:18 am
by miro_igov
Did you enabled display_errors and error_reporting level?
Posted: Wed Jul 25, 2007 12:44 pm
by outdoorxtreme1
Yes and 2039 is the error_reporting
Posted: Wed Jul 25, 2007 12:57 pm
by superdezign
Try echoing mysql_num_rows() right before the while loop. And you shouldn't need error suppression on any MySQL 'fetch' functions.
Posted: Wed Jul 25, 2007 1:45 pm
by outdoorxtreme1
I did the echo and got rid of the error suppression in the fetch function. Still nothing.
Code: Select all
$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>" ;}
}
Posted: Wed Jul 25, 2007 1:50 pm
by miro_igov
My friend, i told you that such a mysql query is wrong
Code: Select all
result = mysql_query ("SELECT * FROM links ORDER BY RAND() where date='' LIMIT $num_displayed")or die ('Error: '.mysql_error ());
You should first use the WHERE clause then ORDER BY (if any) and after that LIMIT (if needed). Otherwise the mysql query will be wrong.
I wonder why you still use the wrong order of commands.
Posted: Wed Jul 25, 2007 1:57 pm
by outdoorxtreme1
Ok. I fixed that and still nothing is being echoed.
Current code:
Code: Select all
$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>" ;}
}
Posted: Wed Jul 25, 2007 2:02 pm
by superdezign
And what does mysql_num_rows() echo to the screen?