我是PHP新手,有问题。我在首页的侧面有一个导航栏,显示4个链接。我应该在子类中编写一个方法setWhichPage,该方法将从父类访问名为'thatpage'的CGI参数,并使用它来确定4个链接页面中的哪个将显示在主页的主要部分中,并使用另一个称为getMainFunction的方法。导航栏是使用子类(称为TravelAgent)中存在的getLeftNavBar和createNavbarArray方法创建的,但从父类(称为Company)中获取其变量。 setWhichPage方法也应该在此子类中。我应该使用值$ _GET或$ _REQUEST数组来设置属性$ whichpage中的值。 $ whichpage属性的可接受值应与getLeftNavBar方法匹配。方法getLeftNavBar和setWhichPage可以一起使用,以便当用户单击导航栏中的链接时,他们应该创建以下链接:
http://amazingadventures.000webhostapp.com/?whichpage=home
http://amazingadventures.000webhostapp.com/?whichpage=sales
http://amazingadventures.000webhostapp.com/?whichpage=support
http://amazingadventures.000webhostapp.com/?whichpage=contact
我遇到麻烦的地方是,当我尝试让setWhichPage将变量$ whichpage设置为链接中四个值之一时,出现了此错误消息:
通知:未定义的索引:/storage/ssd1/873/8888873/public_html/index.php中的哪个页面
你能告诉我我在做什么错吗?在此先感谢您的帮助。以下是相关代码:
<?php
class Company { //start of parent class Company
protected $company_name;
protected $company_address;
protected $company_url;
protected $company_email;
protected $whichpage;
public function __construct(){
$this->company_name = "Amazing Adventures Travel";
$this->company_address = "13999 Turtle Way, Los Angeles, California, 90001";
$this->company_url = "http://amazingadventures.000webhostapp.com/";
$this->company_email = "email.edu";
$this->whichpage = "home";
}
//other methods...
} //end of parent class Company
class TravelAgent extends Company { //start of child class TravelAgent
var $navbar_array;
function create_navbar_array() {
$mainurl = $this->company_url;
$this->navbar_array = array("Home Page"=>"$mainurl?whichpage=home", "Sales"=>"$mainurl?whichpage=sales",
"Support" => "$mainurl?whichpage=support", "Contacts" => "$mainurl?whichpage=contact");
}
function getLeftNavBar() {
echo "<table border=1, td width=100, height = 40><tr>";
foreach($this->navbar_array as $x => $x_value) {
echo '<tr><td><a href="' . $x_value . '">'. $x ."</a></td></tr>";
echo "<br>";
}
echo "</tr></table>";
}
function setWhichPage(){
$whichpage = $_GET['whichpage'];
}
function getMainSection() {
echo "The ".$whichpage." page";
}
}
//end of child class TravelAgent
$travelobject = new TravelAgent(); //object creation
$travelobject->getHeader(); //function calls
$travelobject->create_navbar_array();
$travelobject->getLeftNavBar();
$travelobject->setWhichPage();
$travelobject->getFooter();
?>
<table style='width:100%' border='1'> //HTML
<tr>
<td style='width:15%'>Left Navigation Bar</td>
<td> getMainSection()</td> //function call
</tr>
</table>
答案 0 :(得分:0)
您必须用$ this-> whichpage引用它,并检查是否设置了$ _GET ['whichpage']。大致如下:
function setWhichPage(){
if(isset($_GET['whichpage'])) {
$this->whichpage = $_GET['whichpage'];
}
}
function getMainSection() {
echo "The ".$this->whichpage." page";
}
答案 1 :(得分:0)
通知:未定义的索引:/storage/ssd1/873/8888873/public_html/index.php中的哪个页面
此错误表示$_GET
数组没有键whichpage
。
如果您打开http://amazingadventures.000webhostapp.com/,则$_GET
数组将为空。
最佳做法是在访问之前检查密钥是否存在。 setWhichPage的示例:
$this->whichpage = isset($_GET['whichpage']) ? $_GET['whichpage'] : $this->whichpage; // "home" by default