Welcome Guest, Not a member yet? Register   Sign In
Getting database to output array
#1

[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>
&lt;?php foreach($isbn as $item):?&gt;

<li>&lt;?php echo $item;?&gt;</li>

&lt;?php endforeach;?&gt;
&lt;?php foreach($price as $item):?&gt;

<li>&lt;?php echo $item;?&gt;</li>

&lt;?php endforeach;?&gt;
&lt;?php foreach($author as $item):?&gt;

<li>&lt;?php echo $item;?&gt;</li>

&lt;?php endforeach;?&gt;
&lt;?php foreach($title as $item):?&gt;

<li>&lt;?php echo $item;?&gt;</li>

&lt;?php endforeach;?&gt;
</ul>
    
&lt;/body&gt;
&lt;/html&gt;

Model:
Code:
&lt;?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'];
}
}
}
#2

[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:
&lt;?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:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Books Data&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>Results : &lt;?=$books['num_books']?&gt; BOOKS</h1>
    
  

<ul>
&lt;?php foreach($books['lista'] as $book ):?&gt;

<li>
    ISBN : &lt;?=$book['num_books']?&gt; &lt;br&gt;
    TITLE : &lt;?=$book['title']?&gt; &lt;br&gt;
    PRICE : &lt;?=$book['price']?&gt; &lt;br&gt;
    AUTHOR : &lt;?=$book['author']?&gt;
</li>

&lt;?php endforeach;?&gt;
</ul>
    
&lt;/body&gt;
&lt;/html&gt;

Model:
Code:
&lt;?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
#3

[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;




Theme © iAndrew 2016 - Forum software by © MyBB