How to convert from native php to codeigniter

I have the following db and php. I am trying to make an unordered list of categories menu. The original php works on its own. I am trying to convert this to MVC in codeigniter, and this is what I got so far and not working. If someone can indicate what I am doing wrong, I will be grateful.

Database

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `shortdesc` varchar(255) NOT NULL,
  `longdesc` text NOT NULL,
  `status` enum('active','inactive') NOT NULL,
  `parentid` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

-- 
-- Dumping data for table `categories`
-- 

INSERT INTO `categories` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`) VALUES (1, 'shoes', 'Shoes for boys and girls.', '', 'active', 7);
INSERT INTO `categories` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`) VALUES (2, 'shirts', 'Shirts and blouses!', '', 'active', 7);
...
...

menu.php (source php and working)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>View Tasks</title>
</head>
<body>
<h3>Menu</h3>
<?php 
function make_list ($parent) {

    global $data;
    echo '<ul>';

    foreach ($parent as $task_id => $todo) {

        echo "<li>$todo";

        if (isset($data[$task_id])) { 

            make_list($data[$task_id]);
        }

        echo '</li>';

    }
    echo '</ul>';

} 

$dbc = @mysqli_connect ('localhost', 'root1', 'root', 'ci_day6') OR die ('<p>Could not connect to the database!</p></body></html>');

$q = 'SELECT id, parentid, name FROM categories ORDER BY parentid ASC'; 
$r = mysqli_query($dbc, $q);

$data = array();

while (list($id, $parentid, $name) = mysqli_fetch_array($r, MYSQLI_NUM)) {

    $data[$parentid][$id] =  $name;

}

make_list($data[0]);

?>

</body>
</html>

This php prints the following html

Menu

    * clothes
          o shoes
          o shirts
          o pants
          o dresses
    * fun
          o toys
          o games

My MVC is not working yet.

cat_menu_model.php (model)

<?php

class Cat_menu_model extends Model
{
         function Cat_menu_model()
        {
            parent::Model();

        }

        function get_categories_nav()
        {

            $data = array();
            $this->db->select('id,name,parentid');
            $this->db->where('status', 'active');
            $this->db->orderby('parentid','asc');
            $this->db->orderby('name','asc');

            $Q = $this->db->get('categories');
        if ($Q -> num_rows() > 0){
          foreach ($Q -> result_array() as $row){
          $data[$row['parentid']][$row['id']] = $row['name'];
          }
          }

           $Q->free_result(); 
           return $data; 

        }
}

cat_menu.php (controller)

<?php

class Cat_menu extends Controller
{

    function Cat_menu()
    {
        parent::Controller();
    }
    function make_menu($parent)
    {
        $this->load->model('cat_menu_model');

        $data['navlist'] = $this->cat_menu_model->get_categories_nav();
            $this -> load ->view('menu');
    }
}

menu.php (view)

<?php
if (count($navlist))
{
  $this->make_menu($data[0]);
  echo '<ol>';
    foreach ($parent as $id => $catname) {

        echo "<li>$catname";

        if (isset($data[$id])) { 

        make_menu($data[$id]);

        }

        echo '</li>';

    } 
 echo '</ol>';
}
?>

Error messages are displayed.

A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Cat_menu::make_menu()

Filename: controllers/cat_menu.php

Line Number: 10
+3
source share
3 answers

PHP , , Cat_menu:: make_menu(), . make_menu ($ parent) Cat_menu.

- , $parent - $parent make_menu.

, , , . . :

cat_menu.php()

<?php

class Cat_menu extends Controller
{

    function Cat_menu()
    {
        parent::Controller();
    }

    function make_menu($parent = FALSE) //Is this $parent argument needed?
    {
        $this->load->model('cat_menu_model');

        $data['navlist'] = $this->cat_menu_model->get_categories_nav();

        //If you want to parse data to a view you need to state it
        $this->load->view('menu', $data);
    }
}

, , . . CodeIgniter. , .


OP * , .

** OP = *

print_r ($ navlist) . OP :

Array ( 
    [0] => Array ( 
        [7] => clothes 
        [8] => fun 
    ) 
    [7] => Array ( 
        [3] => pants 
        [2] => shirts 
        [1] => shoes 
    ) 
    [8] => Array ( 
        [6] => games 
        [5] => toys 
    )
)

, ActiveRecord OP CI - MVC:

SELECT 
    id, 
    parentid, 
    name 
FROM 
    categories 
ORDER BY 
    parentid ASC

.

$this->db->select('id,name,parentid');
$this->db->where('status', 'active');
$this->db->orderby('parentid','asc');
$this->db->orderby('name','asc');
$Q = $this->db->get('categories');

CI SQL . SQL :

SELECT
    id, 
    name,
    parentid
FROM
    categories
WHERE
    status = 'active'
ORDER BY
    parentid ASC,
    name ASC

, , , .

OP , . .: PHP/MySQL - . - OP ( MVC) . , html - , .

. , php- , - , OP:

function to_hierarchy($collection = NULL)
{
    if (is_null($collection)) return false;

    $tree = array();

    foreach($collection[0] as $key => $value)
    {
        $tree[$value] = array();

        foreach($collection[$key] as $item)
        {
            array_push($tree[$value], $item);
        }
    }

    return $tree;
}

:

cat_menu.php()

<?php

class Cat_menu extends Controller
{

    function Cat_menu()
    {
        parent::Controller();
    }

    function make_menu($parent = FALSE) //Is this $parent argument needed?
    {
        $this->load->model('cat_menu_model');

        $get_nav_list = $this->cat_menu_model->get_categories_nav();

        $format_nav_list = $this->cat_menu_model->to_hierarchy($get_nav_list);

        //Load the HTML helper for ul/ol conversion
        $this->load->helper('html');

        $data['navlist'] = $format_nav_list;

        //If you want to parse data to a view you need to state it
        $this->load->view('menu', $data);
    }
}

:

menu.php()

<?php
    echo ul($navlist);
?>

: PHP, . , - .

+4

ul() HTML-, <ul> , .

+1

In addition, you do not pass data to the view in the controller. it should be:

$ this → load → view ('menu', $ data);

also, it looks like you will create an infinite loop the way you configure it. make_menu loads the menu.php view, but then the call invokes this method.

0
source

Source: https://habr.com/ru/post/1719805/


All Articles