Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 01-11-2005, 01:12 PM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 136
metazai is on a distinguished road
Stumped about a random number ...

Ok, I've got an online magazine, with little links to the articles. The page I'm having problems with is one that is supposed to show the latest article (by id number and then a random article, choosing from the first article available (id number is 1) to one shy of the latest article. In other words, if the latest article is 16, the random choice should be made between 1 and 15, and it works, except every once in a while I get a broken image and article link, as though it chose no number at all.

Code:
<?php
mysql_connect('sql', 'database', 'password') or die ("Problem connecting to the database");
$query0 = "select id,picon,blurb from features ORDER BY id DESC LIMIT 1"; 
$result0 = mysql_db_query("themid", $query0); 
$row0=mysql_fetch_array($result0);
$nfnumber=$row0[id];
echo ("display $row0 stuff . . .");

$rando=mt_rand(1,($nfnumber--));
$query = "select id,picon,blurb from features where id=$rando"; 
$result = mysql_db_query("themid", $query); 
$row=mysql_fetch_array($result);
echo ("display $row stuff . . ."); ?>
I'm sure the problem is either me not understanding RANDO correctly or my use of my $nfnumber variable. But I'm stumped if I can see what it is . . .
metazai is offline   Reply With Quote
Old 01-11-2005, 02:03 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
well it is possible that you don't have an article for EVERY id in the table, so i'd recommend using the mysql random function in your second query.
PHP Code:
<?
mysql_connect
('sql''database''password') or die ("Problem connecting to the database");

$result mysql_query("select id,picon,blurb from features order by id desc limit 1";

if(
$row mysql_fetch_array($result)){
  
$nfnumber $row['id'];
  
  
// display latest article here
  
  
$result2 mysql_query("select id,picon,blurb from features 
                          where id != "
.$nfnumber." order by rand() limit 1");
                          
  if(
$row2 mysql_fetch_array($result2)){
    
// display random article here
  
}
}

mysql_close();
?>
__________________
Mike
sde is offline   Reply With Quote
Old 01-11-2005, 02:08 PM   #3 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 136
metazai is on a distinguished road
Ohhh . . .

I didn't know you could use RAND() in the ORDER BY . . . that makes things much easier . . . thanks!
metazai is offline   Reply With Quote
Old 01-11-2005, 02:32 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
you don't belong to the o/c php meetup group do you? I've been watching the board the last couple months, but it doesn't seem like they are very active.
__________________
Mike
sde is offline   Reply With Quote
Old 01-11-2005, 02:40 PM   #5 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 136
metazai is on a distinguished road
group

No . . . didn't even know there was such a thing!
metazai is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
random numbers Rotkiv Standard C, C++ 1 07-19-2004 07:26 PM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 09:50 AM
Today's random poll w00t Lounge 6 08-25-2002 01:39 AM


All times are GMT -8. The time now is 09:34 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting