Welcome Guest, Not a member yet? Register   Sign In
How do i create a loop in a view file or move to model??
#1

[eluser]123wesweat[/eluser]
hi,

i have stuffed my controller like a fat turkey. so it's time for me to get it skinnier

to start i have the following code to create a table which i need to include in a view file.

My questions
1/ should i move this code in a view file, if so how do i loop the query results and how do i call the _seotitle function in the controller?

2/ should i move this code in the jobs model file and also move the _seotitle method to the jobs model?

Code:
//call jobs_model with db
$q = $this->jobs_model->get_jobs_limit($config['per_page'],$numOfJobs);
// generate HTML table from query results  
$table = '<table border="0" cellpadding="0" width="100%" cellspacing="15" id="overviewJobs">';
$table .= '<tr><td>REF.</td><td>TITLE</td><td>DATE</td></tr>';
foreach ($q->result() as $job) {
$table .= '<tr>
            <td nowrap>'. anchor('jobs/details/' .$this->_seotitle($job->title).'.html', 'job - '.$job->ref).'</td>
            <td>'. anchor('jobs/details/' .$this->_seotitle($job->title).'.html', $job->title).'</td>
            <td>'. $job->date.'</td></tr>
';    
}
$table .= '</table>';
//end HTML table
#2

[eluser]ciGR[/eluser]
I think you should maintain the call to the model
Code:
$q = $this->jobs_model->get_jobs_limit($config['per_page'],$numOfJobs);
And move the other code for create the table to a view file.

for seotitle you can create a helper.
#3

[eluser]123wesweat[/eluser]
at the moment i opt to put it in the model file and

Code:
$table = $this->jobs_model->get_jobs_limit_display($config['per_page'],$numOfJobs);

so now i can use $table in another view file.

I didn´t choose for a view file as i don´t know how to loop the $q in a view.
#4

[eluser]ciGR[/eluser]
You may use
Code:
$data['jobs'] = $q->result();
$this->load->view('jobs_view',$data)

and in view

Code:
foreach ($jobs as $job)
{
....
}
#5

[eluser]123wesweat[/eluser]
@ciGR, tx

Will try your suggestion.

In general i thought the view file should hold a minimum of php logic???
#6

[eluser]123wesweat[/eluser]
hmm, view file doesn't show up

Code:
$q = $this->jobs_model->get_jobs_limit($config['per_page'],$numOfJobs);
        $dataForTable['jobs'] = $q->result();
        print_r($dataForTable);
        $table = $this->load->view('jobs/view_overview_table',$dataForTable, true);
        echo 'test: '.$table;
        die();

but the table data as a string doesn't show up??

$dataForTable show an array with the values as expected.
#7

[eluser]ciGR[/eluser]
Don't parse the view to an variable, just call it.
$data['jobs'] = $q->result();
$this->load->view('jobs_view',$data);

with this you don't have a lot php in the view, just show the results, the the role of the view in the MVC
#8

[eluser]123wesweat[/eluser]
@ciGR, i get an error

Code:
Fatal error: Cannot use object of type stdClass as array

$data['jobs'] = $q->result_array();

gives a blank page

Now print_r($jobs);
gives an object array combo like
Code:
Array
(
    [0] => stdClass Object
        (
            [id] => 901503
            etc
        )

    [1] => stdClass Object
        (
[id] => 901504
            etc

How can i fix this??

General question when to use an object vs array???

regards
#9

[eluser]ciGR[/eluser]
Hi,
Quote:Fatal error: Cannot use object of type stdClass as array
you get this error because you have an object and you try to manage like an array, for example

in your view you must have the below code
Code:
foreach($jobs as $job):
   echo $job->job_name;
   //etc
endforeach;

instead of

Code:
foreach($jobs as $job):
   echo $job['job_name'];
   //etc
endforeach;

You can use the second way, if in your model you have $query->result_array(), instead of $query->result().

Hope to help you, if not past some of your code(model,view,controller) and I 'll try tell you exactly where is your error.
#10

[eluser]123wesweat[/eluser]
hi,

tx for taking the time.

well this is what i am working with

model
Code:
function get_jobs_limit($per_page,$numOfJobs) {
         $this->db->select('title');
         $q = $this->db->get('tbl_jobs', $per_page,$numOfJobs);
         return $q;
    }

controller
Code:
//call jobs_model with db
$dataTable['jobs'] = $this->jobs_model->get_jobs_limit($config['per_page'],$numOfJobs)->result_array();
        
        //pre parse the table for use in template
        $data['jobs_table'] = $this->load->view('jobs/overviewA', $dataTable);

view
Code:
foreach($jobs as $job){

$ref = $job->title;//$job['title']; //<-tried both
echo $ref;
}

Does my get_jobs_limit method make any sence??/ As i am using get() and your example is query(




Theme © iAndrew 2016 - Forum software by © MyBB