Getting database to output array - El Forum - 11-26-2010
[eluser]Unknown[/eluser]
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');
$data = $this->Booksmod->connect();
$this->load->view('booksview', '$data');
}
}
View:
Code: <html>
<head>
<title>Books Data</title>
</head>
<body>
<h1>Heading 1 here</h1>
<ul>
<?php foreach($isbn as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
<?php foreach($price as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
<?php foreach($author as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
<?php foreach($title as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
</ul>
</body>
</html>
Model:
Code: <?php
class Booksmod extends Model
{
function Booksmod()
{
parent::Model();
}
function connect()
{
$query = $this->db->query('SELECT * FROM books');
foreach ($query->result_array() as $row)
{
echo $row['isbn'];
echo $row['price'];
echo $row['author'];
echo $row['title'];
}
}
}
Getting database to output array - El Forum - 11-26-2010
[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
Getting database to output array - El Forum - 11-27-2010
[eluser]Matt S.[/eluser]
You may want to check your database connectivity settings in:
Code: application/config/database.php
By default, there is an array called 'default' that contains the database connection settings.
Code: $db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'somedb';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
To use these setting just call
Code: // notice there is no database specified in the parenthesis
$this->load->database()
In your controller you were explicitly defining 'books' as the settings group you wish to use. So in application/config/database.php, you should have a separate array containing 'books' connection setting, like below:
Code: $db['books']['hostname'] = 'localhost';
$db['books']['username'] = 'root';
$db['books']['password'] = 'root';
$db['books']['database'] = 'books';
$db['books']['dbdriver'] = 'mysql';
$db['books']['dbprefix'] = '';
$db['books']['pconnect'] = TRUE;
$db['books']['db_debug'] = TRUE;
$db['books']['cache_on'] = FALSE;
$db['books']['cachedir'] = '';
$db['books']['char_set'] = 'utf8';
$db['books']['dbcollat'] = 'utf8_general_ci';
$db['books']['swap_pre'] = '';
$db['books']['autoinit'] = TRUE;
$db['books']['stricton'] = FALSE;
|