Bear in mind that ORDER BY RAND() is far from the fastest thing in the
world; there are better ways to accomplish this.
Anyways, to cakeify it:
(assuming Product belongsTo Category and Category hasMany Product)
$this->Category->hasMany['Product']['order'] = 'RAND()';
$this->Category->hasMany['Product']['limit'] = 4;
$results = $this->Category->find('all', array(
'recursive' => 1,
'order' => 'RAND()',
'limit' => 3
));
This should work fine.
hth
grigri
On Oct 20, 12:07 pm, Rahman Mousavian <[EMAIL PROTECTED]> wrote:
> thanks but any better idea !?
>
> On Oct 20, 1:18 pm, "Liebermann, Anja Carolin"
>
> <[EMAIL PROTECTED]> wrote:
> > This should help you:http://de2.php.net/manual/en/function.array-rand.php
>
> > Of course for the first array, you have to get all datasets. For the next
> > selects you can add conditions.
>
> > Hope that helps
>
> > Anja
>
> > -----Ursprüngliche Nachricht-----
> > Von: [email protected] [mailto:[EMAIL PROTECTED] Im Auftrag von
> > Rahman Mousavian
> > Gesendet: Montag, 20. Oktober 2008 11:18
> > An: CakePHP
> > Betreff: Random Select
>
> > Hi all
> > I want to select 3 random category from database then select 4 random
> > product from each category
>
> > This code should have minimum query and request from data base!
> > The database is mySql.
> > any one have any suggestion ?!
>
> > The previous Code is :
>
> > $link = mysql_connect('localhost', 'user', 'password');
> > $Rcats = mysql_query(" SELECT DISTINCT `main_categories_id` AS
> > `categories_id`
> > FROM selected_products
> > WHERE 1
> > ORDER BY RAND(NOW()) LIMIT
> > 3;",$link);
>
> > while ($Rcats_array = mysql_fetch_assoc($Rcats )) {
> > $cats [] = array ('categories_id' =>
> > $Rcats_array['categories_id']); }
>
> > ////////////// and three times repeat this :
>
> > mysql_query("INSERT INTO random_products (`products_id` , `products_image`
> > , `products_price` , `products_name` , `categories_name` , `categories_id`
> > , `categories_rank`)
> > SELECT DISTINCT
> > products.products_id , products.products_image, products.products_price
> > ,products_description.products_name ,
> > categories_description.categories_name,
> > categories_description.categories_id ,
> > categories_description.categories_rank
> > FROM products ,
> > products_description , selected_products , categories_description
> > WHERE
> > products.`products_id` = selected_products.`products_id`
> > AND
> > products.`products_quantity` > 0
> > AND
> > products.`products_status` = '1'
> > AND products.`products_id`
> > = products_description.`products_id`
> > AND
> > categories_description.categories_id = ".$cats [0] ['categories_id']."
> > AND
> > selected_products.`main_categories_id` = ".$cats [0] ['categories_id']."
> > ORDER BY RAND(NOW())
> > LIMIT 4;",$link);
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---