...

View Full Version : PHP arrays question



McNasty
09-21-2005, 10:20 AM
I am using submission from a form to populate an online PDF proof:


if(isset($_POST['Preview'])){

$name = trim($_POST['name']);
$title_one = trim($_POST['title_one']);
$title_two = trim($_POST['title_two']);
$address_one = (trim($_POST['address_one']));
$address_two = (trim($_POST['address_two']));
$address_three = (trim($_POST['address_three']));

$values=$HTTP_POST_VARS;

//Uses values from a post ignores fields that dont match the pdf
$fdfdata = "%FDF-1.2\n%&&Mac178;\n";
$fdfdata .= "1 0 obj \n<< /FDF ";
$fdfdata .= "<< /Fields [\n";

//loop that adds the field names and values
foreach($values as $key=>$val)
{
$fdfdata.="<< /V ($val)/T ($key) >> ";
}
}


What I need to know is:

I need to make some changes to the variables names that are posted before they are processed by the FDF commands.
I'm presuming that I need to take the post variables and put them in an array? Then I need to make $values = the new details.
Problem is I'm not sure how to do this.
Can anyone help?

Badman3k
09-21-2005, 11:32 AM
The line:
$values = $HTTP_POST_VARS;
gets all the posted values and then assigns it to the array $values, so you don't have to.

In the loop:

//loop that adds the field names and values
foreach($values as $key=>$val)
{
$fdfdata.="<< /V ($val)/T ($key) >> ";
}
}
is where it takes each posted value and the name of the field it was entered into and then add's it to the $fdfdata string. So if you want to make any changes to the values it'd be best to do it within in this loop.

You said you want to change the name of the fields, so to do this you'd use something along the lines of:

$key = $key . "_changed";

before the $fdfdata .= "<< ... line, making all the changes you want to the key variable, as this is where the name of the field is stored.

Hope this helps

JamieR
09-21-2005, 12:35 PM
Hasn't $HTTP_POST_VARS been deprecated?

NancyJ
09-21-2005, 12:57 PM
Hasn't $HTTP_POST_VARS been deprecated?

yes... as of 4.1, it still works though.
I dont see the need to post it into another array though, $_POST is an array.

you can use
foreach($_POST as $key =>$val) rather than putting it into a new array first.

The easiest way to change the values of $key is to change the fieldnames on the form.

McNasty
09-21-2005, 01:43 PM
The details are for business card proofs.

The final pdf proof has the field names:

var1, var2, var3, var4 etc..

However users will have different details. eg some will have a mobile some will not.

Before I even go near the fdf I want to be able to sort the submitted details so the form looks neat. eg it understands that nothing has been entered for fax number so moves all the other fields in to a different variable name to cover the missing space that no fax number would leave.
I want to do this and then pass the details to the $values variable

Any ideas?

NancyJ
09-21-2005, 01:58 PM
how about


$i=1;
foreach($values as $key=>$val)
{
//if there is data
if ($val <> ""){
//put the data into the next available 'var' placeholder
$fdfdata.="<< /V ($val)/T (var$i) >> ";
//increment next available place holder i
$i++;
}
}

McNasty
09-21-2005, 06:02 PM
That would be the best choice but some variables need to remain the same and not move depending upon user input.

So if you could please tell me how to replace the $values=$HTTP_POST_VARS;
so i can jig about with some variables and then make
$values=$whatever I do;
I would be very grateful.

I am happy to write a long script of ifelses regarding what to to depending on user input.


The problem is i am a novice at PHP. Although your suggestions are probably brilliant and less time consuming than the way I want to do it, I dont know how to do it any way but my way.

Sorry for being a pain.

Regards,

Tim

NancyJ
09-21-2005, 06:12 PM
That would be the best choice but some variables need to remain the same and not move depending upon user input.
Then surely you should be validating those fields, to make sure they have values assigned to them before preceeding with the preview...

But anyway you could create an exclude list and do something like this:


$i=1;
foreach($values as $key=>$val)
{
//if there is data
$excludepattern = '/^excludevar1$|^excludevar2$/';
if (($val <> "")|| (preg_match($excludepattern, $key))){
//put the data into the next available 'var' placeholder
$fdfdata.="<< /V ($val)/T (var$i) >> ";
//increment next available place holder i
$i++;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum