Asked  9 Months ago    Answers:  5   Viewed   52 times

i have written this code

  <?php
        $a = Meme::all();
        $b = count($a);
        for($i=$b;$i<$b-3;$i--)  {
    ?>      <div class="col-sm-6"><img class="lazy" data-src="<?php echo $a[$i]->path; ?>" /> </div>
    <?php
    }?> 

i want to output the last 3 memes(images) but this for loop doesn't work. So i found something that is called array_slice and i wanted to use it but everyone was using print_r but in this case i don't need it. So any suggestions?

 Answers

47

Store those 3 elements of array $a in a new array $b by using array_slice:

<?php    
    $a = Meme::all();
    $b = array_slice($a, 3);

    for ($i = 0; $i < 3; $i++){
?>
    <div class="col-sm-6"><img class="lazy" data-src="<?php echo $b[$i]->path; ?>" /> </div>
<?php
    }
?>
Wednesday, March 31, 2021
 
phirschybar
answered 9 Months ago
76

PHPUnit uses an error handler function to trap and display errors, but from the PHP manual on error handlers,

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

If you are running tests in a separate process, PHPUnit will get the error and message from the interpreter, but there will be no stack trace available. This is simply a limitation of the PHP interpreter. Fatal means fatal.

Wednesday, March 31, 2021
 
altermativ
answered 9 Months ago
39

When you set cell values individually, you have the option of setting the datatype explicitly, but when you use the fromArray() method, you don't have this option.

However, by default, PHP uses a default value binder to identify datatypes from the values passed, and set the cell datatype accordingly. This default behaviour is defined in a class /PHPExcel/Cell/DefaultValueBinder.php.

So you can create your own value binder, as described in the PHPExcel Documentation, that would set every value as a string datatype.

Something like:

class PHPExcel_Cell_MyColumnValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
{
    protected $stringColumns = [];

    public function __construct(array $stringColumnList = []) {
        // Accept a list of columns that will always be set as strings
        $this->stringColumns = $stringColumnList;
    }

    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        // If the cell is one of our columns to set as a string...
        if (in_array($cell->getColumn(), $this->stringColumns)) {
            // ... then we cast it to a string and explicitly set it as a string
            $cell->setValueExplicit((string) $value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        }
        // Otherwise, use the default behaviour
        return parent::bindValue($cell, $value);
    }
}

// Instantiate our custom binder, with a list of columns, and tell PHPExcel to use it
PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyColumnValueBinder(['A', 'B', 'C', 'E', 'F']));

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->fromArray($dataArray,null,"A2");
Friday, May 28, 2021
 
Wilk
answered 7 Months ago
26

Like Michael Berkowski suggested:

$groups = array_chunk($menu,4);

Should give you what you need. If you're more into "manual labour":

$groups = array();
while($groups[] = array_splice($menu,0,4))
{//no need for any code here ^^ chunks the array just fine
    printf('This loop will run another %d times<br/>',(int)ceil(count($menu)/4));
}

Update:

I see I got this a bit wrong... want to chunk into 4 arrays, not into arrays of four:

$groups = array_chunk($menu,(int)ceil(count($menu)/4));
Saturday, May 29, 2021
 
Terry
answered 7 Months ago
73

If this code is running, you should be seeing a ton of debug output, even if it is working correctly. You don't actually say what the problem is, but you're doing a few things wrong that I can see. It would really help if you based your code on the examples provided and read the docs instead of just guessing.

$mail->SMTPSecure = 'TLS';

should be:

$mail->SMTPSecure = 'tls';

Don't call smtpConnect() yourself, you'll mess up the tracking of SMTP transaction state. If you want to set SSL params, set them the expected way and then just call send(), which will deal with the connection:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

The next question is why are you doing that? If you can't provide an explicit, specific reason for doing that, you're doing something wrong.

Saturday, May 29, 2021
 
waylaidwanderer
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share