Re: array_slice() - sometimes has errors when total records is not even amount
| From: | Bastien Koert | Date: | Sat, 25 Apr 2015 00:43:36 +0000 |
| Subject: | Re: array_slice() - sometimes has errors when total records is not even amount | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-324824@lists.php.net to get a copy of this message | ||
The easiest approach is to do a modulus check on the size of the result and
figure out if you need a blank cell at the end
$finish = '';
if($allrows %2 !=0){ $finish = "<div id='empty'> </div>";
}
//echo out divs
echo $finish;
On Wed, Apr 22, 2015 at 8:01 PM dealTek <dealtek@gmail.com> wrote:
> Hi all,
>
> NEWBIE: Here's the issue - I am trying to split and find query on a page
> into 2 columns using = array_slice().......
>
> If the get found set count is an EVEN amount - then all is ok - but if not
> then I get an error and the last record on the bottom right like.....
>
> Fatal error: Call to a member function getField() on a non-object in
> /home/test.php on line xxx
>
> I think it has to do with LENGTH param...
>
> - if I make length be $part1 -0 then I get error on bottom right if NOT
> even total
> - if I make length be $part1 -1 then I get NO error on bottom right if NOT
> even total - BUT I DO NOT GET FULL RESULT (MISSING A RECORD)...
>
>
> Q: HOW CAN I SET THIS UP SO NO ERRORS WHEN RESULT OF found set count is
> *NOT* an EVEN amount ?
>
>
>
>
>
> <?php
> $allrows = $mytest_result->getFoundSetCount();
> $part1 = round($mytest_result->getFoundSetCount() / 2);
> ?>
>
>
>
>
> <br />
> all <?php
> echo $allrows; ?><br />
> part1 <?php
> echo $part1; ?><br />
> <br />
>
>
>
>
> <?php
>
>
> // ERROR unless use -1 - check if correct ??? now ok i guess
>
> $firstNameSet = array_slice($mytest_result->getRecords(), 0, $part1 -0 );
> // ERROR unless use -1 - check if correct ??? now ok i guess
>
> $secondNameSet = array_slice($mytest_result->getRecords(), $part1 -0 ); //
> ERROR unless use -1 - check if correct ??? now ok i guess
>
> ?>
>
>
>
> <?php
>
> foreach ($firstNameSet as $key => $nameA) {
> $nameB = $secondNameSet[$key];
> ?>
>
>
> <?php echo $nameA->getField('myfield'); ?>
>
> <?php
> } ?>
>
>
>
>
>
> <?php
>
> foreach ($firstNameSet as $key => $nameA) {
> $nameB = $secondNameSet[$key];
> ?>
>
>
> <?php echo $nameA->getField('myfield'); ?>
>
> divs here etc......
>
> <?php echo $nameB->getField('myfield'); ?>
>
>
>
> <?php
> } ?>
>
> --
> Thanks,
> Dave - DealTek
> dealtek@gmail.com
> [db-15]
>
>