Random Pic Problem

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
rmail2006
Forum Newbie
Posts: 3
Joined: Wed Aug 19, 2009 8:53 am

Random Pic Problem

Post by rmail2006 »

Im trying to choose a picture from my website but;
  • [li]it must be random[/li]
    [li]each pic is only allowed to be rated once per day[/li]
Iv set up the code but for some reason it doesnt always work.

Code: Select all

if($_GET[id]){
    
}
else
{
    $totNum = mysql_num_rows(mysql_query("SELECT * FROM totties"));
    echo $totNum."<br/>";
    $totSelect = mysql_fetch_array(mysql_query("SELECT * FROM totties ORDER BY rand() LIMIT 0,1"));
    $totCheck = mysql_fetch_array(mysql_query("SELECT * FROM ratings WHERE imgId='$totSelect[tottyId]' AND username='$userUsername'"));
    if($totCheck[id]){
    
        $timeDif = time() - $timeLimit;
        $totCheck2 = mysql_fetch_array(mysql_query("SELECT * FROM ratings WHERE id='$totCheck[id]' AND time > $timeDif"));
        $xCount=1;
        if ($totCheck2[id]){
            while($totCheck2[id]){
                $totSelect = mysql_fetch_array(mysql_query("SELECT * FROM totties ORDER BY rand() LIMIT 0,1"));
                $totCheck = mysql_fetch_array(mysql_query("SELECT * FROM ratings WHERE imgId='$totSelect[tottyId]' AND username='$userUsername'"));
                $totCheck2 = mysql_fetch_array(mysql_query("SELECT * FROM ratings WHERE imgId='$totSelect[id]' AND time > $timeDif"));
                if ($xCount==$totNum * 2){
                    echo "Rated All Girls";
                    $noGirls='1';
                    break;
                }
            $xCount++;
            }
            [color=red][b]$tottyChosen = $totSelect[tottyId];
            echo "Got Girl ID ".$totSelect[tottyId];[/b][/color]
            
            
        } else {
        $totDetails = mysql_fetch_array(mysql_query("SELECT * FROM usr_images WHERE id='$totSelect[tottyId]'"));
        echo "Rated But Time ID ".$totCheck[imgId];}
        $tottyChosen = $totCheck[imgId];
        
    }else{
        [color=red][b]echo " No Ratings ID ".$totSelect[tottyId];
        $tottyChosen = $totSelect[tottyId];[/b][/color]
    }
}
echo "<br/>".$tottyChosen;

when it writes 'rated but time id ' and 'No Ratings Id ' it passes the id to the string $tottyChosen. That's Fine

But when it randomly chooses another and echos 'Got Girl ID ' it doesnt pass the id to the string even though it prints it on screen.

Can anyone help please :(
Thanks
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Random Pic Problem

Post by VladSun »

Try to do all of this in a single SQL query. PHP should not be involved ;)

Try it and I'm gooing to help you with that.
Also, please post your DB scheme.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply