dqc3469 2015-11-23 16:15
浏览 43
已采纳

Php方法没被调用

I have been trying to debug this code for a while now, and it looks like the build method is not being called. I put echo's, var_dumps, and all other kinds of signs in it, but never get anything.

Full php code

class Auto_slideshow {

  private $_img_dir;

  //constructor sets directory containing the images
  function __construct($dir) {
    //Directory source from webroot
    $_img_dir = $dir;
  }

  //Iterates through directory and constructs HTML img list
  public function build() {

    //use SPL for directory iteration
    $iterator = new DirectoryIterator($_img_dir);

    $html = '';
    $i = 1;

    //Iterate through directory
    foreach ($iterator as $file) {

      //set a variable to add class of "show" on first element
      $showClass = $i === 1 ? ' class="show"' : null;

      //exclude '.' and '..' files
      if(!$iterator->isDot()) {

        //set src attribute for img tag
        $src = $_img_dir . $iterator->getFilename();

        //Grab sizes of images
        $size = getimagesize($_img_dir . $iterator->getFilename());
        var_dump($src);
        //create img tags
        $html .= '<img src="' . $src . '" ' . $size[3] . $displayNone . ' />' . "
";

        $i++;
      }
    }

    return $html;
  } 
}

html call

<center>
    <div class="imagecontainer" id="auto-slideshow">
    <?php
    $show = new Auto_slideshow("../CMSC/images/master/slidestock/");
    $show->build();
    ?>
</div>
</center>

I also tried print $show->build();, as the tutorial showed, which also did not work.

Update

I changed $_img_dir to $this->$_img_dir' and called the method byecho show->build();` and the method still isn't being called.

This is a matter of the method not even running, not even to the point of find the images yet.

Update 2

If I remove the entire php code within the html, the rest of the page loads just fine. As it is now, the html loads only to the div that contains the php then stop everything after it.

  • 写回答

3条回答 默认 最新

  • doutangliang7769 2015-11-23 16:35
    关注

    Fix and update the code bellow:

    Constructor:

    $this->_img_dir = $dir;

    build Function:

    $iterator = new DirectoryIterator($this->_img_dir);

    $src = $this->_img_dir . $iterator->getFilename();

    $size = getimagesize($this->_img_dir . $iterator->getFilename());

    You can call it:

    echo $show->build();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码