Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
1  
I tested and error - What is the error? –  DanFromGermany Feb 25 at 8:51
add comment

2 Answers

  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!

share|improve this answer
 
thank you also for advice :) –  user3349980 Feb 25 at 13:24
add comment

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>
share|improve this answer
 
Thank you for answered, it's resolved now.once again thank you very much :) –  user3349980 Feb 25 at 13:23
 
If this answer helps you, then please up vote or accept this answer as correct answer –  Hassan Feb 25 at 13:34
 
yes i did it before –  user3349980 Feb 26 at 16:49
 
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 :) –  Hassan Feb 27 at 7:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.