Good afternoon all.

I have built the below class, and seem to have a problem where nothing is being displayed:

<?php

include_once '../include/tableStructure.php';
include 'crud.php'; //<- class by pritaeas

class navigation {

    public $dsn;

    public function __construct() {
        $this->dsn = new crud($username, $passwd, $tableConfig);
    }

    public function nav($table, $live, $loc) {
        /**
         * nav returns multiple records from the specified table with conditions
         * @param string $table Table name
         * @param array $live Set in DB condition to show page
         * @return array $loc Set in DB shows location of page in Nav
         */
        $nav = $this->dsn->getRecords($table, array('live' => $live, 'loc' => $loc));

        $result = count($nav);
        $cnt = 0;
        echo '<nav><ul>';
        while ($cnt < $result) {

            foreach ($nav[$cnt] AS $key => $value) {
                if (is_numeric($key)) {
                    unset($key, $value);
                } else {

                    if ($key != 'id' && $key != 'content' && $key != 'live' && $key != 'keywords' && $key != 'metaDesc') {
                        if ($key == 'title') {
                            echo '<li><a href="#">' . $value . '</a></li>';
                        }
                    }
                    unset($key, $value);
                }
            }
            $cnt++;
        }
        // Facebook include and G+1 code -> see main code page -> put into sep include files for ease of modification
        echo '</ul></nav>';
        unset($dsn);
    }

//    protected function sideNav($loc = 0, $live = 0) {
//        
//    }
}

The class uses the crud class by pritaeas. Any help is greatly appreciated.

Recommended Answers

All 16 Replies

What do you mean, nothing shows? Have you declared an object of this class?...

@phorce,

This my declaration:

$navigationTop = new navigation('user_1', 'password_1', $tableConfig);

$top = $navigationTop->nav('content', 1, 1);

Line 33 excludes any output if the key is content.

Apart from that, the nav function is not returning anything, so why do you try to assign it to the top variable?

Pritaeaes,

I am not sure i understand your second comment?

The content is a table, but also column within the same name table. Not sure why, but this was how it was put together.

What do you expect to happen when you execute line 3:

$top = $navigationTop->nav('content', 1, 1);

What was expecting is the following:

$top was meant to hold teh content of the class function nav, 'content' is the table name with 1,1 being the location and status if the page is live.

The function itself works well as a stand alone function, but i wanted to put it into a class so it can be reused.

Then instead of using echo, append the content to a variable, so you can return it at the end of the method.

I think i am missing the point, i have tried this, but still nothing is output:

public function nav($table, $live, $loc) {
        /**
         * nav returns multiple records from the specified table with conditions
         * @param string $table Table name
         * @param array $live Set in DB condition to show page 1 = Live, 0 = Not Live
         * @return array $loc Set in DB shows location of page in Nav 1 = Top, 0 = Side
         */

        $nav = $this->dsn->getRecords($table, array('live' => $live, 'loc' => $loc));

        $result = count($nav);
        $cnt = 0;
        $navKey = '<nav><ul>';
        while ($cnt < $result) {
            foreach ($nav[$cnt] AS $key => $value) {
                if (is_numeric($key)) {
                    unset($key, $value);
                } else {

                    if ($key != 'id' && $key != 'content' && $key != 'live' && $key != 'keywords' && $key != 'metaDesc') {
                        if ($key == 'title') {
                            $navKey .= '<li><a href="#">' . $value . '</a></li>';
                        }
                    }
                    unset($key, $value);
                }
            }
            $cnt++;
        }
        // Facebook include and G+1 code -> see main code page -> put into sep include files for ease of modification
        $navKey .= '</ul></nav>';
        unset($dsn);

        return $navKey;
    }

If you execute this:

$top = $navigationTop->nav('content', 1, 1);

What does:

$nav = $this->dsn->getRecords($table, array('live' => $live, 'loc' => $loc));

show, if you print_r($nav); ?

I am getting an empty array:

Array()

That's why your result is empty. The problem lies in the call to getRecords. Where do the parameters in the constructor come from?

public function __construct() {
    $this->dsn = new crud($username, $passwd, $tableConfig); // Probably empty/undefined variables
}

Pritaeas i really apprciate your assistance on this.

$tableConfig is from an included file containing a multi dimensional array:

$tableConfig = array(       
    'staff' => array(
        'id' => PDO::PARAM_INT,
        'drv_name' => PDO::PARAM_STR,
        'added' => PDO::PARAM_STR,
        'blurb' => PDO::PARAM_STR,
        'image' => PDO::PARAM_STR,
        'live' => PDO::PARAM_INT
    ),
    'content' => array(
        'id' => PDO::PARAM_INT,
        'title' => PDO::PARAM_STR,
        'content' => PDO::PARAM_STR,
        'live' => PDO::PARAM_INT,
        'loc' => PDO::PARAM_INT,
        'keywords' => PDO::PARAM_STR,
        'metaDesc' => PDO::PARAM_STR
    )
);

I have manually entered the $username & $passwd, but it still does not display

Where is table config coming from? I think you need to pass the value to the constructor like this:

public function __construct($username, $passwd, $tableConfig) 
{
    $this->dsn = new crud($username, $passwd, $tableConfig);
}

Still couldnt get it to work with the __construct

So i have changed to this:

public function nav($table, $live, $loc) {
        /**
         * nav returns multiple records from the specified table with conditions
         * @param string $table Table name
         * @param array $live Set in DB condition to show page 1 = Live, 0 = Not Live
         * @return array $loc Set in DB shows location of page in Nav 1 = Top, 0 = Side
         */
        $tableConfig = array(
                'content' => array(
                'id' => PDO::PARAM_INT,
                'title' => PDO::PARAM_STR,
                'content' => PDO::PARAM_STR,
                'live' => PDO::PARAM_INT,
                'loc' => PDO::PARAM_INT,
                'keywords' => PDO::PARAM_STR,
                'metaDesc' => PDO::PARAM_STR
            )
        );


        $dsn = new crud('USER', 'PASSWORD', $tableConfig);
        $nav = $dsn->getRecords($table, array('live' => $live, 'loc' => $loc));
        $result = count($nav);
        $cnt = 0;
        $navKey = '<nav><ul>';
        while ($cnt < $result) {
            foreach ($nav[$cnt] AS $key => $value) {
                if (is_numeric($key)) {
                    unset($key, $value);
                } else {

                    if ($key != 'id' && $key != 'content' && $key != 'live' && $key != 'keywords' && $key != 'metaDesc') {
                        if ($key == 'title') {
                            $navKey .= '<li><a href="#">' . $value . '</a></li>';
                        }
                    }
                    unset($key, $value);
                }
            }
            $cnt++;
        }
        // Facebook include and G+1 code -> see main code page -> put into sep include files for ease of modification
        $navKey .= '</ul></nav>';
        unset($dsn);

        return $navKey;
    }

And this works. Think i will just have to work back to see where the annoying issue lays.

@Pritaeas -> many thanks for your assistance good sir.

The problem is most likely that you need to pass the right parameters to the constructor, as stated previously.

@Pritaeas,

You are a legend.

I carried on testing and resolved the issue with the __construct:

The class:

include 'crud.php';

class navigation {

    public function __construct($username, $password, $tableConfig) {
        $this->username = $username;
        $this->password = $password;
        $this->tableConfig = $tableConfig;
    }

    public function nav($table, $live, $loc) {
        /**
         * nav returns multiple records from the specified table with conditions
         * @param string $table Table name
         * @param array $live Set in DB condition to show page 1 = Live, 0 = Not Live
         * @return array $loc Set in DB shows location of page in Nav 1 = Top, 0 = Side
         */

        $dsn = new crud($this->username, $this->password, $this->tableConfig);
        $nav = $dsn->getRecords($table, array('live' => $live, 'loc' => $loc));
        $result = count($nav);
        $cnt = 0;
        $navKey = '<nav><ul>';
        while ($cnt < $result) {
            foreach ($nav[$cnt] AS $key => $value) {
                if (is_numeric($key)) {
                    unset($key, $value);
                } else {

                    if ($key != 'id' && $key != 'content' && $key != 'live' && $key != 'keywords' && $key != 'metaDesc') {
                        if ($key == 'title') {
                            $navKey .= '<li><a href="#">' . $value . '</a></li>';
                        }
                    }
                    unset($key, $value);
                }
            }
            $cnt++;
        }
        // Facebook include and G+1 code -> see main code page -> put into sep include files for ease of modification
        $navKey .= '</ul></nav>';
        unset($dsn);

        return $navKey;
    }
}

And the call:

$navigat = new navigation('XXXXXXXXuser', 'XXXXXXXXXX', $tableConfig);

$top = $navigat->nav('content', 1, 1);

print_r($top);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.