Welcome Guest, Not a member yet? Register   Sign In
Change data retrieved from model in controller
#11

[eluser]pyrokinesis[/eluser]
Hi kgill,

There are no spaces in the table names or any spaces in any of the columns in the table.
They tried to get in, and were willing to pay extra but I didn't let them in.

Code:
function get_data() {
    $query =
            "SELECT
                staff_id AS 'Staff ID',
                name AS 'Name',
                email_address AS 'Email Address',
            FROM staff
            ";
    return $this->db->query($query);
}

When the query is returned the stdobject uses the column aliases.
#12

[eluser]jedd[/eluser]
Yeah, I noticed you were casting the variable names to contain spaces on the way out of the SELECT .. but my reaction is similar. I think you're buying yourself a whole heap of forward trouble by having two different names for your columns - the actual column names, and the things that you call them in certain parts of your code.

I'd humbly suggest that you a) come up with names for your columns that you're happy to use everywhere, or at the very least b) don't cast variable names to contain spaces. Ever.

Note that I don't think the world will end, but I do think (as above) that you'll just cause yourself more grief in the future by doing this, and the types of problems you'll find will be weird-arse stuff that is really painful to try to nail down. Much like the weird-arse problem you're having now, actually.
#13

[eluser]pyrokinesis[/eluser]
I have to have the table column headings in human friendly form and most humans I know prefer spaces to underscores, hence the aliases.
I don't want to scare them! :lol:

I've been trying to change the value (str_replace) of any value from data retrieved from a model have come up with nada.

Is it a weird problem I'm having or is it not possible to change the values retrieved from a model in the controller?
#14

[eluser]pyrokinesis[/eluser]
Ok, I've made a bit of progress and managed to replace values in the data returned from the model:

Code:
<?
function get_data_new() {
    $this->load->model('test_model');
    $result = $this->test_model->get_data();        
    
    //foreach ($result->result() as $row) {
    foreach ($result->result_array() as $row) {
        //$row->email_address = str_replace('gmail', 'yahoo', $row->email_address);
        $row['Email Address'] = str_replace('gmail', 'yahoo', $row['Email Address']);
        //print_r($row);
        $results[] = $row;
    }
    
    echo $this->table->generate($results);
}
?>

So, it can be done with arrays but I've had no luck with replacing data in objects ($result->result()).

I modified my select statement to not use column aliases and it didn't make any difference.
I know that spaces should not be used in column names but is the general opinion that spaces should not be used in column aliases as well?

Code:
SELECT first_name AS 'First Name'

I don't see what difference it makes if the column names themselves have no spaces...

If anyone has any ideas how to change the values of data in a controller when using the $data->result() then please post it. Someone is bound to have run into this before.

Thanks for the help guys
#15

[eluser]jedd[/eluser]
Quote:So, it can be done with arrays but I've had no luck with replacing data in objects ($result->result()).

I'm old school - I use arrays everywhere. There's not many compelling reasons to use objects in this context, especially for return values that are consistently array-like. The fact that a lot of PHP functions work better (or at the least more predictably for slow people like myself) for arrays is reason enough for me.

I'd also point out that the CI table class is eschewed by many people around here (including, quite recently, me) - primarily because it breaks the presentation separation that you expect with controllers .v. views. Plus it's pretty sucky, and not just because people use tables where they should use something else (divs, say) but because you're going to get much better results, for very little effort, by creating your own view snippet that you send tabulatable data to instead.

Certainly worth considering trying to dump the table class, anyway. In this case it'd make it (even) easier to manage your column headers, using either an array you define in your config, or perhaps in your MY_Controller extension, that lets you alias your headings. Eg.
Code:
$table_headings = array (
              "staff_id" => "Staff ID",
              "name" => "Name",
              "email_address" => "Email Address",
              );

And in your view, just have your headings call something like:
Code:
$table_headings['staff_id']

Note that doing this also keeps separation between your model and your presentation - because at the moment if you want to change a presented column heading in your HTML page, you have to modify your SELECT statement. Tsk tsk (would be the appropriate response to this situation).

Quote:I know that spaces should not be used in column names but is the general opinion that spaces should not be used in column aliases as well?
That's my opinion, at least. Dunno about the General's.

Quote:If anyone has any ideas how to change the values of data in a controller when using the $data->result() then please post it. Someone is bound to have run into this before.
To be honest I'm now confused which problem we're looking at Wink Does any of the above negate this problem, or at least get you in the right direction?




Theme © iAndrew 2016 - Forum software by © MyBB