PHP关联数组变量不显示内容

时间:2015-05-21 20:40:18

标签: php arrays associative-array

我正在尝试从$ cfstories数组中回显一个值到屏幕。我无法弄清楚为什么$ cfstory [“name”]没有显示任何内容。当我执行var_dump而不是echo时,它返回null。以下是该页面的所有相关文件。这是我在Stack Overflow上的第一篇文章,所以我很抱歉,如果我应该以不同的方式发布这个代码。感谢您提供的任何帮助!

这是campfire.php:

<?php
require_once("../inc/config.php");
require_once(ROOT_PATH."inc/campfire_arr.php");?>
<?php
if (isset($_GET["name"])){
 $cfstoryname = $_GET["name"];
 if (isset($cfstories[$cfstoryname])){
  $cfstory = $cfstories[$cfstoryname];
 }
}
$pageTitle = $cfstory["name"];
?>

<!DOCTYPE html>
<html>
<?php
  include(ROOT_PATH."inc/header.php");
?>
  <header class="contact-header">
  <?php include(ROOT_PATH."inc/name_title_header.php");?>
  <?php include(ROOT_PATH."inc/menu.php");?>
  </header>
   <div class="breadcrumb"><a href="campfirestories.php">Campfire Stories</a> &gt; <?php echo $cfstory ["name"]; ?></div>
  <div id="wrapper">
    <div id="main-left">
      <div class="campfire">
        <h3 id="blog-top-h3"> <?php echo $cfstory["name"];?> </h3>
      </div>
    </div>
  </div>
  <footer class="contact-footer">
    <?php include(ROOT_PATH."inc/footer.php"); ?>
  </footer>
    <?php include(ROOT_PATH.'inc/js_scripts.php');?>

这是campfirestories.php:

<?php
require_once("../inc/config.php");
require_once(ROOT_PATH."inc/campfire_arr.php");?>
<!DOCTYPE html>
<html>
<?php
  $pageTitle="Campfire Stories";
  include(ROOT_PATH."inc/header.php");
?>
  <header class="contact-header">
      <?php include(ROOT_PATH."inc/name_title_header.php");?>
      <?php include(ROOT_PATH."inc/menu.php");?>
  </header>
    <div id="wrapper">
      <div id="main-left">
        <div class="campfire">
          <h3>Campfire Stories</h3>
          <ul class="cts">
            <?php foreach($cfstories as $cfstoryname => $cfstory)
              echo display_campfire_html($cfstoryname, $cfstory);
            ?>
          </ul>
        </div>
      </div>
    </div>
    <footer class="contact-footer">
      <?php include(ROOT_PATH."inc/footer.php"); ?>
    </footer>
    <?php include(ROOT_PATH.'inc/js_scripts.php');?>

这是campfire_arr.php:

<?php
  function display_campfire_html($cfstoryname,$cfstory){
    $output = "";
    $output .= "<li>";
    $output .= '<a href="campfire.php?name='.$cfstoryname.'/">';
    $output .= "<figure>";
    $output .= '<img src="'.BASE_URL.$cfstory["img"] .'" alt="'.$cfstory["location"].'">';
    $output .= "<figcaption>".$cfstory["name"]."</figcaption>";
    $output .= "</figure>";
    $output .= "</a>";
    $output .= "</li>";

    return $output;
  }

  $cfstories = array();
  $cfstories["clearwater_lake"] = array(
    "img" => "img/cl-onf.jpg",
    "name" => "Clearwater Lake",
    "location" => "Ocala National Forest, FL"
  );
  $cfstories["grayton_beach"] = array(
    "img" => "img/denali.jpg",
    "name" => "Grayton Beach",
    "location" => "Santa Rosa Beach, FL"
  );
  $cfstories["juniper_springs"] = array(
    "img" => "img/arch.jpg",
    "name" => "Juniper Springs",
    "location" => "Ocala National Forest, FL"
  );
  $cfstories["cohutta_mountain"] = array(
    "img" => "img/yosemite.jpg",
    "name" => "Cohutta Mountain Loop",
    "location" => "Cohutta Mountain, GA"
  );
?>

0 个答案:

没有答案