dqby43944 2014-05-25 03:54
浏览 23
已采纳

在类函数中轻松迭代php

I'm trying to create a nice and easy iterator and it worked at first, then I realized I'd need more information for the function so I tried to extend it and well it did not work.

Example Usage

$easyCMS->iterate($post,
     echo $content[0];
     echo $content[1];
 );

Class Function

public function iterate($d,$fn){
   $this->item = $d;
   foreach($this->item as $post){
     echo $fn;
   }
}

Current Index.php Usage

$post = $easyCMS->my_query('SELECT * FROM  `newsPost`');
//returns array
$easyCMS->iterate($post,
   $content[0]."<br>",
   $content[1]."<br>",
   $content[2]."<br>",
       $content[3]."<br>",
       $content[4]."<br>",
);
 //$post would be the first argument and after that would be what we want our function to do.

I get the error =>

Parse error: syntax error, unexpected ';' in .../index.php on line 23

Which I know that it's the constant $content[num] but I'd like to know how I'd do this for I know I could with JavaScript using the call method.

My database table looks something like

id: 1 == content: "Whats up" == ...etc

I want my code to iterate over these so then I can write like so

$easyCMS->iterate($post,
  '<div class="hello">'.$content[0].'</div><div id="post_'.$content[1].'"><div class="content">'.$content[2].'</div>'
);
  • 写回答

3条回答 默认 最新

  • dongqiangse6623 2014-05-25 05:28
    关注

    Here is the best way for an easy iterator, took me some time but I finally solved it.

    Class Function

    public function iterate($d,$fn){
         foreach($d as $item){
            $txt = str_replace('{author}',$item["author"],$fn);
        $txt = str_replace('{id}',$item["id"],$txt );
        $txt = str_replace('{content}',$item["content"],$txt);
        $txt = str_replace('{date}',$item["date"],$txt);
        echo $txt;
       }
    }
    

    PHP page IE index.php

    $post = $easyCMS->my_query('SELECT * FROM  `newsPost`');
    $easyCMS->iterate($post,'<div class="hello">{author}</div><div id="post_{id}"><div class="content">{content}</div></div>');
    

    $easyCMS->my_query is just a regular query which returns specific information

    my_query

    public function my_query($sql)
    {
         $array=array();//add an array
        $query = mysqli_query($this->connect,$sql);
        if($query > 0){
        $c = mysqli_num_rows($query);//get how many rows there are
        if($c > 1){//if greater than one push into the array
          while($fetch = mysqli_fetch_array($query)){//while loop to push
            array_push($array, $fetch);
          }
          return $array;
        }else{
         return mysqli_fetch_row($query);//rows is only one
        }
        }else{
        return "No such query";//if the query does not exist!
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?