Page 1 of 1

dynamic variables

Posted: Thu Apr 14, 2005 2:08 am
by psychomachine
Hi.

I've used PHP for a while for small projects, but I am now working on my first more ambitious project and I am hopelessly stuck. Perhaps somebody could help me out.

In my code, I have this function:

Code: Select all

function readvar($name) {
   global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS;
   $temp=$_COOKIE[$name];
   if ($_POST[$name]) $temp=$_POST[$name];
   if ($_GET[$name]) $temp=$_GET[$name];
   $temp=str_replace("\\","\\\\",$temp);
   $temp=str_replace("'","\\'",$temp);
   return $temp;
}
So, when I read the values of a submitted form, I just do

Code: Select all

$new['definition'] = readvar('definition');
which is all fine and working very well. But, the problem I'm having is that I sometimes have to deal with getting the value from form inputs which are themselves dynamically created and I don't know the input name ahead of time:

Code: Select all

<input name = word7 value = &quote;dog&quote;>
<input name = word185 value = &quote;cat&quote;>
X in wordX is the id#, i.e. primary key from the database for the given word. So I really need it to update the record if it has been changed.

SOOOO... What is common practice in situations like this? Is there a regex way to handle this? How do I get something like:

Code: Select all

$new['wordX'] = readvar('wordX');
and how do I then separate the X so that I can say:

Code: Select all

q = mysql_query(&quote;update word set word = '$newї'wordX'] where wordID = 'X'&quote;);
Or is there an easier way to deal with it? As I said, this is the first time I have encountered this level of complexity. And I'll be most grateful for any tips or references you may have. Even if you say: "hey, dude, you're a complete moron in the first place, whoever heard of <input name = 'word17'>... :)

All best,
Tench

Posted: Thu Apr 14, 2005 2:39 am
by CoderGoblin
One way of doing it is to store the variable names in an array which is stored in a hidden field and posted with the form, although this is not necessarily secure. If you are using sessions this information would be better stored there. You could then use these and possibly variable variables to handle them ($$variable_name).

I would also look at the global variable $_REQUEST in the manual.

Hope that helps

Posted: Thu Apr 14, 2005 2:44 am
by n00b Saibot
I think may be (totally untested)...

Code: Select all

/\d*$/
will match the last digits in a string.

Posted: Thu Apr 14, 2005 3:30 am
by psychomachine
thanks for your replies, guys.

i'd like to test the regex way of doing it -- and I have been reading the regex pages in the manual, but I still don't get how I could apply in in my particular situation. all the examples i can find are fake in the sense that you have a $string = "blah" and then you do something on it.

but how is regex used realistically? i have a page which is dynamically generated containing, as i said, some input fields that the user can change the contents of:

Code: Select all

&lt;input name = word7 value = &quote;dog&quote;&gt;
&lt;input name = word185 value = &quote;cat&quote;&gt;
now, the user changes the values and presses submit. my code checks if the server request method is post (i.e. whether the page has just loaded, or if the user is submitting changes...) and it is then that I have to look for and match input names.

now, if

Code: Select all

/\d*$/
matches the last digits in a string, I still don't know how to use it. Is there a way to incorporate the regex search into my code:

Code: Select all

$new['wordX'] = readvar('wordX');
i.e. how do i make php execute the above code and look for every instance in which the variable name starts with "word" and ends with a number?

Many, many thanks in advance.
Tench

Posted: Thu Apr 14, 2005 4:33 am
by n00b Saibot
Here is alittle solution-cum-example it put together for you...

Code: Select all

<?
if (isset($_POST)):
$re = "/word(\d*)$/"; //set regular-expression to use
foreach($_POST as $var=>$data) //loop thru POST vars & data
{
if(preg_match($re,$var,$match)) //if found a match
 echo "No.=> {$match[1]} | Field=> {$var} | Value=> {$data} <br />\r\n"; //I print it out ... you do whatever you want 
}
endif;
?>
<form method=POST action="<?=$_SERVER['PHP_SELF']?>">
<input name=test7 value = "dog">
<input name=word7 value = "pup">
<input name=test185 value = "cat">
<input name=word185 value = "pussy">
<input type=submit>
</form>

wow!

Posted: Thu Apr 14, 2005 5:19 am
by psychomachine
perfect!!!!!!! it works, and what's more important, i can actually follow it and understand it! nOOb Saibot -- many, many thanks, indeed!

Posted: Thu Apr 14, 2005 6:24 pm
by feyd
sidenote: do not use the keyword global with superglobal variables.

Posted: Fri Apr 15, 2005 1:52 am
by psychomachine
thanks feyd for pointing that out.