|
Author |
Message |
domuk
WebHelper
![WebHelper WebHelper](images/rank_2.gif)
Joined: 15 Jan 2004
Posts: 91
|
Posted:
Tue Aug 17, 2004 4:12 pm (19 years, 10 months ago) |
![Reply with Quote Reply with Quote](templates/4WebHelp2/images/lang_english/icon_quote.gif) ![Back to Top Back to Top](templates/4WebHelp2/images/icon_up.gif) |
Hi all,
I currently have a problem that I hope you may be able to give me some pointers on.
I shall explain what I am trying to do and how I am trying (unsuccessfully) to achieve it.
The data that I want to manipulate is say:
Table "names"
name_id | __ first
(pri key)
-------------------------------
1 _____ | __ dom
2 _____ | __ john
3 _____ | __ jo
5 _____ | __ jason
6 _____ | __ dave
Note: omission of 4
I am currently querying the database to find out how many names are in the database (in this case 5), then I am generating a random integer between 1 and 5 and using that integer $x to query the database a second time:
query2
SELECT first
FROM names
WHERE name_id='$x'
This gives me one result to which I assign to a variable and do stuff with.
The problem that I am having is in the random number generation
Code: | $x = rand(1, $num2); |
($num2 is from the first query and is the number names in the database)
As $num2 is the total number of names I can get $x = 4 (according to the code this is a valid value for $x, but when the second query comes along 4 is not a valid name_id in the table).
I can not control name_id number order as names can be deleted. Therefore
I am trying to find simple (worked) examples of loading data (eg name_id & first) from a mysql database into a PHP array and then randomly selecting the data based on a random key. I think this will give me more flexibility.
Could anyone first confirm that this is the way to go and then help me with some example code (or links to worked examples).
Thanks again,
Dom. |
|
|
![Offline](templates/4WebHelp2/images/offline.gif) ![View User's Profile View User's Profile](templates/4WebHelp2/images/lang_english/icon_profile.gif) ![Find all posts by domuk Find all posts by domuk](templates/4WebHelp2/images/lang_english/icon_search.gif) ![Send Personal Message Send Personal Message](templates/4WebHelp2/images/lang_english/icon_pm.gif) |
![](images/spacer.gif) |
adam
Forum Moderator & Developer
![](images/rank_6.gif)
Joined: 26 Jul 2002
Posts: 704
Location: UK
|
Posted:
Wed Aug 18, 2004 9:54 pm (19 years, 10 months ago) |
![Reply with Quote Reply with Quote](templates/4WebHelp2/images/lang_english/icon_quote.gif) ![Back to Top Back to Top](templates/4WebHelp2/images/icon_up.gif) |
From what you've said, I gather that you just want to select a random row? If so you can do something like:
Code: | SELECT * FROM names ORDER BY RAND() LIMIT 1 |
|
________________________________ It's turtles all the way down... |
|
![Offline](templates/4WebHelp2/images/offline.gif) ![View User's Profile View User's Profile](templates/4WebHelp2/images/lang_english/icon_profile.gif) ![Find all posts by adam Find all posts by adam](templates/4WebHelp2/images/lang_english/icon_search.gif) ![Send Personal Message Send Personal Message](templates/4WebHelp2/images/lang_english/icon_pm.gif) ![Visit Poster's Website Visit Poster's Website](templates/4WebHelp2/images/lang_english/icon_www.gif) |
![](images/spacer.gif) |
domuk
WebHelper
![WebHelper WebHelper](images/rank_2.gif)
Joined: 15 Jan 2004
Posts: 91
|
Posted:
Thu Aug 19, 2004 8:00 am (19 years, 10 months ago) |
![Reply with Quote Reply with Quote](templates/4WebHelp2/images/lang_english/icon_quote.gif) ![Back to Top Back to Top](templates/4WebHelp2/images/icon_up.gif) |
Thanks a lot Adam,
After some thought and writing it down, I was thinking along the same lines.
It also simplifies the problem..
Thanks again
Dom. |
|
|
![Offline](templates/4WebHelp2/images/offline.gif) ![View User's Profile View User's Profile](templates/4WebHelp2/images/lang_english/icon_profile.gif) ![Find all posts by domuk Find all posts by domuk](templates/4WebHelp2/images/lang_english/icon_search.gif) ![Send Personal Message Send Personal Message](templates/4WebHelp2/images/lang_english/icon_pm.gif) |
![](images/spacer.gif) |
|
|
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.
|
Page generation time: 0.129564 seconds :: 18 queries executed :: All Times are GMT
Powered by phpBB 2.0
© 2001, 2002 phpBB Group :: Based on an FI Theme
| |