I have this function which someone here helped me minimize to be more efficient but the problem is that this more efficient code isn't working the way I had it originally.
New Class Function
public function iterate($d,$fn){
foreach($d as $item=>$value){
$txt = str_replace('{'.$value.'}',$item[$value],$fn);
//as well as
// $txt = str_replace('{$value}',$item[$value],$fn);
echo $txt;
}
}
Original Class Function
public function iterate($d,$t,$fn){
foreach($d as $item){
if($t == "post"){
$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;
}
}
}
to Instantiate the function I do
easyCMS->iterate($post,'<div class="post" id="post_{id}">{content}</div><div class="author">{author} on {date}</div>');
The new function outputs this:
<div class="post" id="post_{id}">{content}</div>
<div class="author">{author} on {date}</div>
My original function outputs correctly for example.
<div class="post" id="post_1">Hello World</div>
<div class="author">Mr.EasyBB on 5/28/2014</div>
Am I missing something why would my original work perfectly and not this new smaller more "efficient" code not do the trick?
UPDATE
var_dump($value);
array(12) {
[0]=> string(1) "1" ["id"]=> string(1) "1"
[1]=> string(59) "Hello this is post one testing iterator easyCMS->iterate();" ["content"]=> string(59) "Hello this is post one testing iterator easyCMS->iterate();"
[2]=> string(9) "Mr.EasyBB" ["author"]=> string(9) "Mr.EasyBB"
[3]=> string(10) "2014-05-24" ["date"]=> string(10) "2014-05-24"
[4]=> string(11) "Recent Post" ["category"]=> string(11) "Recent Post"
[5]=> string(26) "html,css,javascript,jquery" ["tags"]=> string(26) "html,css,javascript,jquery"
}
$item[$value]to this$value[$item]in your new class function.$value$txt = $fn; foreach ($value as $k => $v){$txt = str_replace('{'.$k.'}', $v, $txt);}