[eluser]vitoco[/eluser]
[quote author="icoder" date="1290839379"]I'm am sure some will figure this out quick as I'm newbie with CI. I need to output from my database what all values I have put into array from my 'books' table. The database is called books as well. The error I receive is You have specified an invalid database connection group. I am sure there are few errors here so please help me out. Thanks!
Controller:
Code:
<?php class Basic extends Controller
{
function Basic()
{
parent::Controller();
}
function fruit()
{
$this->load->database('books'); //
$this->load->model('booksmod');
// CREATE THE DATA ARRAY TO SEND TO THE VIEW
$data = array();
// GET THE BOOKS FROM THE MODEL
$data['books'] = $this->booksmod->connect();
// LOAD THE VIEW
$this->load->view('booksview', $data ); // <----- $data as array, not string
}
}
View:
Code:
<html>
<head>
<title>Books Data</title>
</head>
<body>
<h1>Results : <?=$books['num_books']?> BOOKS</h1>
<ul>
<?php foreach($books['lista'] as $book ):?>
<li>
ISBN : <?=$book['num_books']?> <br>
TITLE : <?=$book['title']?> <br>
PRICE : <?=$book['price']?> <br>
AUTHOR : <?=$book['author']?>
</li>
<?php endforeach;?>
</ul>
</body>
</html>
Model:
Code:
<?php
class Booksmod extends Model
{
function Booksmod()
{
parent::Model();
}
function connect()
{
// IT'S VERY USEFUL TO GET THE QUERY INTO A VARIABLE
// ALSO SPECIFY THE FIELDS INSTEAD IF USE *
$sql = "
SELECT
isbn ,
price ,
author ,
title
FROM
books
";
// EXECUTE THE QUERY
$query = $this->db->query( $sql );
// CREATE AN ARRAY TO RETURN THE BOOKS ( ROWS )
$books = array(
'num_books' => 0 ,
'list' => array() ,
);
foreach ($query->result_array() as $row)
{
// INCREMENT THE COUNTER OF BOOKS
$books['num_books']++ ;
// STORE THE BOOK ( $row )
$books['list'][] = $row ;
}
// CLEAR THE QUERY
$query->free_result();
// RETURN THE LIST OF BOOKS
return $books ;
}
}
[/quote]
Hope it helps you
Saludos
Saludos