Welcome Guest, Not a member yet? Register   Sign In
Is there another way to get sql data in view?
#1

[eluser]phantom-a[/eluser]
I'm using
<?php foreach($query->result() as $row): ?>

statements to get the $data passed from the controller
I've tried just putting something like

<?$query->$sql_field;?> but this does not work.
Is there another way with less php code to put?

In my controller I'm getting 1 row from mysql with a "WHERE something="
So I think foreach loop is kind of too much to type just get a 1 field from that that row.
Code:
<?php foreach($query->result() as $row): ?>
<?php endforeach; ?>
#2

[eluser]Sarfaraz Momin[/eluser]
if you want to get only one row you can try using something like this
Code:
$query = $this->db->query('select * from tablename');
return $query->row();
This would just pull one row. Also the query used is just an example and you can use Active Record here without issues. This way it would not give u a multidimensional array instead just a single dimensional array for one row.

Have a good day !!!
#3

[eluser]Mirage[/eluser]
If all you need is to pull one row from the query you don't need a foreach loop:

Code:
$row = $query->row(0);
echo $row->field;

Personally, I'm also not big on passing the query object to the view, but rather the result - but it's certainly possible to do so.

Cheers -
m
#4

[eluser]Colin Williams[/eluser]
Quote:Personally, I’m also not big on passing the query object to the view, but rather the result - but it’s certainly possible to do so.

I always go back and forth. I think I'm going to start sending both Smile That way any front-end guy can be dumb and just use simple result sets, or if they need to get fancy they have access to the result object.
#5

[eluser]Mirage[/eluser]
Quote:I think I’m going to start sending both Smile

:lol:

It's an efficiency thing. If the row method would actually pull the rows from the query result on the fly it would make some sense to work with the query object in the view. But from what I remember after studying the db driver is that the first call to row() will actually pull and cache the entire result() anyway. So there's hardly a point in passing the query object.

Also it opens up the possibility to manipulate the result() array before it makes it to the view.

Cheers -
m
#6

[eluser]Christopher Blankenship[/eluser]
Hey here are 2 more cents. hehe

Controller:
Code:
function index()
{
   $query = $this->db->query("SELECT * FROM tablename LIMIT 1");
      // or $query = $this->db->query("SELECT * FROM tablename WHERE field = searching");
   return $query->result();
}

Normally, though I like to put my database calls into Models for loading and calling a particular function such as getItemByID($var) and return the data. This will cut down on code congestion and having to change it in multiple places. For that here is a quick example.

Controller:
Code:
function index()
{
   $this->load->model('Items', 'items');

   $id = $this->uri->segment(3,0);
   $data = array(
         'proj' => $this->item->getItemByID($id)
         );

   $this->load->view('page', $data);
}

Model:
Code:
function getItemByID($id)
{
   $query = $this->db->query("SELECT * FROM tablename WHERE field = $id");
   // or $query = $this->db->query("SELECT * FROM tablename LIMIT 1"); without the variable
   return $query->result();
}

Have fun.




Theme © iAndrew 2016 - Forum software by © MyBB