문제

Background

I'm in the process of moving all my old scripts over to the cakePHP framework. Reason for this is less time spent trying to find and fix bugs and just must better layout and structure. Oh, and speed. But with this comes a huge learning curve. This is my first app with cakePHP, so I'm pretty new (and clueless) with the functions and abilities of the framework.

My problem

In an older script, I go through the alphabet recursively to retrieve names from a database that starts with that specific letter. These results are catagorised according to their starting letter.

To achieve this, my old script looked like this:

$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for($l = 0; $l < strlen($letters); $l++) {
    $names = mysql_query("SELECT name,ext,new FROM admin_extensions WHERE office = '" . $_GET['branch'] . "' AND name LIKE '" . $letters[$l] . "%' ORDER BY name ASC")or die(mysql_error());
    // Do something fancy...
}

I have no idea how to translate this into cakePHP. This script outputted contents to a excel sheet. Here's a small view of what the excel sheet looked like:

Extract of Excel File

Question

My question is simple: How do I achieve the same effect with cakePHP? Since this generates an excel sheet, I'll need to be able to either a) do this in the view, or b) transfer the data to the view file.

Is this possible?

도움이 되었습니까?

해결책

So after some more sweating, I worked it out.

Here's how I did it.

In my controller:

$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for($l = 0; $l < strlen($letters); $l++) {
    $adminExtensions[$letters[$l]] = $this->AdminExtension->find('all',
        array(
            'conditions' => array(
                'AND' => array(
                    'AdminExtension.location_id'=>'3',
                    'HrEmployee.name LIKE '=>$letters[$l].'%'
                )
            )
        )
    );
}
$this->set(compact('adminExtensions'));

And in my view I did this:

$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$k = 1;
for($l = 0; $l < strlen($letters); $l++) {
    $this->PhpExcel->write('B'.($l+$k+1),$letters[$l]);
    foreach ($adminExtensions[$letters[$l]] as $adminExtension) {
        $k++;
        $this->PhpExcel->write('A'.($l+$k+1),$adminExtension['HrEmployee']['name'] . ' ' . $adminExtension['HrEmployee']['surname']);
        $this->PhpExcel->write('B'.($l+$k+1),$adminExtension['AdminExtension']['ext']);
    }
}

It works perfectly! :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top