Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts

    sql top 25 but only show 8 array_rand?

    Okay, so I want to take the "Top" 25 entries in the database, but I only want it to show 8 at a time on the page. Here is the code im working with, (well a small part)

    PHP Code:
    $SQL $Data->queryCustom("g_featured='1'",25,0);
        
    foreach(
    $SQL as $R

        
    $g_id $R->g_id;
        echo 
    $g_id;

    Now that would "display" 25 g_id on the page, but I want it to only show 8 on "rand", how to place the array_rand, in this statement?

    Thanks

  • #2
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Was thinking, I could possible build something like this,

    PHP Code:

    $aRand 
    array_rand$SQL 5);
    for(
    $i 0$i 5$i ++)

     
    $R $SQL[$aRand[$i]]; 

  • #3
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    I know I could do something like

    but cant figure out what im doing wrong there.. not used to the array.
    PHP Code:

    $aRand 
    array_rand$SQL 5);
         
     foreach(
    $aRand as $key)
     { 
      echo 
    $SQL[$key]['g_id'];
     }

    // probably this instead 

    $R $SQL[$key]; 
    Last edited by sitNsmile; 06-15-2010 at 07:07 PM.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $SQL $Data->queryCustom"g_featured='1'"25);

    // Shuffle the order of $SQL
    shuffle$SQL );

    // Reduce $SQL to first 8 items
    $SQL array_slice$SQL0);

    // Display the 8 random items
    foreach( $SQL as $_SQL )
    {
        
    print_r$_SQL );



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •