nickvd wrote:What does $_POST look like?
I'm trying to understand why you're using variable variables while accessing $_POST data...
Code: Select all
if($_POST[$$varname] != ""){
++$_POST[$$varname]; // this is even weirder...
$this->replace($var, $_POST[$$varname]);
/...
I hate asking this again, but I'm still nowhere even close to understanding what you want out of this... I'm not one to read code to understand programming questions.
You have this data..... (....)
You preform certain actions (your code that you posted)
You get a result that you expect (what is the intended result?)
I'm don't want to know what your code already does, I want to know what YOU WANT IT TO DO
If you have many post variables named 'post_this, post_that', you dont need to use variable variables to access them.
Code: Select all
$varname = 'that';
$var = 'post_'.$varname;
$theVariable = $_POST[$var];
Try explaining what you want without using 'variable variable' as they can be very tricky to diagnose and troubleshoot.
Here are the new functions, I figured out it is a problem with my string replace. I am calling the whileiterationcnt function twice, and these to calls can end up with the same number output, resulting in the wrong digit str_replaced(); I'm guessing I'll have to make an array of some invisible html characters to tell the digits apart.
Code: Select all
function replace($call, $content) {
$varname = "replace_".$call;
$content .= $call;
if($_POST[$varname] == ""){
$this->template = str_replace("#$call#", $content, $this->template);
$_POST[$varname] = "$content";
}
else{
$this->template = str_replace($_POST[$varname], $content, $this->template);
}
}
function whileiterationcnt($call){
$varname = "post_".$call;
if($_POST[$varname] != ""){
$_POST[$varname]++;
$this->replace($call, $_POST[$varname]);
}
else{
$_POST[$varname] = 1;
$this->replace($call, $_POST[$varname]);
}
}
What I want the code to do is count each while iteration, (no particular reason why, I just wanted to see if it could be done) and have many nested while's with the same functionality as having one.
Code: Select all
$i = 0;
while($i<30){
$n = 0;
while($n<20){
$template->whileiterationcnt("seconditeration");
$n++;
}
$template->whileiterationcnt("firstiteration");
$i++;
}
In each while loop it calls whileiterationcnt, sets the name value of the whileiterationcnt to specified within the function called (example "firstiteration"), the variable is used to distinguish itself from the rest of the pack by using a different variable name. The whileiterationcnt increments each time the function is called. The problem is that str_replace is replacing the wrong digit if I have more than one call to whileiterationcnt. This is actually all done like a template like this....
(Main File)
Code: Select all
<?php
include ('template.class.php');
$template = new Template;
$template->load("home.html");
$i = 0;
while($i<30){
$n = 0;
while($n<20){
$template->whileiterationcnt("seconditeration");
$n++;
}
$template->whileiterationcnt("firstiteration");
$i++;
}
$template->publish();
?>
(html file)
Code: Select all
<html>
<head>
</head>
<body>
#firstiteration#<br>
#seconditeration#<br>
</body>
</html>
(template engine file)
Code: Select all
<?
class Template {
public $template;
function load($filepath) {
$this->template = file_get_contents($filepath);
}
function replace($call, $content) {
$varname = "replace_".$call;
$content .= $call;
if($_POST[$varname] == ""){
$this->template = str_replace("#$call#", $content, $this->template);
$_POST[$varname] = "$content";
}
else{
$this->template = str_replace($_POST[$varname], $content, $this->template);
}
}
function whileiterationcnt($call){
$varname = "post_".$call;
if($_POST[$varname] != ""){
$_POST[$varname]++;
$this->replace($call, $_POST[$varname]);
}
else{
$_POST[$varname] = 1;
$this->replace($call, $_POST[$varname]);
}
}
function publish() {
eval("?>".$this->template."<?");
}
}
?>