lmcm2008 Posted August 26, 2012 Share Posted August 26, 2012 Hi: I would like to do something really easy, but I never know how to do it. Please help me. I have a data base where I make the selection in this way: $cadena="SELECT * FROM table WHERE (TYPE='2')"; $result=mysql_query($cadena) or die(mysql_error()); $totalreg=mysql_num_rows($result); Ok, here not problem... I get all the registers with that condition.... Now is the problem... I want to make this... ARRAY [1][iD register 1] ARRAY [2][iD register 2] ARRAY [3][iD register 3] and so on until finish when no more registers whave that condition... for example: ARRAY [66][iD register 66] <--- of $totalreg before. So I have 66 (or XXX ) records selected in the array, and know the Id of every register that I selected before. AND NOW... I want to make a random selection on that array and operate with the register of that selection and make a select to the database on the ID register X... Can you help me?? Thanks a lot. Regards Link to comment https://forums.phpfreaks.com/topic/267594-array-2-dimensions-and-random-selection/ Share on other sites More sharing options...
MMDE Posted August 26, 2012 Share Posted August 26, 2012 Can't you instead just pick a random row from the database with a MySQL query? Just know that ORDER BY RAND() LIMIT 1 can be slow if you got a huge table. Link to comment https://forums.phpfreaks.com/topic/267594-array-2-dimensions-and-random-selection/#findComment-1372559 Share on other sites More sharing options...
Christian F. Posted August 26, 2012 Share Posted August 26, 2012 array_rand ()? Check the PHP manual. Link to comment https://forums.phpfreaks.com/topic/267594-array-2-dimensions-and-random-selection/#findComment-1372562 Share on other sites More sharing options...
lmcm2008 Posted August 27, 2012 Author Share Posted August 27, 2012 Hi: Ok, I made this... $cadena="SELECT * FROM mytable WHERE (TIPO='2')"; $result=mysql_query($cadena) or die(mysql_error()); $totalreg=mysql_num_rows($result); echo "Registros: $totalreg<br>"; // running perfectly the selection... // begin the array $contador=0; while ( $registro=mysql_fetch_array($cadena) ) { $numeros[$contador]=$registro["ID"]; // ID is my field in the database $contador++; } $elementos=sizeof($numeros-1); //debug echo"Elementos: $elementos<br>"; Ok, In the debug line that I make a echo... echo"Elementos: $elementos<br>"; I receive -1 Why??? It seems that the array is looking good.. or not?? How can I be sure that I fill the array correctly and I have all the items that I received from the data base ??? How can I make an echo from all the array to see if is good filled or not?? Please, help... Link to comment https://forums.phpfreaks.com/topic/267594-array-2-dimensions-and-random-selection/#findComment-1372792 Share on other sites More sharing options...
Christian F. Posted August 27, 2012 Share Posted August 27, 2012 If you use print_r () or var_dump () you can see exactly what the array contains, should help you on the way. Link to comment https://forums.phpfreaks.com/topic/267594-array-2-dimensions-and-random-selection/#findComment-1372808 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.