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.
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.
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.
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".
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.
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.
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.
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.
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.
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.
lol, the days of testing my code before sharing are long, long gone. type fast, post fast, rely on recipient to debug