Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> How To's
Author Message
Dibas
New Member
New Member



Joined: Apr 04, 2005
Posts: 16

PostPosted: Fri May 19, 2006 5:06 am Reply with quote

Hi there, im trying to implement a code to show up a random image, using php/mysql...

this is what i'm using:

Code:
global $prefix, $db;


$result = $db->sql_query("SELECT pic, id, banda, FROM ".$prefix."_bandas RAND() 0,1");
while ($row = $db->sql_fetchrow($result)) {
$id = intval($row['id']);
$banda = stripslashes($row['banda']);
$pic = stripslashes(check_html($row['pic'], "nohtml"));

echo "<img src=\"/images/bandas/$pic\" alt=\"$banda\"><br><strong><a href=\"banda$id.html\">$banda</a></strong>";


Though... for some reason i can't understand i can't get the $pic to show up the image source... all i get is a blank image with the address /images/bandas/ <-- no picture file (whish is defined on the table under "pic".
 
View user's profile Send private message
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Fri May 19, 2006 5:29 am Reply with quote

I have never tried the RAND() function, so cannot comment on whether the query is working or not. Have you tried something like this in your while to make sure the query is returning the data properly?

Code:


$result = $db->sql_query("SELECT pic, id, banda, FROM ".$prefix."_bandas RAND() 0,1");
while ($row = $db->sql_fetchrow($result)) {
echo "id = ".$row['id']." :: ";
echo "banda = ".$row['banda']." :: ";
echo "pic = ".$row['pic']."<br>";


I am just not seeing anything immediately obvious that is wrong with your code. The $pic variable is not getting valued for some reason.

_________________
Where Do YOU Stand?
HTML Newsletter::ShortLinks::Mailer::Downloads and more... 
View user's profile Send private message Visit poster's website
Dibas







PostPosted: Fri May 19, 2006 6:22 am Reply with quote

ok i had some errors on the code but i tryied yours with my fixes on it... i only get this:

id = 21 :: banda = :: pic =

he's only catching the ID... not the correspondent pic and banda from that ID...
 
montego







PostPosted: Fri May 19, 2006 6:31 am Reply with quote

And you have validated using something like phpMyAdmin that ALL of your rows of data have these fields valued properly? If so, please post the table schema here so I can take a look at it.
 
Dibas







PostPosted: Fri May 19, 2006 6:45 am Reply with quote

ok... first here's the correction to that code:

Code:
$result = $db->sql_query("SELECT pic, id, banda FROM ".$prefix."_bandas ORDER BY RAND() LIMIT 0,1");

while ($row = $db->sql_fetchrow($result)) {
echo "id = ".$row['id']." :: ";
echo "banda = ".$row['banda']." :: ";
echo "pic = ".$row['pic']."<br>";


here's the table structure:

#
# Table structure for table nuke_bandas
#

DROP TABLE IF EXISTS `nuke_bandas`;
CREATE TABLE `nuke_bandas` (
`Id` int(11) NOT NULL auto_increment,
`banda` varchar(60) NOT NULL default '',
`bio` text NOT NULL,
`email` varchar(60) default NULL,
`url` varchar(100) NOT NULL default '',
`pic` varchar(100) default NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
montego







PostPosted: Fri May 19, 2006 6:53 am Reply with quote

You changed the code on the SQL. Is this working now?
 
Dibas







PostPosted: Fri May 19, 2006 6:54 am Reply with quote

no it.. gives that result i showed u.. only id has a result.
 
montego







PostPosted: Fri May 19, 2006 7:42 am Reply with quote

Quote:

And you have validated using something like phpMyAdmin that ALL of your rows of data have these fields valued properly?


Ok, need to know the answer to this original question then. If you are seeing the data in the database, then take out the ORDER BY RAND() LIMIT 0,1 and at least make sure your code is working.

Like I said, haven't used the RAND function in SQL so just trying to rule that out.
 
Dibas







PostPosted: Fri May 19, 2006 8:00 am Reply with quote

yes the rows have data... i did a order by id... i still can't get data from the other fields... only ID:

id = 1 :: banda = :: pic =

theres definatly something wrong in the code, but no idea what.
 
CodyG
Life Cycles Becoming CPU Cycles



Joined: Jan 02, 2003
Posts: 714
Location: Vancouver Island

PostPosted: Fri May 19, 2006 8:14 am Reply with quote

I'm no expert, but ... might it have something to do with using a mixed case for the id? Shouldn't it be Id all the way through the script as the db table is Id.

Just for reference, here is another random picture script that works for me.

Code:


<?php

if (eregi("block-randomphoto.php",$PHP_SELF)) {
    Header("Location: index.php");
    die();
}


        global $user,$user_prefix,$dbi,$prefix;

        $total = sql_fetch_array(sql_query("SELECT COUNT(id) AS total FROM ".$user_prefix."_user_profile1 where photo='1' and approved='1'",$dbi),$dbi);

        $p = mt_rand(0,($total[total] - 1));

        $pic = sql_fetch_array(sql_query("SELECT ffusername,photopath FROM ".$user_prefix."_user_profile1 where photo='1' and approved='1' LIMIT $p,1",$dbi),$dbi);


       $content = "<center><a href=\"modules.php?name=FriendFinder&op=userinfo&ffusername=$pic[ffusername]\"><img src=\"modules/FriendFinder/images/members/thumbs/$pic[photopath]\" border=\"0\" alt=\"$pic[ffusername]\"><br><font size=\"1\">$pic[ffusername]</font></a><br><a href=\"modules.php?name=FriendFinder\">Go To Friendfinder</a></center>";

?>


_________________
"We want to see if life is ubiquitous." D.Goldin 
View user's profile Send private message
montego







PostPosted: Sat May 20, 2006 12:42 am Reply with quote

Well, the interesting thing is Dibas is getting the id to be valued from the database.

I have been looking at this from a million angles and the code looks like, only that I don't know what is after this code. I didn't put a closing } on my while statement because it was meant to show where to INSERT a few lines of code and not replace. But I am just not seeing anything else wrong with it.
 
Dibas







PostPosted: Sat May 20, 2006 6:58 am Reply with quote

Yeah i can't understand why is only getting the results from the ID and not the correspondent results of banda and pic of that ID.
 
Dibas







PostPosted: Sat May 20, 2006 7:24 am Reply with quote

i noticed some weird thing on your code montego..

Code:
echo "id = ".$row['id']." :: ";

echo "banda = ".$row['banda']." :: ";
echo "pic = ".$row['pic']."<br>";


if i change for instance the order... first the banda echo.. i get result on that one and none on Id... lol... pretty weird. the first one echoing is the only one with results.. no matter what column is.
 
Dibas







PostPosted: Sat May 20, 2006 7:56 am Reply with quote

Ok... after a though battle i finally figured out a way to make it work... here's my final code working

Code:
<?php


global $prefix, $db;

$result = $db->sql_query("SELECT id FROM ".$prefix."_bandas ORDER BY RAND() LIMIT 0,1");
while ($row = $db->sql_fetchrow($result)) {
$id = intval($row['id']);

$result2 = $db->sql_query("SELECT banda FROM ".$prefix."_bandas where id='$id'");
while ($row = $db->sql_fetchrow($result2)) {
$banda = stripslashes($row['banda']);

$result3 = $db->sql_query("SELECT pic FROM ".$prefix."_bandas where id='$id'");
while ($row = $db->sql_fetchrow($result3)) {
$pic = stripslashes(check_html($row['pic'], "nohtml"));

echo "<img src=\"/images/bandas/$pic\" alt=\"$banda\"><br><strong><a href=\"banda$id.html\">$banda</a></strong>";

}
}
}
?>


There's probably an easier way to do this but well this one works Smile lol thanks for the tips.
 
montego







PostPosted: Sat May 20, 2006 2:30 pm Reply with quote

You should not have to do that! Wish I could see the issue! That is frustrating.

Bang Head Bang Head Bang Head Bang Head Bang Head
 
Dibas







PostPosted: Sat May 20, 2006 2:32 pm Reply with quote

you bet it is lol... i had to make a query for each row... puting banda and pic on the second one wont show me one of them lol... only puting the tree separated works... weird
 
montego







PostPosted: Sat May 20, 2006 2:53 pm Reply with quote

We're missing something "easy" but I have been staring at it too long...
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> How To's

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©