[英]OOP PHP MySQL return multiple rows and variable
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) 如果我的问题可能太简单了,我是w / OPP的新手,请原谅:)表格类别,导航等包含多行(类别:三星,苹果等;以及导航:关于我们,条款等),并且两者都作为菜单在所有页面(首页,产品等)中
My old php code and work good is below 我的旧PHP代码和工作良好如下
<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 我想实现OOP PHP并创建类,然后将其存储在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. 我测试了错误,可能是因为setMenu函数中有多行(来自表)。 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. 您正在编写PHP4 OOP样式,这已经过时了。 Don't use var
, use public
, protected
, private
. 不要使用var
,使用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]
$this->title = $rowmenu[title]
在这里, title
用作常量(不带引号),正确: $this->title = $rowmenu['title']
,与$rowcategories[title]
"SELECT * FROM $db"
is this correct? "SELECT * FROM $db"
是否正确? Or do you mean SELECT * FROM menu WHERE xxx='" . $db . "'
, do you catch errors if the lookup fails? 还是说SELECT * FROM menu WHERE xxx='" . $db . "'
,如果查找失败,您是否会捕获错误?
You should also look at PHP design patterns and code style to improve! 您还应该查看PHP的设计模式和代码样式以进行改进!
Try following PHP code 尝试遵循以下PHP代码
<?
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 和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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.