Finding variables from my post data by their names?

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
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Finding variables from my post data by their names?

Post by robster »

Hi all :)

I have a loop that draws part of a form for me, it looks like this:

Code: Select all

<?
	mysql_select_db($dbname);
	
	$sql = "SELECT * FROM tablename ORDER BY id ASC";
	$content = mysql_query($sql);
	$Xcontent = mysql_fetch_array($content);	
	$cShowMax = mysql_num_rows($content);

	for ($y=1; $y<=$cShowMax; $y++)
	{ 
		$id = $Xcontent["id"];
		$present = $Xcontent["present"];

		echo "<input name=\"present\" type=\"checkbox\" value=\"$present\">$present<br>";	
			
	$Xcontent = mysql_fetch_array($content);
	}
	mysql_free_result($content);	
  
?>
This is fine except it gives each and every checkbox the same name (ie: 'present').

What I want is to say, name each checkbox individually, using the $id appended, for example:

Code: Select all

echo &quote;&lt;input name=\&quote;present_$id\&quote; type=\&quote;checkbox\&quote; value=\&quote;$present\&quote;&gt;$present&lt;br&gt;&quote;;
Assuming this works (my code may be a bit out, but in theory it should be fine) I then get individually named checkboxes, which is great, but when I then go to read the post data, how can I know what the names will be? There could be 10 selections, all taken from the database with different $id values appended.

So my question is: How can I pull the individual variables from my post data by their names, if I don't know what the names are? Is there some kind of function to parse the post data, extract names and use them to create variables for use in my php project?

Currently I've been doing it manually like below, but I know it won't work, as I said, I don't know what they will be called, nor how many will be selected:

Code: Select all

$get_present = $_GET['present'];
This one is really tricky for a post data neub like myself, it's an interesting one though, I have to say. :)

Any advice really appreciated,

Rob
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Untested:

Code: Select all

for ($y=1; $y<=$cShowMax; $y++)
    { 
        $get_present = $_GET["present_$x"];
//or
        $get_present = $_GET[${"present_$x"}];

    }
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

It was actually done with a bit of on the sly trickery. I actually used this method:

Code: Select all

1- use the table ID appended to the end of a constant string to create a string that matches the name of the passed variable

Code: Select all

mysql_select_db($dbname);
$sql = "SELECT * FROM table_name ORDER BY id ASC";
$content = mysql_query($sql);
$Xcontent = mysql_fetch_array($content);	
$cShowMax = mysql_num_rows($content);

for ($y=1; $y<=$cShowMax; $y++)
{ 
	$id = $Xcontent["id"];
	$present = $Xcontent["present"];
	
	$get_var_matchup = "present_$id";  //Get the name of the passed variable by recreating it from the $id and a string named 'present_'  ie: present_ + $id

	$get_present_match = $_GET["$get_var_matchup"];  //Use the created name to pull info from the passed variable of the same name
...then on with the loop.


This of course works as I create my variables with the same manor, so the $id from the database will always match the appended _id that gets posted as a variable name.

I hope that makes sense anyway, and I hope it helps someone in the future. This place is a great knowlegebase due to complete threads. (even if they are a bit of skull duggery hacky hackity ;)).

Thanks again hawleyjr, always much appreciated and it got me moving towards my final result.

Rob
Post Reply