Welcome Guest, Not a member yet? Register   Sign In
Trying to learn plz help.... ( getting values from database )
#1

[eluser]harry_singh[/eluser]
Hi ... m new to codeigniter and php . Found codeigniter pretty interesting so got some motivation to build some application .... I have seen the video tutorials they are helpful but i m stuck. I just want to build some application having blog in it and some other features like themes and much more ( will discuss later ) so i thought of starting with making model, controller and view for categories ...

Here is the model page

Code:
<?php

class Article_cat_model extends Model{
    function Article_cat_model()
    {
    parent::Model();
    }
    
    function get_article_cats()
    {    
        $query = $this->db->get('article_category');
        return $query->result();
    }
    
        }

?>

this is the controller page

Code:
<?php

    class Article_cats extends Controller {
        
        function Article_cats()
        {
            parent::Controller();
            $this->load->model('Article_cat_model');
        }
        
        function index()
        {
                $data['title'] = "Test page title";
                $data['query'] = $this->Article_cat_model->get_article_cats();
                $this->load->view('template', $data);
        }
        
    
}    
?>

Template looks like this

Code:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
&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;/head&gt;
&lt;body&gt;
<div id=”wrapper”>
<div id=”main”>
&lt;?php
    foreach ($query as $row)
    {
    print $row-> article_cat_name;
    print '</br>';
    }
?&gt;
</div>
</div>
&lt;/body&gt;
&lt;/html&gt;


I m just stuck what to do next with the view page and dont even know whether what i m doing is correct or not ....

Waiting for reply :down:
#2

[eluser]jedd[/eluser]
Hi Harry, and welcome to CI.

You seem to be doing okay. Two things I'd suggest up front - show us your schema (your model makes a reference to a database column that looks a bit too verbose, even by my standards!). Secondly, your controller hasn't asked your model for any data here .. yet.

EDIT: Missed the view .. flipping OSX's rapid scroll feature. Sorry.

With your view, are you getting any output at all? It looks to be fine, though I tend to work with arrays over objects.
#3

[eluser]Thorpe Obazee[/eluser]
harry_singh, have you tried phpmyadmin?

Try outputting the sql query generated and paste it on phpmyadmin. See if it actually works.
#4

[eluser]harry_singh[/eluser]
thanks for your concern Jedd and Bargainph . . . Yes i do get an output for that just trying to know what wud be the better way of doing it.....
#5

[eluser]Thorpe Obazee[/eluser]
I thought you were having some problem when you posted this :long:
[quote author="harry_singh" date="1239545170"]I have seen the video tutorials they are helpful but i m stuck.[/quote]

What you're doing is correct.
[quote author="harry_singh" date="1239545170"]I m just stuck what to do next with the view page and dont even know whether what i m doing is correct or not ....

Waiting for reply :down:[/quote]
#6

[eluser]harry_singh[/eluser]
Hi Bargainph,

I wasnt clear for what i want to do... let me describe step by step

here is my database table
Code:
CREATE TABLE IF NOT EXISTS `article_category` (
  `id` int(11) NOT NULL auto_increment,
  `order_by` int(11) default '0',
  `parent_id` int(11) NOT NULL default'0',
  `title` varchar(50) default NULL,
  `meta_description` varchar(255) default NULL,
  `meta_keywords` varchar(255) default NULL,
  `locked` tinyint(1) default '0',
  `subcat` varchar(11) default NULL,
  `item_counter` int(11) default '0',
  `subcat_counter` int(11) default '0',
  PRIMARY KEY `id` (`id`)
) ENGINE=MyISAM  ;

In the application i want categories and subcategories , and option to change the display order of the categories but thats the later part. All i want is to show only parent categories first and when clicked on it the sub categories may show up

here is what m trying for my model
Code:
&lt;?php

class Article_cat_model extends Model{
    function Article_cat_model()
    {
    parent::Model();
    }
    
    function get_article_cats()
    {    
        $query = $this->db->query('SELECT * FROM article_category WHERE parent_id = 0');
        return ( $query->num_rows () > 0 );
    }
    
        }

?&gt;

as i said earlier that i dont have knowledge of php so little help wud be great and just what i shud do in the models page and controller page ....
#7

[eluser]Thorpe Obazee[/eluser]
Try this in the Controller:
Code:
&lt;?php


        function index()
        {
                $id = (int) $this->uri->segment(3);
                $data['title'] = "Test page title";
                $data['query'] = $this->Article_cat_model->get_article_cats($id);
                $this->load->view('template', $data);
        }

?&gt;
Try this in the Model:
Code:
&lt;?php

    
    function get_article_cats($id)
    {    
$query = $this->db->query("SELECT * FROM article_category WHERE parent_id = $id");
    // do an if $query->num_rows() > 0 here then
        return $query->result();
    }

?&gt;
#8

[eluser]harry_singh[/eluser]
thanks Bargainph, thats works fine , i ll continue with that and post here if any problem is there .




Theme © iAndrew 2016 - Forum software by © MyBB