Need help with random image code
Moderator: General Moderators
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Code: Select all
mysql_num_rows($result);- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Totally wrong. Must be
mysql_num_rows() requires argument, but if you say "Nothing at all" then your display_errors is disabled so this will make it hard to debug the script.
Code: Select all
echo mysql_num_rows($result);-
outdoorxtreme1
- Forum Newbie
- Posts: 18
- Joined: Tue Mar 21, 2006 10:59 am
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
outdoorxtreme1
- Forum Newbie
- Posts: 18
- Joined: Tue Mar 21, 2006 10:59 am
I went thru and took all the error suppression out of the code. I forgot about the line I had it in. What I am looking to do is echo <a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" selecting random rows from my database at a weekly time period.
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" selecting random rows from my database at a weekly time period.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
outdoorxtreme1
- Forum Newbie
- Posts: 18
- Joined: Tue Mar 21, 2006 10:59 am
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Turn on error reporting. $result can't possibly be a valid resource.
Also, run this before the while loop:
Also, run this before the while loop:
Code: Select all
if(!is_resource($result)) echo 'Invalid resource.';-
outdoorxtreme1
- Forum Newbie
- Posts: 18
- Joined: Tue Mar 21, 2006 10:59 am
Error reporting is on and I still dont get an echo.
Current code:
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 ());
if(!is_resource($result)) echo 'Invalid resource.';
echo mysql_num_rows($result);
//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>" ;}
}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
That's impossible.
The only way that could be possible is if you aren't even getting past your initial if statement.
Code: Select all
ini_set('display_errors', 1);
error_reporting(E_ALL);-
outdoorxtreme1
- Forum Newbie
- Posts: 18
- Joined: Tue Mar 21, 2006 10:59 am
-
outdoorxtreme1
- Forum Newbie
- Posts: 18
- Joined: Tue Mar 21, 2006 10:59 am
If I change this line
to this
then I get an echo but the pictures appear randomly each time the page is refreshed. What I am looking to do is Have the same picture for a week then after a week switch to another picture at random using the location of that picture from the database table.
Code: Select all
if(mysql_num_rows($result)>1){to this
Code: Select all
if(mysql_num_rows($result)){then I get an echo but the pictures appear randomly each time the page is refreshed. What I am looking to do is Have the same picture for a week then after a week switch to another picture at random using the location of that picture from the database table.
Last edited by outdoorxtreme1 on Wed Jul 25, 2007 4:02 pm, edited 3 times in total.