Jump to content


Photo

Splitting variables' names


  • Please log in to reply
3 replies to this topic

#1 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 05 August 2006 - 08:45 AM

Hiya!

I'm almost there with a script, but I'm having trouble with splitting a variable name into numbers and letters.

I'm using "explode" to chop up posted variables like this:
foreach($_POST as $k=>$v)
	{
		if (($k !="Submit") &&($v!="Submit"))
		{
			$explode=explode('|', $k, 2));
Each field that is being posted is automatically generated from a database and is called something like this: 14|format

There's also 1|weight_min etc.

The point is that I want to chop up the keys into the number and the word so that I can use the number as an id field in the database, to update the relating field.
I hope that makes sense.

I've used explode so-far, but I'm not sure how to execute the SQL query, or how to access the first or second part of the explode array. (Is it even an array!?)

Please help.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#2 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 05 August 2006 - 08:58 AM

Try:

$array = explode('|', $row['k'], 2);
$k = $array[0];
$number = $array[1];

#3 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 05 August 2006 - 09:08 AM

it's cool... I got it:

if ($_REQUEST['change']=='y')
{
	foreach($_POST as $k=>$v)
	{
		if (($k !="Submit") &&($v!="Submit"))
		{
			$explode=explode('|', $k, 2);
  			$q = "UPDATE `postage` SET `".$explode[1]."` = '".$v."' WHERE `id` =".$explode[0]." LIMIT 1 ;";
			$rs = mysql_query($q) or die("Problem with query: $q<br>" . mysql_error());
		}
	}
}

I did it all by myself too... how proud I am!!
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#4 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 05 August 2006 - 09:09 AM

oh, sorry hostfreak, didn't see your reply there. thanks for that.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users