Welcome Guest, Not a member yet? Register   Sign In
Getting data from table in one line?
#1

[eluser]mitnick[/eluser]
Hey,

I'm wondering, is it possible to fetch content from db->table with an ID in one line ?.

Such as, my table is "my_table", and i'm trying to fetch the "body" content from ID 3.

get this "BODY" from "MY_TABLE" with an id of "ID". Hope you understand my question but i can't find this in the user guide.

I appreciate it!.
#2

[eluser]Flemming[/eluser]
Not entirely sure I understood the question but here's a stab at an answer anyway! ...

Code:
$this->db->select('BODY')->from('MY_TABLE')->where('ID', $ID);
$query = $this->db->get();
$result = $query->row();

// now you can access BODY by ...
$body = $result->BODY;

I wonder if that helps?
#3

[eluser]mitnick[/eluser]
Hi Flemming, Yes that is what i'm trying to do.

But the question is, is this possible in one line from the View ? instead of 4 lines...

Looking forward to your thoughts.
#4

[eluser]Flemming[/eluser]
The way I would do it is write a helper.

Create a new helper in 'application > helpers' called for example, general_helper.php :

Code:
<?php
// general_helper.php
function get_body($id)
{
  // here you need to use the CI super object like so:
  $CI = &get;_instance();

  // now you can access the database via $CI instead of $this, eg.
  $CI->db->select('BODY')->from('MY_TABLE')->where('ID', $id);
  $query = $CI->db->get();
  $result = $query->row();
  $body = $result->BODY;
  return $body
}

Include the new helper in the constructor of your controller:

Code:
function __construct()
{
  $this->load->helper('general_helper');
}

Now in your view you can call the helper function like this:

Code:
<h1>Hello World</h1>
<p>&lt;?php echo get_body($id_you_are_passing_in)?&gt;</p>
#5

[eluser]mattpointblank[/eluser]
Code:
$row = $this->db->get_where('my_table', array('id' => 3))->row_array();
// accessible now through $row['body'];
#6

[eluser]mitnick[/eluser]
Hi Flemming,

That's exactly what i need, however when i add the helper and load it, the Application turns back blank. I reviewed and changed according to my table, but it won't load correctly. Can you something wrong here?.

Looking forward to it..
#7

[eluser]Flemming[/eluser]
ahh glad that's what you needed - pity it didn't work!

some things to check:

1. the helper is stored in the correct location (and uploaded to your server!) 'application > helpers'
2. the function you are calling from your view is typed correctly to correspond to the one in your helper
3. you could leave out the code from the view and see if you still get the blank page (which would indicate a problem with the helper or the controller)
4. you could comment out all the db stuff in the helper and just have it return something like 'hello world' to see if it's a DB problem
#8

[eluser]mattpointblank[/eluser]
When you load your helper, you just need to write $this->load->helper('general'), not $this->load->helper('general_helper').
#9

[eluser]Johan André[/eluser]
Fetching stuff from the model directly from the view may seem tempting but breaks the MVC. It can be done but should not. (my opinion)
#10

[eluser]mitnick[/eluser]
Yes,

I did both without luck. Still turns up blank, works just fine for other models.




Theme © iAndrew 2016 - Forum software by © MyBB