简体   繁体   English

表中显示的PhP关联数组

[英]PhP Associative Array displayed in a Table

I am VERY new to PhP. 我是PhP的新手。 Quick question that I am having trouble finding the answer to online, although I am sure most of you will quickly know the answer. 我很难在网上找到答案的快速问题,尽管我相信你们中的大多数人都会很快知道答案。 I have the following code creating an associative array and then I am trying to display it in a table. 我有以下代码创建关联数组,然后尝试在表中显示它。 I know there are easier ways to display it in a table like a foreach loop, but I would like to learn this method first : 我知道有一种更简单的方法可以像foreach循环一样在表中显示它,但我想先学习一下这种方法:

class CarDealer extends Company {

var $navbar_array = array();

 function  create_navbar_array ( )  {
    $mainurl = $this->company_url;   // get the main url address of this web page
    $this->navbar_array = array( "Home Page"=>"$mainurl?whichpage=home", "Sales"=>"$mainurl?whichpage=sales",
       "Support" => "$mainurl?whichpage=support", "Contacts" => "$mainurl?whichpage=contact" );
 }

function getLeftNavBar() {
$data ="<table border='1' style='background-color:yellow; width:35%'>";
$data .="<tr><td>$this->navbar_array['Home Page']</td></tr>";
$data .="<tr><td>$this->navbar_array['Sales']</td></tr>";
$data .="<tr><td>$this->navbar_array['Support']</td></tr>";
$data .="<tr><td>$this->navbar_array['Contacts']</td></tr>";
$data .="</table>";
return $data;
}

}

Later in my code I create an object for my class and then try to print the table. 在稍后的代码中,我为类创建一个对象,然后尝试打印该表。 Unfortunately I am just getting an output of things like Array['Home Page']. 不幸的是,我只是得到Array ['Home Page']之类的东西的输出。

$carobject = new CarDealer();

$carobject->create_navbar_array();
print $carobject->getLeftNavBar();

before return $data;, print_r($data); 在返回$ data;之前,print_r($ data); so you can know the associative array's structure. 这样您就可以知道关联数组的结构。 If it didnt print on the page, try looking in view-page-source. 如果它没有在页面上打印,请尝试查看view-page-source。 You'll get an clear picture. 您会得到清晰的画面。 I think $data should be an array, so prob it should be like $data['someField'] = $someValues; 我认为$ data应该是一个数组,所以概率应该像$ data ['someField'] = $ someValues;。 And, Create or define the array before you use them. 并且,在使用它们之前创建或定义数组。

you need to pass array values to the function try 您需要将数组值传递给函数try

class extends Company {

var $navbar_array = array();

 function  create_navbar_array ( )  {
    $mainurl = $this->company_url;   // get the main url address of this web page
    $this->navbar_array = array( "Home Page"=>"$mainurl?whichpage=home", "Sales"=>"$mainurl?whichpage=sales",
       "Support" => "$mainurl?whichpage=support", "Contacts" => "$mainurl?whichpage=contact" );
       return $this->navbar_array;
 }

function getLeftNavBar($arr) {
$data ="<table border='1' style='background-color:yellow; width:35%'>";
$data .="<tr><td>".$arr['Home Page']."</td></tr>";
$data .="<tr><td>".$arr['Sales']."</td></tr>";
$data .="<tr><td>".$arr['Support']."</td></tr>";
$data .="<tr><td>".$arr['Contacts']."</td></tr>";
$data .="</table>";
return $data;
}

}

$carobject = new CarDealer();

$arr = $carobject->create_navbar_array();
print $carobject->getLeftNavBar($arr);

or need to make public your array 或需要公开您的阵列

public $navbar_array = array();

You need to concatenate the array values inside the getLeftNavBar() method so php knows to parse them. 您需要在getLeftNavBar()方法内连接数组值,以便php知道解析它们。

try 尝试

 $data .="<tr><td>{ $this->navbar_array['Home Page'] }</td></tr>";

or 要么

 $data .="<tr><td>".$this->navbar_array['Sales']."</td></tr>";

Why not just call create_navbar_array() inside getLeftNavBar() 为什么不只在getLeftNavBar()内部调用create_navbar_array()

function getLeftNavBar()   {
   $this->create_navbar_array();
  .....

You could rewrite it like this 您可以这样重写它

 class CarDealer  {

   private function getUrls()  {
    return  array( 
      "Home Page"   => "$this->company_url?whichpage=home", 
      "Sales"   => "$this->company_url?whichpage=sales",
      "Support"     => "$this->company_url?whichpage=support", 
      "Contacts"    => "$this->company_url?whichpage=contact" 
    );

  }

public function getLeftNavBar() {

    $data ="<table border='1' style='background-color:yellow; width:35%'>";

    foreach($this->getUrls() as $url ) {
        $data .="<tr><td>".$url."</td></tr>";
    }

    return  $data . "</table>"; 
   }

  }


 $carobject = new CarDealer();
 print $carobject->getLeftNavBar();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM