PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old August 22nd, 2002, 09:48 PM
russ russ is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: London, UK
Posts: 129 russ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to russ Send a message via Yahoo to russ
Rand() banner display

I have wrote a little random banner script, for some reason, it doesn't seem to display, there are not errors, and I tried out the sql query in phpmyadmin and it works fine

php Code:
Original - php Code
  1.  
  2. <?php
  3. $db = @mysql_connect("$dbhost", "$dbuname", "$dbpage");
  4.  
  5. if (!$db) {
  6.   echo( "<p>Unable to connect to the " .
  7.         "database server at this time.</p>" );
  8.   exit();}mysql_select_db("$dbname", $db);
  9.  
  10. if (! @mysql_select_db("$dbname") ) {
  11.   echo( "<p>Unable to locate the banner " .
  12.         "database at this time.</p>" );
  13.   exit();}
  14.  
  15.  $result = mysql_query("SELECT id FROM bannerdb ORDER BY rand() LIMIT 1",$db);
  16.  
  17. $myrow = mysql_fetch_array($result);{
  18.  
  19. echo( "<a href=http://" .$myrow['link']. "><img src=/images/bannerpics/" .$myrow['picname']. " width=468 height=60 border=0 alt=" .$myrow['name']. "></a>" );
  20.  
  21. }
  22.  
  23. ?>

Reply With Quote
  #2  
Old August 22nd, 2002, 09:57 PM
russ russ is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: London, UK
Posts: 129 russ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to russ Send a message via Yahoo to russ
RE: Rand() banner display

forget what I have just wrote, should of selected * instead on id

Reply With Quote
  #3  
Old August 22nd, 2002, 09:57 PM
guyer guyer is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Bloomington, IN USA
Posts: 47 guyer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to guyer
RE: Rand() banner display

Just two potential problems... Are you sure that your path to the images is correct? Also, what's up with the curly brackets after the fetch array?

Reply With Quote
  #4  
Old August 22nd, 2002, 09:58 PM
guyer guyer is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Bloomington, IN USA
Posts: 47 guyer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to guyer
RE: Rand() banner display

Oh, yeah. That too.

Reply With Quote
  #5  
Old August 23rd, 2002, 01:29 AM
Kohaar Kohaar is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Denmark
Posts: 147 Kohaar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 37 sec
Reputation Power: 2
Send a message via ICQ to Kohaar
RE: Rand() banner display

I've had some problems with rand().

on some os, the number was displayed as 4343242, and on others 432432e24 (or something). Be aware of this if you need an real number.

I used round($i, 10) to correct this problem. worked fine...

Reply With Quote
  #6  
Old August 23rd, 2002, 06:27 AM
D1NGO D1NGO is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Perth, Australia
Posts: 221 D1NGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Rand() banner display

kohaar, the php rand() function isnt what he was referring to i dont think, as you cant generate a random image through that function...i dont think. if you can i would like it hear it
he was talkin bout the SQL rand() function

Reply With Quote
  #7  
Old August 23rd, 2002, 11:54 AM
Kohaar Kohaar is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Denmark
Posts: 147 Kohaar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 37 sec
Reputation Power: 2
Send a message via ICQ to Kohaar
RE: Rand() banner display

Sorry... I have no idea on how the php db functions work (haven't got around to install sql on my server)

Reply With Quote
  #8  
Old August 23rd, 2002, 07:12 PM
Taoism Taoism is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Winnipeg, MB, Canada
Posts: 81 Taoism User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 2
RE: Rand() banner display

Russ,

I just wanted to add that I think your code was better when you had 'id' instead of * (I know you need more than 'id' for your code to work - I am suggesting you type the column names out for your queries instead of using *).

Cheers,
Keith.

Reply With Quote
  #9  
Old August 23rd, 2002, 08:25 PM
russ russ is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: London, UK
Posts: 129 russ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to russ Send a message via Yahoo to russ
RE: Rand() banner display

so if I has something like this?

SELECT id name link picname FROM bannerdb ORDER BY rand() LIMIT 1

Reply With Quote
  #10  
Old August 23rd, 2002, 08:33 PM
Taoism Taoism is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Winnipeg, MB, Canada
Posts: 81 Taoism User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 2
RE: Rand() banner display

Yeah, but you need commas between the column names...

php Code:
Original - php Code
  1.  
  2. <?php
  3. $SQL ='SELECT id,name,link,picname ';
  4. $SQL.='FROM bannerdb ';
  5. $SQL.='ORDER BY rand() ';
  6. $SQL.='LIMIT 1 ';
  7. //or however you like to write your queries :)
  8. //just make sure the columns have commas between them
  9. ?>



Cheers,
Keith.

Reply With Quote
  #11  
Old August 23rd, 2002, 09:56 PM
rickendall rickendall is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: North Yorkshire, England
Posts: 1 rickendall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to rickendall
RE: Rand() banner display

You also need to seed the random number generator using srand, see

http://www.php.net/manual/en/function.srand.php

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Rand() banner display


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |