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.
[Solved] Editing multiple values PHP
-
- Registered User
- Posts: 8346
- Joined: 10 May 2006, 02:00
- Processor: Intel i5-3750
- Motherboard: Gigabyte
- Graphics card: nVidia GTX 550Ti
- Memory: 8GB Jetram
- Contact:
[Solved] Editing multiple values PHP
Last edited by DarkRanger on 19 Jan 2011, 08:51, edited 1 time in total.
-
- Registered User
- Posts: 8346
- Joined: 10 May 2006, 02:00
- Processor: Intel i5-3750
- Motherboard: Gigabyte
- Graphics card: nVidia GTX 550Ti
- Memory: 8GB Jetram
- Contact:
Re: Editing multiple values PHP
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:
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.
}
}
-
- Registered User
- Posts: 8346
- Joined: 10 May 2006, 02:00
- Processor: Intel i5-3750
- Motherboard: Gigabyte
- Graphics card: nVidia GTX 550Ti
- Memory: 8GB Jetram
- Contact:
Re: Editing multiple values PHP
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:
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:
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
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);
-
- Registered User
- Posts: 8346
- Joined: 10 May 2006, 02:00
- Processor: Intel i5-3750
- Motherboard: Gigabyte
- Graphics card: nVidia GTX 550Ti
- Memory: 8GB Jetram
- Contact:
Re: Editing multiple values PHP
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)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. } }