login  Naam:   Wachtwoord: 
Registreer je!
 Forum

Random mysql selecteren

Offline robmoorman - 21/09/2004 16:24
Avatar van robmoormanPHP interesse ik wil nu een random items selecteren uit mijn database tabel Fotos...

hoe zet ik dat nu om in één query... dus niet met zon verneutke array opzetten en dan random, is te omslachtig

2 antwoorden

Gesponsorde links
Offline DRUNK - 21/09/2004 16:35
Avatar van DRUNK PHP gevorderde ik gebruik deze code om een random plaatje te selecteren uit mijn DB, mischien heb je hier wat aan:
  1. <?php
  2.  
  3. function get_rand_pic($cat, $size, $tmp_dir, $type=0) {
  4. global $conf;
  5.  
  6. $max_img_id = mysql_result(
  7. "SELECT MAX(img_id) FROM images
  8. WHERE img_cat = '".$cat."'"
  9. ), 0
  10. );
  11. $img_result = false;
  12. $img_dir = $conf['doc_root'] . 'images/';
  13. $th_dir = $conf['doc_root'] . 'images/th/';
  14.  
  15. while ($img_result === false) {
  16. if ($max_img_id == 0) {
  17. $src_img = imagecreatefromjpeg($img_dir . 'fusionmedia.jpg');
  18. $tgt_img = imagecreate($size[0], $size[1]);
  19. $tmp_file = $tmp_dir . 'tmp_'.rand_str(10).'.jpg';
  20. $img_rnd_x = rand(0, $size[0]);
  21. $img_rnd_y = rand(0, $size[1]);
  22.  
  23. $copy = imagecopyresized($tgt_img, $src_img, 0, 0, $img_rnd_x, $img_rnd_y, $size[0], $size[1], $size[0], $size[1]);
  24. $new_img = imagejpeg($tgt_img, $tmp_file, 80);
  25.  
  26. if (!$copy || !$new_img) {
  27. die('falied to create temp image');
  28. }
  29. imagedestroy($src_img);
  30. imagedestroy($tgt_img);
  31.  
  32. $img_result = true;
  33. return $tmp_file;
  34. }
  35. else {
  36. $rand_img_id = rand(1, $max_img_id);
  37. $img_query = mysql_query(
  38. "SELECT img_file FROM images
  39. WHERE img_cat = '".$cat."'
  40. AND img_id = '".$rand_img_id."'"
  41. ) or die(mysql_error());
  42. $img_result = (mysql_num_rows($img_query) == 1) ? true : false;
  43. }
  44. }
  45. if ($type == 0) {
  46. $img_data = mysql_fetch_array($img_query);
  47.  
  48. $src_img = imagecreatefromjpeg($img_dir . $img_data['img_file']);
  49. $tgt_img = imagecreate($size[0], $size[1]);
  50. $tmp_file = './tmp/tmp_'.rand_str(10).'.jpg';
  51. $img_rnd_x = rand(0, $size[0]);
  52. $img_rnd_y = rand(0, $size[1]);
  53.  
  54. $copy = imagecopyresized($tgt_img, $src_img, 0, 0, $img_rnd_x, $img_rnd_y, $size[0], $size[1], $size[0], $size[1]);
  55. $new_img = imagejpeg($tgt_img, $tmp_file, 80);
  56.  
  57. if (!$copy || !$new_img) {
  58. die('falied to create temp image');
  59. }
  60. imagedestroy($src_img);
  61. imagedestroy($tgt_img);
  62.  
  63. return $tmp_file;
  64. }
  65. else {
  66. $img_data = mysql_fetch_array($img_query);
  67. return $th_dir . $img_data['img_file'];
  68. }
  69. }
  70. ?>
Offline Thomas - 21/09/2004 16:37
Avatar van Thomas Moderator Hij vond een array al omslachtig 

lang leve de ZOEKFUNCTIE:
http://www.site...262#id2583
Gesponsorde links
Dit onderwerp is gesloten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.171s