$results = mysql_query("SELECT * FROM table WHERE id = '$id' ORDER BY time desc LIMIT 3"); while($row = mysql_fetch_array($results)) { $page->replace_tags(array( "ptitle" => $row[title] )); } PHP: I want to repeat ptitle/$row[title] three times on a page. However it only shows once. I'm using a template system, but it only repeats the title i want, once . Here's the HTML and the function replace_tags(); function replace_tags($tags = array()) { if (sizeof($tags) > 0) foreach ($tags as $tag => $data) { $data = (file_exists($data)) ? $this->parse($data) : $data; $this->page = eregi_replace("<!>" . $tag . "<!>", $data, $this->page); } else die("No tags designated for replacement."); } PHP: <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <!>ptitle<!> </body> </html> HTML: Thanks!!!
I think this is correct. ** Original ** $data = (file_exists($data)) ? $this->parse($data) : $data; $this->page = eregi_replace("<!>" . $tag . "<!>", $data, $this->page); PHP: Change like this: $data .= (file_exists($data)) ? $this->parse($data) : $data; $this->page .= eregi_replace("<!>" . $tag . "<!>", $data, $this->page); PHP: or $data[] = (file_exists($data)) ? $this->parse($data) : $data; $this->page[] = eregi_replace("<!>" . $tag . "<!>", $data, $this->page); PHP: depending on how you're using those variables. Sorry, I was a little quick to give you that solution. Let me look a bit closer at this code...
I used this one and got this error $data[] = (file_exists($data)) ? $this->parse($data) : $data; $this->page[] = eregi_replace("<!>" . $tag . "<!>", $data, $this->page); PHP: Maybe I'm oblivious or something, but I'm not very knowledgeable with arrays .
The code's working properly. The fix I gave wouldn't do what you're looking for. Change the code back and try putting in three of those tags. In the meantime, I'll take a closer look at the code. If I find a method that works I'll post it.
I'm not sure what you need, but this code did the assignments. You should be able to implement it. It looks like you'll need to add something like this to your function: Sample: $tag = array( "Title1" => "File one", "Title2" => "File two", "Title3" => "File three", ); $data = "OUy g;uiotgsw <!>Title1<!> bwrb;oiwn; ;o <!>Title2<!> wibn;oinw;oityh;bw <!>Title3<!> w wr'yhijw 'www "; foreach($tag as $t => $v) { switch($cnt) { case 0: $first = preg_replace("/<!>$t<!>/", $v, $data); break; case 1: $sec = preg_replace("/<!>$t<!>/", $v, $first); break; case 2: $page = preg_replace("/<!>$t<!>/", $v, $sec); break; default: break; } $cnt++; } PHP: Results: Before : OUy g;uiotgsw <!>Title1<!> bwrb;oiwn; ;o <!>Title2<!> wibn;oinw;oityh;bw <!>Title3<!> w wr'yhijw 'www After: Here $first = OUy g;uiotgsw File one bwrb;oiwn; ;o <!>Title2<!> wibn;oinw;oityh;bw <!>Title3<!> w wr'yhijw 'www Here $sec = OUy g;uiotgsw File one bwrb;oiwn; ;o File two wibn;oinw;oityh;bw <!>Title3<!> w wr'yhijw 'www Here $page = OUy g;uiotgsw File one bwrb;oiwn; ;o File two wibn;oinw;oityh;bw File three w wr'yhijw 'www Notice how the variable page has all three assignments. This switch takes your three variables and as they come and assigns the first round to the $first storage variable which is then used in the second round and then that's used in the final assignment to the $page variable. You get the idea. An example: function replace_tags($tags = array()) { if (sizeof($tags) > 0) { $cnt = 0; foreach ($tags as $tag => $data) { $data = (file_exists($data)) ? $this->parse($data) : $data; //$this->page = eregi_replace("<!>" . $tag . "<!>", $data, $this->page); switch($cnt) { case 0: $first = preg_replace("/<!>$tag<!>/", $data, $data); break; case 1: $sec = preg_replace("/<!>$tag<!>/", $data, $first); break; case 2: $this->page = preg_replace("/<!>$tag<!>/", $data, $sec); break; default: break; } $cnt++; } } } else { die("No tags designated for replacement."); } PHP:
OMG! Wow! Thanks, if this works, I may have to pay you for your time . Once I'm done with work, I'll test it and report back . How would I integrate MySQL results into this though? Say I want this: $tag = array( "Title1" => "result one", "Title2" => "result two, "Title3" => "result three", ); PHP: