PHP Arrays

its a form to get the info into a db I want to try something like this

mysql_query("INSERT INTO table ($key) VALUES ($value)

using $_POST so i need to get rid of the last comma still.

thx for the info

/edit cool that worked but you still have to unset the old key so when do you want to claim your free beer :D
 
Last edited:
Now if you have a clever way of getting rid of the last comma after the key and value list theres a sixpack in the fridge.
 
Gunny, if it's an array it shouldn't have a comma after the last entry. If you generate the array yourself, instead of trying to take it out after you've generated it, why not make sure not to include it in the array in the first place. Normally when generating my own array, I use the following

arr = "," + value (this is in no way programming language specific and just for explanation purposes)

but before you do that, check if the lenght of the arr is 0, if it is, just do this:

arr = value

That way, when the loop comes around once more for the next value, the lenght would be greater than 0 and the arr = "," + value would be triggered and build the array correctly without you having to get rid of the last comma (because there will be no comma at the end)

Or you could just use a right function, check the last character, and if it's a comma, do a left with the lenght set to the lenght of the string minus 1.
 
Hi Gunny,

to un set a var in php:

Code:
<?php unset ($array ["elm"]) ?>
But since this is a superglobal, php will handle it. I still don't understand where the comma gets in, but here goes - $_POST get's mapped to the following:

$_POST ["var"]=value

So, if you post say username=user and password=pass from a form, you will get:

$_POST ["username"]="user";
$_POST ["password"]="pass";

However, if you do the following:

http://www.domain.com/script.php?username=user&password=pass

the look into the $_REQUEST superglobal. It will break that down like the $_POST one so you get:

$_REQUEST ["username"]="user";
$_REQUEST ["password"]="pass";

Hope that helps !

Laterz !
Gunny said:
Now if you have a clever way of getting rid of the last comma after the key and value list theres a sixpack in the fridge.
 
Gunny said:
Now if you have a clever way of getting rid of the last comma after the key and value list theres a sixpack in the fridge.

For trailing characters that you want to get rid of there are 3 functions that might help:

trim()
ltrim()
rtrim()

These can trim any characters of the end of a string. Have a look at the PHP manual under string functions.

e.g: rtrim($value, ',')

HTH
 
Gunny said:
Now if you have a clever way of getting rid of the last comma after the key and value list theres a sixpack in the fridge.

$string = substr($string,-1);

gets rid of the last character.

Don't drink, but a 6pack Coke will be fine. You can fax it to....

:D
 
Thanx for all the ideas guys i ended up with this

Code:
foreach($_POST as $key => $value)

{
$val .= ("$key,");
$va2 .= ("$value,");
}


$val = substr($val,0,-1);
$va2 = substr($va2,0,-1);

echo("$val<br>$va2");

Now of course I have a new problem because the 2 variables have never been called before i get a funky error saying they not set but it does what I ask it neway
 
Last edited:
Can one of you clever folks help me with this one.

To insert values into MySQL they need to be enclosed in these babies ""
e.g ("value1","value2",...

How do I make PHP do that ???
 
Or you could just do the following...

PHP:
function Q($n){ return("\"$n\""); }
$keys=implode(",",array_keys($_POST));
$vals=implode(",",array_map("Q", $_POST));
echo "$keys $vals";
 
PHP is quite cleva here -

You can say the following:
Code:
$query='insert into table (col) values ("string");'; (Notice the string is in ' ')
or
$query="insert into table (col) value (\"string\");";

Laterz !

Gunny said:
Can one of you clever folks help me with this one.

To insert values into MySQL they need to be enclosed in these babies ""
e.g ("value1","value2",...

How do I make PHP do that ???
 
Tazz_Tux said:
PHP is quite cleva here -

You can say the following:
Code:
$query='insert into table (col) values ("string");'; (Notice the string is in ' ')
or
$query="insert into table (col) value (\"string\");";

Laterz !

Yep - I find it better the second way, because
Code:
$query="insert into table (col) value (\"$stringvalue\");";
will work,
but
Code:
$query='insert into table (col) value ("$stringvalue");';
will actually insert $stringvalue into the table and not the value in the variable.
 
Not always :)

Code:
$query='insert into table (col) value ("'.$stringvalue.'");';

But rather escape them :D

Moederloos said:
Code:
$query='insert into table (col) value ("$stringvalue");';
will actually insert $stringvalue into the table and not the value in the variable.
 
Thanks all

This is the working code learning slowly but getting there.


PHP:
$val = "";
$va2 = "";

foreach($_POST as $key => $value)

{

$val .= ("$key,");
$va2 .= ("\"$value\",");

}

$val = substr($val,0,-1);
$va2 = substr($va2,0,-1);

//echo("$val<BR>$va2<BR>")

$g =("INSERT INTO stock ($val) VALUES ($va2)");

$result = mysql_query($g);
if($result == 0)
 {
     echo "<b>Error ".mysql_errno().": ".mysql_error().
          "</b>";
}

Thanx again

Regards
G
 
Top
Sign up to the MyBroadband newsletter
X