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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大