Welcome Guest, Not a member yet? Register   Sign In
building a list of categories
#1

[eluser]paulcj2[/eluser]
Building a list of categories, I am getting undefined variables in the views for my header and body of my category page.


My controller code includes:
Code:
$data = array(); //holds data for header, homepage & footer views
            $sql = "
            SELECT id, name, descrip_cat, keywords_cat, order_cat
            FROM category_products
            WHERE site_id = '" . SITE_ID . "'
            AND visible='Y'
            ORDER BY order_cat, name";
                
        $rs = $this->db->query($sql);
        foreach ($rs->result() as $row) {
            $prod_cat['id_cat'] = $row->id;
            $prod_cat['name_cat'] = $row->name;
            $prod_cat['descrip'] = $row->descrip_cat;
            $prod_cat['keywords'] = $row->keywords_cat;
            $prod_cat['order_cat'] = $row->order_cat;
            $prod_cat_results[] = $prod_cat;
            }
            $data['prod_cat_results'] = $prod_cat_results;
            $data['title'] = "Product categories";

        $this->load->view('page/header', $data);    
        $this->load->view('prod/cats_prod', $data);
        $this->load->view('page/footer');
My view code includes:
Code:
foreach ($prod_cat_results as $prod_cat) {
  echo "<h3><a href='/cat_prod/" . SITE_ADDRESS . "/$id_cat'>$name_cat</a></h3>";
  echo "<p>$descrip</p>";
  }

I suspect the problem is here
Code:
$prod_cat_results[] = $prod_cat;
            }
            $data['prod_cat_results'] = $prod_cat_results;
Any thoughts?
#2

[eluser]paulcj2[/eluser]
I just realized I had the wrong syntax on my view. I changed it to:
Code:
foreach ($prod_cat_results as $prod_cat) {
  echo "<h3><a href='/cat_prod/" . SITE_ADDRESS . "/".$prod_cat['>".$prod_cat['name_cat']."</a></h3>";
  echo "<p>".$prod_cat['descrip']."</p>";
  }
However, it only displays the last category in the foreach loop. The header variable is looking for the variable $descrip. It probably chokes on $prod_cat['descrip'].

So there are two issues here:
1) Why am I not getting a looping to show the list of categories but only the last?
2) I'll have to figure out a way of accommodating the header's need for one variable $descrip instead of the looped description for each category.

I can use help on #1.

BTW, the first variable $prod-cat['id_cat'] is not displaying correctly in this post, but it is right in my script.
#3

[eluser]paulcj2[/eluser]
I found the answer to issue #1. I was missing an apostrophe at the end of href='....

in the view script:
Code:
echo "<h3><a href='/cat_prod/" . SITE_ADDRESS . "/".$prod_cat['>".$prod_cat['name_cat']."</a></h3>";
It is restored as above and now the script is looping fine.

Re #2: I set defined some variables for the header outside the product category loop in the controller script.
Code:
$data['descrip'] = "Product categories of '" . SITE_ID . "' ";
$data['keywords'] = "product categories of '" . SITE_ID . "' ";




Theme © iAndrew 2016 - Forum software by © MyBB