在类函数中轻松迭代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>'
);

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!
    }
}

the error is caused by:

$easyCMS->iterate($post,
   $content[0]."<br>";
   $content[1]."<br>";
   $content[2]."<br>";
   $content[3]."<br>";
   $content[4]."<br>";
);

which should be

$easyCMS->iterate($post,
   $content[0]."<br>",
   $content[1]."<br>",
   $content[2]."<br>",
   $content[3]."<br>",
   $content[4]."<br>"
);

i don't think that this code solves your needs

Can't help but think you're over-complicating things here.

If you're using an array without an index key then it would be as simple as:

public function iterate($d,$fn){
     foreach($d as $content){
        echo $content;
     }
  }

Only if an index is key=>pair do you need to it like:

foreach ($d as $key=>$value) {
stuff//
}


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

Is wrong. When using " and ', you want to wrap ' inside of the ".

If, what you want is to irerate through a loop inside a loop, you'd want something like:

Foreach($post as $pos) {
$class->some_func($pos);
}

public function some_func ($post) {
/formatting.
echo $post;
/formatting.
}

The simplest I can come up with, based on your code currently is:

foreach($stuff_from_database_call as $content) 
echo "formatting stuff". $content . "/close formatting";

Technically you could 1 line it, so long as dont mind using . to join strings :)

Note the lack of [0] [1] etc, which is un-needed, since you are iterating through the array. However, if it was a key=>pair you'd do it like this:

foreach($stuff_from_database_call as $key=>$content) 
echo "formatting stuff". $key[$content] . "/close formatting";

Updated this after you wrote out and accepted your own answer. Instead of:

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;
   }
}

I'd suggest something more like:

public function iterate($d,$fn){
    foreach($d as $item=>$value){
    $txt = str_replace('{$value}',$item[$value],$fn);
    echo $txt;
   }
}

This will make it a LOT more flexible, as you can easily add fields, without having to touch the function itself. When coding, ALWAYS try and do so with as much forethought as you can, so you save yourself headaches down the road.

Either way, glad you got it sorted, and glad you came back to post your sollution.

1 last afterthought. Try naming your variables a little more reader friendly :) $d is nowhere near descriptive enough. Just another avoidable headache, for yourself and for anyone else having to look at your code :)