Kod
$this->template->content = '<h1>NEWSY</h1>';
$this->news = new News_Model;
$query = $this->news->get_latest_news();
foreach($query as $item) {
$this->template->content .= new View('news', $item );
}
$this->news = new News_Model;
$query = $this->news->get_latest_news();
foreach($query as $item) {
$this->template->content .= new View('news', $item );
}
W $this->template->content kompletuje sobie zawartość mojej strony którą przekazuje do widoku. Z modelu pobieram tablicę z newsami. Wygląda tak:
Kod
Mysql_Result Object
(
[fetch_type:protected] => mysql_fetch_object
[return_type:protected] => stdClass
[result:protected] => Resource id #54
[insert_id:protected] =>
[sql:protected] => SELECT *
FROM `news`
ORDER BY `id` DESC
LIMIT 0, 10
[current_row:protected] => 0
[total_rows:protected] => 2
)
(
[fetch_type:protected] => mysql_fetch_object
[return_type:protected] => stdClass
[result:protected] => Resource id #54
[insert_id:protected] =>
[sql:protected] => SELECT *
FROM `news`
ORDER BY `id` DESC
LIMIT 0, 10
[current_row:protected] => 0
[total_rows:protected] => 2
)
W $item po each mam takie coś:
Kod
stdClass Object
(
[id] => 3
[title] => asfsdf
[text] => sdfsdf
)
(
[id] => 3
[title] => asfsdf
[text] => sdfsdf
)
Przekazuje to do widoku, żeby dla każdego newsa wygenerować sobie widok, jednak w widoku nie mogę używać zmiennych $id, $title, $text.
Czy ktoś wie co mogę zrobić z tym obiektem $item, żeby stał się tablicą, albo na czym polega błąd?
Rozwiązanie konkursu.
I sposób - mapowanie
Kod
$item2=array();
foreach ($item as $key=>$value) {
$item2[$key]=$value;
}
foreach ($item as $key=>$value) {
$item2[$key]=$value;
}
II sposób - rzutowanie
Kod
$item = (array)$item;
III sposób - <A href="http://docs.kohanaphp.com/libraries/database/result#result_array" target="_blank">http://docs.kohanaphp.com/libraries/databa...lt#result_array
Kod
foreach( $this->query->result_array(FALSE) as $item ) {
$this->template->content .= new View('news', $item );
}
$this->template->content .= new View('news', $item );
}