CodeIgniter Forums
Question about selecting from table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Question about selecting from table (/showthread.php?tid=39314)



Question about selecting from table - El Forum - 03-07-2011

[eluser]Davcon[/eluser]
Hi,

I have a question about basic table selection using the Active Record method.

I understand that the method for a basic table select would be:

Code:
$query = $this->db->get('mytable');

foreach ($query->result() as $row)
{
    echo $row->title;
}

However, there's something about this which bothers me and I can't find an answer.

Suppose $title happens to be empty (i.e., NULL) on a particular row. Now, let's say you wanted to replace that nothingness with a hyphon (after all, empty boxes can ruin table cells). How can you possibly do that in an elegant manner using Active Record?

I guess I could have asked the same thing about stripping slashes, string replacing or doing any kind of manipulation of the data that's being selected.

Thanks for your help.


Question about selecting from table - El Forum - 03-07-2011

[eluser]Jaketoolson[/eluser]
Code:
SELECT if(is_null(title), '-', title) as title FROM mytable

just build your query out


Question about selecting from table - El Forum - 03-07-2011

[eluser]Davcon[/eluser]
That's brilliant. Exactly what I needed. Thanks!

Can I just asked where you learned that coding style?


Question about selecting from table - El Forum - 03-07-2011

[eluser]Jaketoolson[/eluser]
I'm not sure what you mean, 'coding style'. Do you mean how did I know how to do that in MySQL?

Books.