$_REQUEST['whatever'] questions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

$_REQUEST['whatever'] questions

Post by Saethyr »

Okay I have a list of POST variables that I need to concatenate into one variable for entry into a table cell in a database, the list of variables is C1 Through C55. What would be the best way to concat these variables into one single variable for entry into a database?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

somthing like this might help..

Code: Select all

<?php
foreach($_POST as $key => $value)
{
$var .= $value.', ';
}
echo$var;
?>
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

qads,

I thought about that as well but, there are several Post variables I do not want to concat. A1-A15, B1-B20, C1-C32, D1-D10 each will be concat'd in it's own variable, sorry I know I didn't add that piece of information in the original post. would it be easier just to name the variables like C[] and create an array out of each one?
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Using C[] as the form field name would probably be the best way. If not, maybe this would work?

Code: Select all

<?
foreach($_POST as $key => $value)
{
  $pos = strpos($value, "C")
  if($pos === true) // Note use of ===.  Simply == would not work as expected
  {
    $C[] .= $value.', ';
  }
}
echo$var;
?>
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

Duff,
Thanks bro, I will work with that and see what I can get going.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

An alternate method would be using a for loop:

Code: Select all

$concat_string = '';
for ($i = 1; $i <= 55; $i++) {
    $concat_string .= $_POST['C'.$i];
}
Mac
Post Reply