Welcome Guest, Not a member yet? Register   Sign In
Errors
#1

[eluser]NateL[/eluser]
I'm reading Professional CodeIgniter and I'm having some issues with the first bit...I've already posted in the Wrox forum, but there's no activity and i doubt I will get an answer any time soon.

These are the two errors I encounter:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: id

Filename: controllers/welcome.php

Line Number: 13
Code:
A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND `status` = 'active' ORDER BY `category_id` asc LIMIT 100' at line 4

SELECT `id`, `name`, `thumbnail`, `category_id` FROM (`products`) WHERE `id` != AND `status` = 'active' ORDER BY `category_id` asc LIMIT 100

These are some snippets of the code - which I think is exact..but who knows, could be a typo somewhere, I'm hoping.

Controller - welcome.php
Code:
class Welcome extends Controller {
function Welcome(){
        parent::Controller();    
    }
    
    function index(){
        $data["title"] = "Welcome to Claudia's Kids";
        $data["navlist"] = $this->MCats->getCategoriesNav();
        $data['mainf'] = $this->MProducts->getMainFeature();
        $skip = $data['mainf']['id'];
        $data['sidef'] = $this->MProducts->getRandomProducts(3,$skip);
        $data['main'] = 'home';
        $this->load->vars($data);
        $this->load->view('template');


    }
}

model - mproducts.php
Code:
function getRandomProducts($limit, $skip){
            $data = array();
            $temp = array();
            if ($limit == 0){
                $limit=3;
            }
            $this->db->select("id,name,thumbnail,category_id");
            $this->db->where('id !=', $skip);
            $this->db->where('status', 'active');
            $this->db->orderby("category_id", "asc");
            $this->db->limit(100);
            $Q = $this->db->get('products');
            if ($Q->num_rows() > 0){
                foreach ($Q->result_array() as $row){
                    $temp[$row['category_id']] = array(
                    "id" => $row['id'],
                    "name" => $row['name'],
                    "thumbnail" => $row['thumbnail']);
                }
            }
            shuffle($temp);
            if (count($temp)){
                for($i=1;$i<=$limit;$i++){
                    $data[] = array_shift($temp);
                }
            }
            $Q->free_result();
            return $data;
        }// end getRandomProducts

It appears that 'id' is never called in from somewhere, which is resulting in both of these errors. I can see that my mysql query is messed up because it says != AND

I have many more files with a lot more code that I *think* is irrelevant to this issue. If it appears that i am missing something that could help you diagnose this problem, please let me know. Thanks!
#2

[eluser]Pascal Kriete[/eluser]
The problem seems to be that getMainFeature() isn't returning a result that has id in it, so $skip is null which renders it blank in the query.
Could you post the code for that model function please.
#3

[eluser]NateL[/eluser]
Thanks, I believe this is the one you need:
Code:
function getProduct($id){
            $data = array();
            $options = array('id' => $id);
            $Q = $this->db->getwhere(products,$options,1);
            if ($Q->num_rows() > 0){
                $data = $Q->row_array();
            }
            
            $Q->free_result();
            return $data;
        }
#4

[eluser]Pascal Kriete[/eluser]
Hmm, now I'm confused. Does getMainFeature calls getProduct then? (i don't have the book)

If it does there should be a check in there to make sure it gets a result.
Code:
$skip = isset($data['mainf']['id']) ? $data['mainf']['id'] : '0';
#5

[eluser]NateL[/eluser]
I'm confused too...still learning this OOP stuff :-S

Anyway, perhaps it will be easier if I post all of the code I've typed up from the book so far...

Controllers - welcome.php
Code:
class Welcome extends Controller {

    function Welcome(){
        parent::Controller();    
    }
    
    function index(){
        $data['title'] = "Welcome to Claudia's Kids";
        $data['navlist'] = $this->MCats->getCategoriesNav();
        $data['mainf'] = $this->MProducts->getMainFeature();
        $skip = isset($data['mainf']['id']) ? $data['mainf']['id'] : '0';
        $data['sidef'] = $this->MProducts->getRandomProducts(3,$skip);
        $data['main'] = 'home';
        $this->load->vars($data);
        $this->load->view('template');


    }
}
Model - mproducts.php
Code:
class MProducts extends Model{
        function MProducts(){
            parent::Model();
        }    

        function getProduct($id){
            $data = array();
            $options = array('id' => $id);
            $Q = $this->db->getwhere('products',$options,1);
            if ($Q->num_rows() > 0){
                $data = $Q->row_array();
            }
            
            $Q->free_result();
            return $data;
        }
        
        function getAllProducts(){
            $data = array();
            $Q = $this->db->get('products');
            if($Q->num_rows() > 0){
                foreach ($Q->result_array() as $row){
                    $data[] = $row;
                }
            }
            $Q->free_result();
            return $data;
        }
        
        function getMainFeature(){
            $data = array();
            $this->db->select("id,name,shortdesc,image");
            $this->db->where('featured', 'true');
            $this->db->where('status', 'active');
            $this->db->orderby("rand()");
            $this->db->limit(1);
            $Q = $this->db->get('products');
            if ($Q->num_rows() > 0 ){
                foreach ($Q->result_array() as $row){
                    $data = array(
                    "id" => $row['id'],
                    "name" => $row['name'],
                    "shortdesc" => $row['shortdesc'],
                    "image" => $row['image']
                    );
                }
            }
            $Q->free_result();
            return $data;
        }// end getMainFeature
        
        function getRandomProducts($limit, $skip){
            $data = array();
            $temp = array();
            if ($limit == 0){
                $limit=3;
            }
            $this->db->select("id,name,thumbnail,category_id");
            $this->db->where('id !=', $skip);
            $this->db->where('status', 'active');
            $this->db->orderby("category_id", "asc");
            $this->db->limit(100);
            $Q = $this->db->get('products');
            if ($Q->num_rows() > 0){
                foreach ($Q->result_array() as $row){
                    $temp[$row['category_id']] = array(
                    "id" => $row['id'],
                    "name" => $row['name'],
                    "thumbnail" => $row['thumbnail']);
                }
            }
            shuffle($temp);
            if (count($temp)){
                for($i=1;$i<=$limit;$i++){
                    $data[] = array_shift($temp);
                }
            }
            $Q->free_result();
            return $data;
        }// end getRandomProducts
    } // end class

Model - mcats.php

Code:
class MCats extends Model {
    function MCats(){
        parent::Model();
}
    function getCategory($id){
        $data = array();
        $options = array('id' => $id);
        $Q = $this->db->getwhere('categories', $options, 1);
        if ($Q->num_rows() > 0){
            $data = $Q->row_array();
        }
        
        $Q->free_result();
        return $data;
    }
    
    function getAllCategories(){
        $data = array();
        $Q = $this->db->get('categories');
        if ($Q->num_rows() > 0){
            foreach ($Q->result_array() as $row){
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }
    
    function getCategoriesNav(){
        $data = array();
        $Q = $this->db->get('categories');
        if ($Q->num_rows() > 0){
            foreach ($Q->result_array() as $row){
                $data[$row['id']] = $row['name'];
            }
        }
    }
}
Views - template.php
Code:
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
&lt;link href="&lt;?=base_url();?&gt;css/default.css" rel="stylesheet" type="text/css" /&gt;
[removed]
//&lt;![CDATA[
base_url='&lt;?=base_url();?&gt;';
//]]>
[removed]
&lt;/head&gt;

&lt;body&gt;
    <div id="wrapper">
        <div id="header">
            &lt;?php $this->load->view('header');?&gt;
        </div>
        
        <div id="nav">
            &lt;?php $this->load->view('navigation');?&gt;
        </div>
        
        
        
        
        
        <div id="main">
            &lt;?php $this->load->view($main);?&gt;
        </div>
    
            <div id="footer">
                &lt;?php $this->load->view('footer');?&gt;
            </div>    
    
    
    </div>
&lt;/body&gt;
&lt;/html&gt;

View - navigation.php
Code:
if (count($navlist)){
    echo "<ul>";
        foreach ($navlist as $id => $name){
            echo "<li>";
            echo anchor("welcome/cat/$id", $name);
            echo "</li>";
        }
    echo "</ul>";
}

then there's the CSS, header and footer which only contain dummy code right now - nothing relevant to this issue.
#6

[eluser]NateL[/eluser]
The whole idea is to create a shopping cart. When someone is viewing a product, there are 3 random products that are displayed.
some other information that may help:
autoload.php
Code:
$autoload['model'] = array('MProducts', 'MCats');

View - home.php
Code:
<div id='pleft'>
    <img src='&lt;?= $mainf[' border='0' align='left' />
    <h2>&lt;?= $mainf['name'] ?&gt;;</h2>
    <p>&lt;?=$mainf['shortdesc'];?&gt;<br />
        &lt;?=anchor('welcome/product/'.$mainf['id'], 'see details');?&gt;<br />
        &lt;?=anchor('welcome/cart/'.$mainf['id'], 'buy now');?&gt;</p>
</div>

<div id='pright'>
    &lt;? foreach ($sidef as $key => $list) : ?&gt;
    <img src='&lt;?= $list[' border='0' align='left' />
    <h4>&lt;?= $list['name'];?&gt;</h4>
    <p>&lt;?=anchor('welcome/cart/'.$list['id'], 'buy now');?&gt;</p>
    &lt;? endforeach; ?&gt;
</div>
#7

[eluser]Sumon[/eluser]
In your controller you have
Code:
$data['mainf'] = $this->MProducts->getMainFeature();
$skip = $data['mainf']['id'];
lets have a close look in model
Code:
function getMainFeature(){
$data = array();
$this->db->select("id,name,shortdesc,image");
$this->db->where('featured', 'true');
$this->db->where('status', 'active');
$this->db->orderby("rand()");
$this->db->limit(1);
$Q = $this->db->get('products');
if ($Q->num_rows() > 0 ){
foreach ($Q->result_array() as $row){
    $data = array(
    "id" => $row['id'],
    "name" => $row['name'],
    "shortdesc" => $row['shortdesc'],
    "image" => $row['image']
    );
     }
}
$Q->free_result();
return $data;
}// end getMainFeature
As though getMainFeature() function return a a multi dimensional array, so $data['mainf'] in controller can't be accessed as
Code:
$data['mainf']['id'];
i think, this is permissible to access as
Code:
$index=0; //for example
$data['mainf'][$index]['id'];
and i hope second problem
Code:
A Database Error Occurred
will solved automatically. let me know is it works for you?
#8

[eluser]NateL[/eluser]
Thanks for the reply!

Unfortunately, this generates a new error while still giving me the database error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 0

Filename: controllers/welcome.php

Line Number: 16
#9

[eluser]Sumon[/eluser]
please change the model as
Code:
function getMainFeature(){
$data = array();
$this->db->select("id,name,shortdesc,image");
$this->db->where('featured', 'true');
$this->db->where('status', 'active');
$this->db->orderby("rand()");
$this->db->limit(1);
$Q = $this->db->get('products');
if ($Q->num_rows() > 0 ){
foreach ($Q->result_array() as $row){
    $data[] = array(
    "id" => $row['id'],
    "name" => $row['name'],
    "shortdesc" => $row['shortdesc'],
    "image" => $row['image']
    );
     }
}
$Q->free_result();
return $data;
}// end getMainFeature
$data[] instead of $data




Theme © iAndrew 2016 - Forum software by © MyBB