1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Limiting an array to 5 items

Discussion in 'PHP' started by qwikad.com, Sep 26, 2016.

  1. #1
    I want to set sql LIMIT = 50, but then I want to limit the array itself to 5 items (it should always have 5 items or less). I do not want to set LIMIT = 5. How can I do this? I hope it makes sense.



     
    qwikad.com, Sep 26, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Okay, why? I mean, why pull more from the database than you are gonna use? It's not a problem to limit the array, or just use the first 5 entries in the array, but I just feel that you're going about it the wrong way.
     
    PoPSiCLe, Sep 26, 2016 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #3
    I'm guessing in the results processing you'll be moving things about

    something like this should do the trick

    
    $testArray = array();
    $data = getDataFromDatabase(50);
    
    foreach($data as $row){
       if ($row['fieldofinterest'] = 'valueofinterest'){
          $testArray[] = $row;
       }
       if (count($testArray) == 5) {break; }
    }
    
    PHP:
    Sometimes it's just a b**** to write a query the right way and easier just to do some post query processing.
     
    sarahk, Sep 26, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Well, yes, but if it is to sort or pull specific items into the limited array, then it will normally be better to do it in the query. Hence why I'm asking "why".
     
    PoPSiCLe, Sep 26, 2016 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #5
    I worked with a guy early in my career whose mantra was to let the database do as much as possible. He'd be astounded, I'm sure, to know he made an impact.

    I don't speed test much anymore because I've proven time and again that a well-tuned query is always worth the effort of writing it.
     
    sarahk, Sep 26, 2016 IP
  6. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #6
    I can't really explain it, because it probably won't make sense, but to put it plainly if I set LIMIT = 5 but then if the items do not meet certain conditions it may show 4, 3, 2, 1 or even none items. So I want to get 50 (or 30 ) of them and then set the limit to 5 so that the likelihood of 5 of them always showing would increase.
     
    qwikad.com, Sep 26, 2016 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #7
    Sounds like you need to get funky with subqueries and case statements in your field selection and where statements
    However, with your current skill set, that may be a bit much.
    Get the job done, you can always revisit it later.
     
    sarahk, Sep 26, 2016 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    I would just do it like this:
    
    $result = [];
    $get_data = pull_data_from_db(50);
    
    for ($c=0; $c<5;$c++) {
      $result[$c] = $get_data[$c];
    }
    
    PHP:
    However, in the scenario you put forth, what guarantee do you have that there will actually be 5 results, even if you set the limit to 50? I'm still thinking you're overcomplicating something here, but the above will always pull the first 5 results returned from the DB. Means you'll have to do the return as an array, for instance via fetchAll() using PDO.
     
    PoPSiCLe, Sep 26, 2016 IP
  9. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #9
    Had to re-do half of the stuff but finally got it working. You were right PoP it was kinda dumb to do what I was trying to do. The solution is almost always simpler. Thanks everyone.


     
    qwikad.com, Sep 26, 2016 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    So put those conditions in the query too.

    Sarahk's is probably the best approach, but I'd move the count check inside the "if" statement's {} since there's no reason to check the count if you haven't added anything... count can be slow though since PHP arrays aren't real arrays, so you might be better off "wasting a variable" on that. I'd set the variable to five and then:

    if (!(--$count)) break;

    inside the loop.

    
    $results = [];
    $data = getDataFromDatabase(50);
    $max = 5;
    
    foreach($data as $row){
    	if ($row['fieldofinterest'] == 'valueofinterest') {
    		$results[] = $row;
    		if (!(--$max)) break;
    	}
    }
    
    Code (markup):
    Oh, and @sarahk... I think you meant ==, not =. Oops.
     
    deathshadow, Sep 26, 2016 IP
  11. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #11
    lol, the days of testing my code before sharing are long, long gone. type fast, post fast, rely on recipient to debug :)
     
    sarahk, Sep 26, 2016 IP