Help "rand" and "in_array" !!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
agabrean
Forum Newbie
Posts: 3
Joined: Tue May 06, 2008 9:18 am

Help "rand" and "in_array" !!!!

Post by agabrean »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi,

I am trying to get php display randomly 6 products from my data base.

Here is the code I am trying to implement :

Code: Select all

<?php require_once('Connections/imexso.php'); ?>
<?php
$promo_find = $imexso->newFindCommand('WEB - Homepage');
$promo_findCriterions = array('Affaire du jour'=>'oui','Statut'=>'a vendre','ID VN'=>'VO',);
foreach($promo_findCriterions as $key=>$value) {
    $promo_find->AddFindCriterion($key,$value);
}
$result = $promo_find->execute();
$records = $result->getRecords();
$j = ( count( $records ) >=6 )? 6 : count($records);
 
for($i=0; $i<$j ; $i++ ) {
     $k = rand(0,count($records)-1);
     if( in_array($k,$taken) ) $i--;
     else {
          $taken[] = $k;
     }
}
 
foreach($taken as $array_pos) {
     $cur_record = $records[$array_pos];
     $cur_record->getField('MARQUE');
}
 
?>
 
Here is the error I get on my page :
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /data/WebSites/imexso.com/test.php on line 14
Could someone please help me understand this code and get it work?!

Thank you,

Adrian.


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Help "rand" and "in_array" !!!!

Post by pickle »

It doesn't look like $taken is defined anywhere.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
agabrean
Forum Newbie
Posts: 3
Joined: Tue May 06, 2008 9:18 am

Re: Help "rand" and "in_array" !!!!

Post by agabrean »

Yes, I can see that.

But, this is a piece of code that I found on the net. I am trying to make it work. Unfortunatelly, I am new to php and I have a hard time understanding the logic of it and how I could make this work.

If someone here could explain the code to me and give me a hint how to make this work, I would very much appreciate.

I understand this :

1. I run a search on my database, that gets the desired recordset.

2. I use php to randomly select 6 results out of the total of results.

3. The php code apparently gets stuck at the if (in_array... because $taken is not defined previously.

But, why would the guy that created this code put a non defined parameter there?! Did he forget something?!

Can anyone make this thing work?!

Thank you for all the help I could get,

Adrian.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Help "rand" and "in_array" !!!!

Post by pickle »

If you're selecting 6 random rows from a database don't use PHP to do it - use MySQL:

Code: Select all

SELECT
  whatever_is_field_1,
  whatever_is_field_2,
  however_many_fields_you_want
FROM
  whatever_your_table_is
ORDER BY
  RAND()
LIMIT
  6
http://dev.mysql.com/doc/refman/5.0/en/ ... ction_rand
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Help "rand" and "in_array" !!!!

Post by Apollo »

Pickle's suggestion is the best imho, but if you don't have access to the SQL query: insert $taken=array(); before the for-loop (before line 12) to get rid of the error message.

But the randomizing algorithm is quite messy (and theoretically it could even take a very long time to complete).
There are nice default functions in php to randomize arrays or extract random parts from arrays. Functions like array_rand or a combination of shuffle and array_splice pretty much do what you need.
But, why would the guy that created this code put a non defined parameter there?! Did he forget something?!
Maybe the php is included by another php file, where the variable is defined. But then again, finding a piece of random code on the internet hardly implies any quality guarantees :)
agabrean
Forum Newbie
Posts: 3
Joined: Tue May 06, 2008 9:18 am

Re: Help "rand" and "in_array" !!!!

Post by agabrean »

Thank you everybody,

It's working perfectly with $taken=array(); before the for loop.

I could not use the MySQL solution because my data base is FileMaker, not SQL.

It's working nice and very very fast. I am very happy with this solution.

Thank you,

Have a nice day,

Adrian.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Re: Help "rand" and "in_array" !!!!

Post by mchaggis »

Another way would have been to shuffle the array and get the first 6 elements:

Code: Select all

 
<?php
array_shuffle($records);
 
for($x=0; $x < 6; $x++) {
        $records[$x]->getField('MARQUE');
}
?>
 
Post Reply