[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 03-03-2008, 08:12 AM   #1 (permalink)
Senior Member
 
Join Date: Jul 2005
Location: lebanon
Posts: 2,279
7,614.65 NP$ (Donate)

rcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to behold


MYSQL question showing random row from database

MYSQL question showing random row from database

i am trying to mess around with tags on my site
i would like to show random row content from the available rows in my mysql table
i want to run something like the cloud of tags
how can i do this ?

Regards
rcep is offline  
Old 03-03-2008, 09:18 AM   #2 (permalink)
Danltn.com
 
Daniel's Avatar
 
Join Date: May 2007
Location: Danltn.com / Nottingham, UK
Posts: 1,201
13.51 NP$ (Donate)

Daniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond repute

Ethan Allen Fund Ethan Allen Fund
Either ORDER BY RAND() (Slightly slower)
Or use SELECT COUNT(*) to get the number of rows then use rand($start, $count); to get a random row number, then use select to grab that row or rows.
Daniel is offline  
Old 03-03-2008, 09:39 AM   #3 (permalink)
Senior Member
 
Join Date: Jul 2005
Location: lebanon
Posts: 2,279
7,614.65 NP$ (Donate)

rcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to behold


thx the count(*) was what am searching for
anyway is it a good idea to place like 30 random links to 30 different topic at the bottom of each page ?
rcep is offline  
Old 03-03-2008, 09:52 AM   #4 (permalink)
NamePros Regular
 
monaco's Avatar
 
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 695
314.80 NP$ (Donate)

monaco will become famous soon enough


Quote:
Originally Posted by rcep
thx the count(*) was what am searching for
anyway is it a good idea to place like 30 random links to 30 different topic at the bottom of each page ?
I guess this depends on what your goal is in doing this. Why 30 random topics? Why not 30 topics that are somehow related?
__________________
My Website | My Blog
monaco is offline  
Old 03-03-2008, 09:58 AM   #5 (permalink)
Senior Member
 
Join Date: Jul 2005
Location: lebanon
Posts: 2,279
7,614.65 NP$ (Donate)

rcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to behold


i want it for 2 perposes
1) in a directory i want to give the chance for all pages in directory to get index faster ( because files not getting indexed
2) add random topic in my site


how can i make the related thing is it by % like% ?
rcep is offline  
Old 03-03-2008, 10:38 AM   #6 (permalink)
NamePros Regular
 
monaco's Avatar
 
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 695
314.80 NP$ (Donate)

monaco will become famous soon enough


Quote:
Originally Posted by rcep
i want it for 2 perposes
1) in a directory i want to give the chance for all pages in directory to get index faster ( because files not getting indexed
2) add random topic in my site


how can i make the related thing is it by % like% ?
Related would be tricky...you'd actually need to have something like a keyword-based index to get that done (in the simplest way).

I would do it like this...

* A table containing keywords (A)
* A table containing a list of pages (B)
* A table relating (A,B) with unique constraint (A,B) and frequency of occurrence.

To load the "related" links, do a keyword inventory on the page and take as many keywords as you need to fill 30 spots, and match up highest occurrence in page with occurrence in the relation table above.

Sorry if all that sounded vague, I'm kinda creating the idea in my head as I type, LOL.
__________________
My Website | My Blog
monaco is offline  
Old 03-03-2008, 11:00 AM   #7 (permalink)
Senior Member
 
Join Date: Jul 2005
Location: lebanon
Posts: 2,279
7,614.65 NP$ (Donate)

rcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to beholdrcep is a splendid one to behold


thanks better than nothing :P

please guys more details if possible
rcep is offline  
Old 03-04-2008, 01:40 AM   #8 (permalink)
i love automation
 
xrvel's Avatar
 
Join Date: Nov 2007
Posts: 1,409
987.78 NP$ (Donate)

xrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud ofxrvel has much to be proud of


Hi, Ryan.

Usually, i push available row IDs to an array.
And choose random IDs from the array (which means 30 IDs in your example).
Next, i take the data.

Here's the code
PHP Code:
// Here is our storage array
$ids = array();

// Get available IDs and store it in our array
$q = "SELECT product_id FROM products WHERE product_price > 100 LIMIT 0,100";
$q = mysql_query($q);

while (
$r = mysql_fetch_row($q)) {
   
$ids[] = $r[0];// push new ID
}

// We will store chosen IDs here
$chosen_ids = array();

// Get 30 random rows
$random_n = 30;

while (
count($chosen_ids) < $random_n) {// while we want more random data
   
$index = rand(0, count($ids));// get random index
   
$chosen_ids[] = $ids[$index];// push random element
   
unset($ids[$index]);// remove chosen element
}

// We have chosen ids,
// lets grab the data with single execution

// We will make OR clause such as
//    WHERE id=3 OR id=8 OR id=9
$or_clause = '';
foreach (
$chosen_ids as $id) {
   if (
$or_clause == '') {
      
$or_clause .= ' product_id = ' . $id;
   } else {
      
$or_clause .= ' OR product_id = ' . $id;
   }
}
// OR clause completed

// Let's read the random rows
$q = " SELECT * FROM products WHERE $or_clause ORDER BY product_id ASC";
$q = mysql_query($q);
while (
$r = mysql_fetch_array($q)) {
   
// here's your random data
   
print_r($r);
}
Regards,
Kurniawan.

Last edited by xrvel; 03-04-2008 at 01:48 AM.
xrvel is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 02:52 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85