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 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架