Jump to content

Accessing $_REQUEST info


tomccabe

Recommended Posts

Hi,

 

This is probably going to be a stupidly simple question to answer...

 

I've got a script to reorder values in a table by dragging/dropping. The script works fine, but at this point I have to hard code the name of the table into the script. The table name comes through via the $_GET or $_REQUEST superglobal. My problem lies in the fact that multiple tables need to use this script, but the tables are dynamically created, so I need to be able to tell the script to use whatever table comes in from the superglobal. Here's my script which has one table hard coded into it:

 

<?php // If reorder is submitted 
if (isset($_POST['update'])) {
	$size = $_POST['how_many'];
	for ($k=1; $k<=$size; $k++) {
		$old = $_POST[$k];
		$query = "SELECT cat_id FROM categories WHERE position='$old'";
		$result = mysql_query($query, $connection);	
		confirm_query($result);
		$catids[$k] = mysql_fetch_row($result);
	}
	$i = 1;
	while ($i <= $size) {
		$new_value = $i;
		$id = $catids[$i][0];
		$query = "UPDATE categories SET position='$new_value' WHERE cat_id='$id' LIMIT 1";
		$result = mysql_query($query, $connection);
		confirm_query($result);
		$i++;
	}
	go_to("categories.php?redir=reorder");
}
?>
<br />
<form name="update_position" method="post" action="reorder.php">
<?php // Runs on reorder drag
$result = $_REQUEST["cat_table"];
$count = count($result);
$how_many = $count - 1;
for ($j=1; $j<=$how_many; $j++) {
	$old = $result[$j];
	$new_value = $j;
	echo "<input type=\"hidden\" name=\"$new_value\" value=\"$old\" />";
}
echo "<input type=\"hidden\" name=\"how_many\" value=\"$how_many\" />";
?>
<input type="hidden" name="update" value="yes" />
<input type="submit" value="Update Order" />
<input type="button" name="Cancel" value="Cancel" onclick="window.location='categories.php'" />
</form>

 

A var_dump of the $_REQUEST variable on drag/drop produces these results:

 

array(2) { ["cat_table"]=> array(6) { [0]=> string(0) "" [1]=> string(1) "2" [2]=> string(1) "1" [3]=> string(1) "3" [4]=> string(1) "4" [5]=> string(1) "5" } ["PHPSESSID"]=> string(32) "ddbea61cfd7f94763e03be50112369e4" }

 

What I need to do is in the line

 

$result = $_REQUEST["cat_table"];

 

set $result equal to whatever the table accessing the script is without having to refer to it by name because it may be different all the time based on what info is in the database. I googled around and couldn't find anyone explaining how to access superglobal data from array notation (which I assume is how to get it), nor could I figure it out by messing around with stuff like $_REQUEST[0][0], etc.

 

Sorry if this is a dumb thing to ask. It seems so straightforward I'm sort of embarrassed that I can't figure it out!  :wtf:

Link to comment
https://forums.phpfreaks.com/topic/196661-accessing-_request-info/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.