Welcome Guest, Not a member yet? Register   Sign In
How display $var in view?
#1

[eluser]piker[/eluser]
Simple problem but

How to display, a variables in views
Query works fine in phpmyadmin

Table name = historia
Column name = data

What result of query below?
It's a number or table cell?

In controller I have:
Code:
$data['query2'] = $this->db->query('SELECT max(data) FROM historia AS sss');
$this->load->view('terminale_view', $data);
In views I have:
it don't work Sad
Code:
<?php foreach($query2->result() as $row): ?>
<?=$row->sss;?>
<?php endforeach; ?>
<?=$row->data;?>
Dont work too.
I have error message:
Severity: Notice
Message: Undefined property: stdClass::$sss

Don't work too
Code:
<?=$query2?>
I have error message:
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
#2

[eluser]xwero[/eluser]
should
Code:
$data['query2'] = $this->db->query('SELECT max(data) FROM historia AS sss');
not be
Code:
$data['query2'] = $this->db->query('SELECT max(data) AS sss FROM historia');
#3

[eluser]ELRafael[/eluser]
Hey Piker....

Try xwero suggestion. 99.9% to work.

Adesso one more thing. You'll pass only one record to view? If yes, i'm using another way.

In the controller file
Code:
$query2 = $this->db->(.....
$row2= $query->row();
$data['sss'] = $row2;

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

View file
Code:
<?=$sss;?>

In this case, i put the php logic inside the controller, not in view. To me, view is just to view Confusedhut: Only in loop cases that i php logic in the view.

And other thing. If there is only one record, why put that inside a FOREACH loop?

[]
#4

[eluser]Michael Wales[/eluser]
He had it in the foreach loop because he was using the result() method, which places one layer of objects between the parent object and the object you are trying to retrieve (it's an auto-incrementing integer).

But yeah, take Rafael's suggestion. Do everything in the controller (or even a model), pass the row() method to the view, and echo from there.
#5

[eluser]piker[/eluser]
ELRafael
I think should be like this:

Code:
$query2 = $this->db->query('SELECT data FROM historia LIMIT 1');
$row2= $query2->row();
$data['sss'] = $row2;

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

but this don't work :-(

Quote:Severity: 4096

Message: Object of class stdClass could not be converted to string
#6

[eluser]gtech[/eluser]
Code:
$query2 = $this->db->query('SELECT data FROM historia LIMIT 1');
$row2= $query2->row();
$data['sss'] = $row2->data;

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

remember print_r and echo are your friends when debugging (as long as you take them out)

also this would work

Code:
$query2 = $this->db->query('SELECT data FROM historia LIMIT 1');
$row2= $query2->row_array();
$data['sss'] = $row2['data'];

$this->load->view('.....', $data);
#7

[eluser]ELRafael[/eluser]
is there records in database?

gtech solution is almost equal mine. both should work.
#8

[eluser]gtech[/eluser]
[quote author="ELRafael" date="1196353544"]is there records in database?

gtech solution is almost equal mine. both should work.[/quote]

Hi ElRafael, my solution was just a fix to your solution Smile as I think you had a typo:

$data['sss'] = $row2;

should of been

$data['sss'] = $row2->data;

as you were passing in the database object to the view as opposed to the value of the database field 'data'. Thats why when $sss was outputted in the view, the error message "Message: Object of class stdClass could not be converted to string" occurred.

alternatively the view variable needs changing ($sss->data).
#9

[eluser]piker[/eluser]
All previus conception are good but only one row
can be viewed.

I found solution in the Internet.
Let in the model be somthing like this:
Code:
function get_latest_news()
    {
    $this->db->orderby("id", "desc");
    return $this->db->get('newsy', 10);
    }

Let in controller be something like this:
Code:
function index {
    $this->load->model('News');
    $query = $this->News->get_latest_news();
    if ($query->num_rows() > 0)
         {
        $this->load->view('......', array('result' => $query->result()));
        }

Let in views be something like this:
Code:
<html>
<?PHP
foreach($result as $val)
    {
echo $val->news_title;
echo $val->news_text;
    }
?>
</html


What are you thing about this conception?
It's possible don't use foreach loop in views?

My answer is NO.
Clear separation MVC is not possible.




Theme © iAndrew 2016 - Forum software by © MyBB