Welcome Guest, Not a member yet? Register   Sign In
Problem listing DB contents
#1

[eluser]clintonbeattie[/eluser]
Hi,

I have some code but it only lists the first item in a database table and not all of them.
I have used print_r($data), as you'll see below in the model, but just get "Array ( [id] => 1 [cat_name] => Search Engine Optimisation )"

Any help greatly appreciated!

CONTROLLER
Code:
function getCategoriesNav(){
            $data = array();
            $this->db->select('id,cat_name');
            $this->db->from('category');        
            $Q = $this->db->get();
            if($Q->num_rows() > 0) {
            $data = $Q->row_array();
            print_r($data);
            }        
            $Q->free_result();
            return $data;
        }

VIEW
Code:
if (count($category_navigation)){
  echo "<ul>";
  foreach ($category_navigation as $id => $cat_name){
    echo "<li>";
    echo anchor("welcome/category/$id",$cat_name);
    echo "</li>";
  }
  echo "</ul>";
}

MODEL
Code:
function getCategoriesNav(){
            $data = array();
            $this->db->select('id,cat_name');
            $this->db->from('category');        
            $Q = $this->db->get();
            if($Q->num_rows() > 0) {
            $data = $Q->row_array();
            print_r($data);
            }        
            $Q->free_result();
            return $data;
        }
#2

[eluser]Dam1an[/eluser]
It's because you're using row_array, which is meant to only return a single item, you want result_array (or result if you want the object)

The in the foreach loop in the view, do something like
Code:
echo "<ul>";
foreach ($category_navigation as $category){
    echo "<li>";
    echo anchor("welcome/category/$category['id']",$category['cat_name']);
    echo "</li>";
}
echo "</ul>";

It's not tested, but you get the idea
#3

[eluser]clintonbeattie[/eluser]
Thanks.

I used result array, fine, but the text the list now says "Array" for each item in the database.

Any thoughts?
#4

[eluser]clintonbeattie[/eluser]
This code doesn't display anything which is odd...

Code:
echo "<ul>";
foreach ($category_navigation as $category){
    echo "<li>";
    echo anchor("welcome/category/$category['id']",$category['cat_name']);
    echo "</li>";
}
echo "</ul>";
#5

[eluser]Dam1an[/eluser]
Does it display any errors? What do you get when you do a var_dump/print_r on $category_navigation

And if the view doesn't display anything, what where you refering to when you said it says array for everything? I'm confused now :-S
#6

[eluser]clintonbeattie[/eluser]
I get this when I print_r...

Code:
Array ( [0] => Array ( [id] => 1 [cat_name] => Search Engine Optimisation ) [1] => Array ( [id] => 2 [cat_name] => Social Media Marketing ) )

Looks right, but i have two list items this when using the code, so "Array" is the first item with a link of "category/0" and "Array" as the second item with a link of "category/1"

Totally confused also.
#7

[eluser]Dam1an[/eluser]
I think I know whats going on, you're passing the array which wraps these 2 into the view? (I can't be sure as I just noticed your controller code is your model code in your first post)
You need to wrap that in another array (which you would also use for any other variables you pass through

So somthing like
Code:
function view_categories() {
    $data['category_navigation '] = $this->category_model->getCategoriesNav();
    $this->load->view('categories/nav', $data);
}
(There would obviously be more in there as well...




Theme © iAndrew 2016 - Forum software by © MyBB