Need help with random image code

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

outdoorxtreme1
Forum Newbie
Posts: 18
Joined: Tue Mar 21, 2006 10:59 am

Post by outdoorxtreme1 »

nothing at all.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Code: Select all

mysql_num_rows($result);
Maybe I should have been more specific.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you need to pass $result to mysql_num_rows()
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Totally wrong. Must be

Code: Select all

echo mysql_num_rows($result);
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.
outdoorxtreme1
Forum Newbie
Posts: 18
Joined: Tue Mar 21, 2006 10:59 am

Post by outdoorxtreme1 »

Still nothing. Could the <1 in this line be wrong?

if(@mysql_num_rows($result)<1){
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Enable display_errors in your php.ini file or with ini_set(), looks like the issue is in the code. And remove any @ which suppress the error reporting.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I don't know what you're after, so I couldn't tell you.

And please... you are DEBUGGING the script. Stop using error suppression. You need all of the error messages you can get right now.
outdoorxtreme1
Forum Newbie
Posts: 18
Joined: Tue Mar 21, 2006 10:59 am

Post by outdoorxtreme1 »

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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Okay, that's fine and all, but our concern right now is the result of mysql_num_rows($result) right before the while loop.
outdoorxtreme1
Forum Newbie
Posts: 18
Joined: Tue Mar 21, 2006 10:59 am

Post by outdoorxtreme1 »

nothing on in the browser shows up. I view source code and nothing there either.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Turn on error reporting. $result can't possibly be a valid resource.

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

Post by outdoorxtreme1 »

Error reporting is on and I still dont get an echo.

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>" ;}
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

That's impossible.

Code: Select all

ini_set('display_errors', 1);
error_reporting(E_ALL);
The only way that could be possible is if you aren't even getting past your initial if statement.
outdoorxtreme1
Forum Newbie
Posts: 18
Joined: Tue Mar 21, 2006 10:59 am

Post by outdoorxtreme1 »

Every time I refresh there is nothing and the sorce code is blank also. Could there be another part of the code wrong?
outdoorxtreme1
Forum Newbie
Posts: 18
Joined: Tue Mar 21, 2006 10:59 am

Post by outdoorxtreme1 »

If I change this line

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.
Post Reply