0

I am new w/ OPP and big pardon if my question maybe too simple :) Table category, navigation, etc contains multiple rows (category : samsung, apple, etc; and navigation : about us, terms, etc) and both stand as Menu in all pages (home, product,etc)

My old php code and work good is below

    <div id="categories">
    <ul>
        <?
        $mydbcategories = new myDBC();
        $resultcategories = $mydbcategories->runQuery("SELECT * FROM `category`");
        while ($rowcategories = $mydbcategories->runFetchArray($resultcategories)) {
            echo '<li><a href="'.ROOT_URL.$rowcategories[url].'">'.$rowcategories[title].'</a></li>';
        }
        ?>
    </ul>
    </div>
    <div id="navigation">

    <ul>
        <?
        $mydbnavigation = new myDBC();
        $resultnavigation = $mydbnavigation->runQuery("SELECT * FROM `navigation`");
        while ($rownavigation = $mydbnavigation->runFetchArray($resultnavigation)) { echo '<li><a href="'.ROOT_URL.$rownavigation [url].'">'.$rownavigation [title].'</a></li>';
        }
        ?>
    </ul>
    </div>

I would like to implement OOP PHP and create class then store in classes.php

   <?
   class Menu{
   var $title;
   var $url; 
   function setMenu($db){
   $mydbMenu= new myDBC();
   $resultmenu = $mydbMenu->runQuery("SELECT * FROM `$db`");
   $resultmenurows = mysqli_num_rows($resultmenu);
   while ($rowmenu = $mydbMenu->runFetchArray($resultmenu)){
        $this->title = $rowmenu[title];
        $this->url = $rowmenu[url];
    }
  }
  function getTitle() { return $this->title;}
  function getUrl() { return $this->url;}
  }
  ?>

Then i'm edit my old code with new one below;

    <div id="categories">
    <ul>
    <?
   $catmenu = new Menu();
   while ($catmenu ->setMenu('category')) { 
       echo '<li><a href="'.ROOT_URL.$catmenu->getUrl().'">'.$catmenu->getTitle().'</a></li>';
        }
        ?>
    </ul>
    </div>

    <div id="navigation">
    <ul>
        <?
        $navmenu = new Menu();
        while ($navmenu ->setMenu('category')) {
  echo '<li><a href="'.ROOT_URL.$navmenu ->getUrl().'">'.$navmenu ->getTitle().'</a></li>';
        }
        ?>
    </ul>
 </div>

I tested and error maybe because there are multiple rows (from table) in the setMenu func. How can i return this multiple rows ? should i use array ? Please help me to solve this and any reply really very appreciate

1
  • 1
    I tested and error - What is the error? Commented Feb 25, 2014 at 8:51

2 Answers 2

2
  1. You are coding PHP4 OOP style, this is very outdated. Don't use var, use public, protected, private.

  2. $this->title = $rowmenu[title] in here, title is used as a constant (no quotes), proper: $this->title = $rowmenu['title'], same with $rowcategories[title]

  3. "SELECT * FROM $db" is this correct? Or do you mean SELECT * FROM menu WHERE xxx='" . $db . "', do you catch errors if the lookup fails?

You should also look at PHP design patterns and code style to improve!

Sign up to request clarification or add additional context in comments.

Comments

-1

Try following PHP code

<?

class Menu {

var $title;
var $url;

    function setMenu($db) {
        $mydbMenu = new myDBC();
        $resultmenu = $mydbMenu->runQuery("SELECT * FROM `$db`");
        $resultmenurows = mysqli_num_rows($resultmenu);
        $this->title = array();
        $this->url = array();
        while ($rowmenu = $mydbMenu->runFetchArray($resultmenu)) {
            $this->title[] = $rowmenu['title'];
            $this->url[] = $rowmenu['url'];
        }
    }

    function getTitle($ind) {
        return $this->title[$ind];
    }

    function getUrl($ind) {
        return $this->url[$ind];
    }

}
?>

And HTML

<div id="categories">
    <ul>
        <?
        $catmenu = new Menu();
        $catmenu->setMenu('category');
        $i = 0;
        while ($catmenu->getTitle($i)) {
            echo '<li><a href="' . ROOT_URL . $catmenu->getUrl($i) . '">' . $catmenu->getTitle($i) . '</a></li>';
            $i++;
        }
        ?>
    </ul>
</div>

<div id="navigation">
    <ul>
        <?
        $navmenu = new Menu();
        $navmenu->setMenu('navigation');
        while ($navmenu->getTitle($i)) {
            echo '<li><a href="' . ROOT_URL . $navmenu->getUrl($i) . '">' . $navmenu->getTitle($i) . '</a></li>';
            $i++;
        }
        ?>
    </ul>
</div>

3 Comments

Thank you for answered, it's resolved now.once again thank you very much :)
If this answer helps you, then please up vote or accept this answer as correct answer
I think you need to do it again. Your first mark it as accepted and than you again click so it marked as not accepted :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.