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
You are coding PHP4 OOP style, this is very outdated. Don't use var
, use public
, protected
, private
.
$this->title = $rowmenu[title]
in here, title
is used as a constant (no quotes), proper: $this->title = $rowmenu['title']
, same with $rowcategories[title]
"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!
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>