Welcome Guest, Not a member yet? Register   Sign In
How to format template?
#1

(This post was last modified: 04-24-2017, 09:50 AM by PolkaDodge.)

Hi im new to codeigniter and im just wondering on how to format the data from a template.

I can format data coming from a row_array result since i dont need to loop it but how can i format data coming from a result_array()??


If i use foreach in controller to format the data i cant loop them in views.

Controller:
Code:
public function loadClients(){
    $clientsTotal = $this->client_list_model->getClientTotal();
    $activeCount = $this->client_list_model->activeCount();
    $inactiveCount = $this->client_list_model->inactiveCount();
    $archiveCount = $this->client_list_model->archiveCount();

    $data = array(
        'project_total' => $clientsTotal['projects_total'],
        'pending_total' => $clientsTotal['pending_total'],
        'on_going_total' => $clientsTotal['on_going_total'],
        'completed_total' => $clientsTotal['completed_total'],
        'cancelled_total' => $clientsTotal['cancelled_total'],
        'total_price' => number_format($clientsTotal['total'],2),
        'paid_total' => number_format($clientsTotal['paid_total'], 2),
        'balance_total' => number_format($clientsTotal['balance_total'],2),
        'held_price_total' => number_format($clientsTotal['held_price_total'],2),

        'client_total' => $clientsTotal['client_total'],
        'active_count' => $activeCount['active'],
        'inactive_count' => $inactiveCount['inactive'],
        'archive_count' => $archiveCount['archive']
    );

    $data['clients'] = $this->client_list_model->getClients();// I would like to format these results
    $data['title'] = 'Client List';

    $this->load->view('templates/header');
    $this->load->view('pages/activities');
    $this->parser->parse('pages/client_list', $data);
    $this->load->view('templates/footer');
}

Model:
Code:
function getClients(){
        $this->db->select("c.client_id, c.company_name, count(p.project_id) AS projects,
            count(CASE WHEN p.project_status = 'Pending' THEN p.project_status END) AS pending,
            count(CASE WHEN p.project_status = 'On Going' THEN p.project_status END) AS on_going,
            count(CASE WHEN p.project_status = 'Completed' THEN p.project_status END) AS completed,
            count(CASE WHEN p.project_status = 'Cancelled'THEN p.project_status END) AS cancelled,  
            sum(p.total_price) AS total_price, sum(p.paid) AS paid, sum(p.balance) AS balance,
            sum(p.held_price) AS held_price, c.client_status");
    $this->db->from('clients c');
    $this->db->join('projects p', 'c.client_id = p.client_id', 'LEFT');
    $this->db->group_by('c.client_id');
    $clients = $this->db->get();
    return $clients->result_array();
}


View:
Code:
{clients}
    <tr>
            <td class="left">{client_id}</td>
            <td class="left">{company_name}</td>
            <td>{projects}</td>
        <td>{pending}</td>
        <td>{on_going}</td>
        <td>{completed}</td>
        <td>{cancelled}</td>
        <td>{total_price}</td>
        <td>{paid}</td>
        <td>{balance}</td>
        <td>{held_price}</td>
        <td>
                <span>{client_status}</span>
            <a href="client_profile/{client_id}" class="view button">View</a>
        </td>
    </tr>
{/clients}

I would like to be able to number format or date format the result coming from the model in controllers which is
Code:
$data['clients'] = $this->client_list_model->getClients();
Reply
#2

I use blade templating engine in CI, its easier.

In your case using parser you can use php tags.
God Bless CI Contributors Smile
Reply
#3

(04-24-2017, 02:44 PM)marksman Wrote: I use blade templating engine in CI, its easier.

In your case using parser you can use php tags.

What do you mean by i can use php tags? i tried doing this in views
But this only gives me an error

An uncaught Exception was encountered
Type: ParseError
Message: syntax error, unexpected '{'


Code:
<td><?php number_format({total_price}); ?></td>


And also something like this in controller but this returns only 1 row and i cant get all the

data since its not looping

Code:
$clients = $this->client_list_model->getClients();

foreach($clients AS $clientValue){
    $data['total_price'] = number_format($clientValue['total_price'],2);
}
Reply
#4

(This post was last modified: 04-24-2017, 10:56 PM by PolkaDodge.)

I also would like to add if statements based on some of the data but i dont know how to do it in a template
Ex:

Controller:

$data['clients'] = $this->client_list_model->getClients();

if($data['client_status'] == 'Active'){ //I want to get this and add if statemens, its a data from $data['clients']
$data['filter'] = '<span class="active_filter">'
}
else if($data['client_status' == 'Inactive'){
$data['filter'] = '<span class="inactive_filter">'
}

View:
{filter}{client_status}</span>
Reply
#5

If you use php tags you dont need curly braces anymore. . from {var} to $var
God Bless CI Contributors Smile
Reply
#6

Cant i use only the logics in the controller only? cause i want the view for displaying only as much as possible with no php tags or i have no choice
Reply
#7

I knew you would say that, thats why at first ived recommend blade templating on top of CI. you can do code like

Code:
<ul>
<!-- foreach statement -->
@foreach ($datas as $data)
    <li>{{ $data['key'] }}</li>
@endforeach
</ul>

<!-- if statement -->
@if (true)
    <p>looks nice?</p>
@endif

you can benefit more of it's features like extending, yielding, etc. that would make your front looks good Smile

I'm currently using it on CI 3.1.4
God Bless CI Contributors Smile
Reply
#8

(This post was last modified: 04-28-2017, 03:52 AM by InsiteFX. Edit Reason: fixed spelling error )

You can create a method in your controller to create the html output loop through it
all assigning it to a variable then assign that variable to your $data['yourName'];
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9
Lightbulb 

There is a library designed for CodeIgniter to simulate Laravel's Blade.

https://github.com/GustMartins/Slice-Library

Cool
Reply




Theme © iAndrew 2016 - Forum software by © MyBB