Page 1 of 1

[Solved] Editing multiple values PHP

Posted: 19 Aug 2010, 11:44
by DarkRanger
Can anyone help me here.

The best way to explain my request is by giving an example.

I want to do the something similar to what phpmyadmin does. You select all the rows you want to edit by means of checkboxes. Then you click edit and it loads all the values and enables you to edit values.

How do I do that? I made my checkboxes and set the Value to the ID of the corresponding row. But how do I request only those rows from the DB? At the moment it just calls the last row.

Re: Editing multiple values PHP

Posted: 20 Aug 2010, 11:48
by DarkRanger
Ok, so this is how I did it.

I took the post result, then parsed it for all the id's which i read into an array, and then just looped through the array.

The snippet of code should anyone be interested:

Code: Select all

$body = file_get_contents('php://input'); // Get's POST variables
while($body!="") // Check's if control $body is empty
{
	$body=substr($body,strpos($body,"=")+1); // Remove's 'id' string
	if(strlen($body)> 3) // check's if $body has more than 3 characters
		$id=substr($body,0,strpos($body,"&")); //more than one 'id' remains, get 'id'
	else
		$id=$body; // Remaining body = last 'id'
	$body=substr($body,strpos($body,"&")); // Removes last 'id' parsed
	$ids[]=$id; // Adds 'id' to end of array
	$pos= "n".strpos($body,"&"); // Checks if & characters is first
	if(strpos($body,"&") != "n0")
	{
		$body=""; // If not first, id's successfully parsed.
	}
}

Re: Editing multiple values PHP

Posted: 07 Sep 2010, 09:31
by DarkRanger
Interesting fact here, and this is why it is always good to test your web page in as many browsers as possible.

The above fix only works with Chrome. Want to know why? Well, from what I can gather, Firefox and IE sends POST variables as one liners. For example, where Chrome sends post as id=1&id=2 etc, FF and IE sends it as:

Code: Select all

id=1
id=2
So to get it working in FF and IE, you have to parse for special characters, and not the & sign. I did it the other way around though, I changed /n to &, and then parsed and deleted any other special chars it might find. Here is the code snippet:

Code: Select all

$body = file_get_contents('php://input'); // Gets whole POST request
$body = str_replace("\n","&",$body);
$body = str_replace(" ","",$body);
$body = str_replace("\t","",$body);
$body = str_replace("\r","",$body);
$body = str_replace("\0","",$body);
$body = str_replace("\x0B","",$body);

Re: Editing multiple values PHP

Posted: 08 Nov 2010, 13:00
by DarkRanger
DarkRanger wrote:Ok, so this is how I did it.

I took the post result, then parsed it for all the id's which i read into an array, and then just looped through the array.

The snippet of code should anyone be interested:

Code: Select all

$body = file_get_contents('php://input'); // Get's POST variables
while($body!="") // Check's if control $body is empty
{
	$body=substr($body,strpos($body,"=")+1); // Remove's 'id' string
	if(strlen($body)> 3) // check's if $body has more than 3 characters
		$id=substr($body,0,strpos($body,"&")); //more than one 'id' remains, get 'id'
	else
		$id=$body; // Remaining body = last 'id'
	$body=substr($body,strpos($body,"&")); // Removes last 'id' parsed
	$ids[]=$id; // Adds 'id' to end of array
	$pos= "n".strpos($body,"&"); // Checks if & characters is first
	if(strpos($body,"&") != "n0")
	{
		$body=""; // If not first, id's successfully parsed.
	}
}
Is there a easy way to do the above? An easy way to parse a PHP Post request for similar values? Like the post I have is: id=1&id=2&id=3&id=4. Is there an easier way of reading all those values into an array so that print_r($arr) returns Array ([0] => 1, [1] => 2, [2] => 3, [3] => 4)