PHP Post Variable Variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
serfczar_
Forum Commoner
Posts: 34
Joined: Sun Feb 25, 2007 5:27 pm
Location: USA, Texas
Contact:

PHP Post Variable Variable

Post by serfczar_ »

I want to create a php post variable variable and i'm doing something wrong I guess, could someone tell me what it is?

Code: Select all

function replace($var, $content) {
    $varname = "replace_".$var;
    if($_POST[$varname] == ""){
   $this->template = str_replace("#$var#", $content, $this->template);
   $_POST[$varname] = "$content";
   }
   else{
      $this->template = str_replace($_POST[$varname], $content, $this->template);
      }
  }
  function whileiterationcnt($var){
  $varname = "post_".$var;
  if($_POST[$$varname] != ""){
  ++$_POST[$$varname];
  $this->replace($var, $_POST[$$varname]);
  }
   else{
    $_POST[$$varname] = 1;
    $this->replace($var, $_POST[$$varname]);
   }
  }
Last edited by serfczar_ on Sat Mar 03, 2007 6:04 pm, edited 1 time in total.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Perhaps explaining what you want to accomplish, and providing us with what is working/not working can help us help you.

"I did something to my car and it's not working, what did i do wrong?"
User avatar
serfczar_
Forum Commoner
Posts: 34
Joined: Sun Feb 25, 2007 5:27 pm
Location: USA, Texas
Contact:

Post by serfczar_ »

nickvd wrote:Perhaps explaining what you want to accomplish, and providing us with what is working/not working can help us help you.

"I did something to my car and it's not working, what did i do wrong?"
I did tell you what is working/not working when I said I wanted to know how to make a "php post variable variable"


"I put a new carbourator on my car, suddenly it doesn't want to start, I must've installed it wrong. Can someone please help me with my carbourator installation?"
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Okay, my mistake... could you please elaborate on the meaning of "php post variable variable"

I know what a variable variable, but I'm not too sure what a php post variable variable is.

What happens (or doesn't happen) when you run your function/script? Are there any errors or warnings? What would an example use of this function be?

I'm going to take a wild stab at this however. You want to turn $_POST['somevar'] into $somevar. Is this correct?

(yes i understand that i could just look at the function(s) you posted, but I want to know what the end goals are before reading what you've already written)
User avatar
serfczar_
Forum Commoner
Posts: 34
Joined: Sun Feb 25, 2007 5:27 pm
Location: USA, Texas
Contact:

Post by serfczar_ »

nickvd wrote:Okay, my mistake... could you please elaborate on the meaning of "php post variable variable"

I know what a variable variable, but I'm not too sure what a php post variable variable is.

What happens (or doesn't happen) when you run your function/script? Are there any errors or warnings? What would an example use of this function be?

I'm going to take a wild stab at this however. You want to turn $_POST['somevar'] into $somevar. Is this correct?

(yes i understand that i could just look at the function(s) you posted, but I want to know what the end goals are before reading what you've already written)
ok, here is the thing, I want to know every iteration of a while statement like this...

Code: Select all

$i = 0;
while($i<30){
$n = 0;

	while($n<20){
	$template->whileiterationcnt("seconditeration");
	++$n;
	}
$template->whileiterationcnt("firstiteration");
++$i;

}
those functions are in a class file called template.php.class(so don't freak out about it)
the end result is

Code: Select all

222
2
i know this number has to be wrong.

the php post variable variable is I want to name a superglobal post variable a variables name like this...

Code: Select all

$var = "varnametonamepost";
$_POST[$var];
but I don't think i'm doing it right.
i tried it two different ways in my functions and both ways seem to produce some sort of output, I don't think either way it right though.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

serfczar_ wrote:ok, here is the thing, I want to know every iteration of a while statement like this...

Code: Select all

$i = 0;
while($i<30){
$n = 0;

	while($n<20){
	$template->whileiterationcnt("seconditeration");
	++$n;
	}
$template->whileiterationcnt("firstiteration");
++$i;

}
those functions are in a class file called template.php.class(so don't freak out about it)
the end result is

Code: Select all

222
2

i know this number has to be wrong.
Not knowing what $template->whileiterationcnt() does, I am unable to agree or disagree. I am however curious as to why you're pre-incrementing instead of post-incrementing.
serfczar_ wrote:
the php post variable variable is I want to name a superglobal post variable a variables name like this...

Code: Select all

$var = "varnametonamepost";
$_POST[$var];
but I don't think i'm doing it right.
i tried it two different ways in my functions and both ways seem to produce some sort of output, I don't think either way it right though.
Assuming that there is a post variable named 'varnametonamepost' then yeah, it will work fine like that, just make sure that $var is in the correct scope and you shouldn't have any problems...
User avatar
serfczar_
Forum Commoner
Posts: 34
Joined: Sun Feb 25, 2007 5:27 pm
Location: USA, Texas
Contact:

Post by serfczar_ »

nickvd wrote:
serfczar_ wrote:ok, here is the thing, I want to know every iteration of a while statement like this...

Code: Select all

$i = 0;
while($i<30){
$n = 0;

	while($n<20){
	$template->whileiterationcnt("seconditeration");
	++$n;
	}
$template->whileiterationcnt("firstiteration");
++$i;

}
those functions are in a class file called template.php.class(so don't freak out about it)
the end result is

Code: Select all

222
2

i know this number has to be wrong.
Not knowing what $template->whileiterationcnt() does, I am unable to agree or disagree. I am however curious as to why you're pre-incrementing instead of post-incrementing.
serfczar_ wrote:
the php post variable variable is I want to name a superglobal post variable a variables name like this...

Code: Select all

$var = "varnametonamepost";
$_POST[$var];
but I don't think i'm doing it right.
i tried it two different ways in my functions and both ways seem to produce some sort of output, I don't think either way it right though.
Assuming that there is a post variable named 'varnametonamepost' then yeah, it will work fine like that, just make sure that $var is in the correct scope and you shouldn't have any problems...

o my gosh man, i posted the two functions the first time right off the bat!
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

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.
User avatar
serfczar_
Forum Commoner
Posts: 34
Joined: Sun Feb 25, 2007 5:27 pm
Location: USA, Texas
Contact:

Post by serfczar_ »

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."<?");
   }
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are these doing what you'd expect?

Code: Select all

<?php
// This functions does ...
function replace($call, $content) {
    // Set a function scope variable that prepends 
    // the string replace_ to the param $call
    $varname = "replace_" . $call;
    
    // Append the param $call to the param $content
    $content .= $call;
    
    // Let assume we have a POST var, so it is totally 
    // OK to not check if it is set or not
    // Who really cares about undefined index warnings anyway?
    if($_POST[$varname] == "") {
        // Set the value of the class var template to a replaced 
        // version of call and content 
        $this->template = str_replace("#$call#", $content, $this->template);
        
        // Set the value of the post var $varname to the new content
        // making sure to tell PHP to parse the var as part of a string
        $_POST[$varname] = "$content";
    } else {
        // If the post variable varname was not empty 
        $this->template = str_replace($_POST[$varname], $content, $this->template);
    }
}

function whileiterationcnt($call) {
    // Set a function scope variable called varname
    $varname = "post_" . $call;
    
    // Again, lets make some assumptions about post varname being set
    if($_POST[$varname] != "") {
        // Incrememnt the post var varname by one
        // regardless of type
        $_POST[$varname]++;
        
        // Call our replace function
        $this->replace($call, $_POST[$varname]);
    } else {
        // Create a post varname value
        $_POST[$varname] = 1;
        
        // And call our replace function here as well
        // since this is called regardless of the conditonal
        // value, wouldn't it make send to put it outside
        // of the if?
        $this->replace($call, $_POST[$varname]);
    }
}
?>
User avatar
serfczar_
Forum Commoner
Posts: 34
Joined: Sun Feb 25, 2007 5:27 pm
Location: USA, Texas
Contact:

Post by serfczar_ »

Everah wrote:Are these doing what you'd expect?

Code: Select all

<?php
// This functions does ...
function replace($call, $content) {
    // Set a function scope variable that prepends 
    // the string replace_ to the param $call
    $varname = "replace_" . $call;
    
    // Append the param $call to the param $content
    $content .= $call;
    
    // Let assume we have a POST var, so it is totally 
    // OK to not check if it is set or not
    // Who really cares about undefined index warnings anyway?
    if($_POST[$varname] == "") {
        // Set the value of the class var template to a replaced 
        // version of call and content 
        $this->template = str_replace("#$call#", $content, $this->template);
        
        // Set the value of the post var $varname to the new content
        // making sure to tell PHP to parse the var as part of a string
        $_POST[$varname] = "$content";
    } else {
        // If the post variable varname was not empty 
        $this->template = str_replace($_POST[$varname], $content, $this->template);
    }
}

function whileiterationcnt($call) {
    // Set a function scope variable called varname
    $varname = "post_" . $call;
    
    // Again, lets make some assumptions about post varname being set
    if($_POST[$varname] != "") {
        // Incrememnt the post var varname by one
        // regardless of type
        $_POST[$varname]++;
        
        // Call our replace function
        $this->replace($call, $_POST[$varname]);
    } else {
        // Create a post varname value
        $_POST[$varname] = 1;
        
        // And call our replace function here as well
        // since this is called regardless of the conditonal
        // value, wouldn't it make send to put it outside
        // of the if?
        $this->replace($call, $_POST[$varname]);
    }
}
?>
The code is all working correctly, as I've said before, there can be more than one call to these functions at once, so str_replace is replacing the wrong number sometimes.
I need to figure out a way to make str_replace, replace the number that corresponds to the call.
Post Reply