Jump to content

[SOLVED] Neater / Better Way To Present This Code?


stublackett

Recommended Posts

Hi,

 

I'm working with SESSION and POST Vars for a Purchase Order System, I've included all my code in a file I've called "session_vars.php"

 

The code works..... But theres a damn lot of it, I'm looking for a better way which will help me in the future and obviously now, To present data collected in a much neater or shorter format

 

Any suggestions welcome...

 

Heres my code...

<?php

//Collect Post Vars

//Sender First
$sendertitle = $_POST['sendertitle'];
$senderforename = $_POST['senderforename'];
$sendersurname = $_POST['sendersurname'];
$senderaddress = $_POST['senderaddress'];
$senderaddress1 = $_POST['senderaddress1'];
$sendertown = $_POST['sendertown'];
$sendercounty = $_POST['sendercounty'];
$sendertelephone = $_POST['sendertelephone'];
$senderemail = $_POST['senderemail'];

//Receiever Details
$receievertitle = $_POST['receievertitle'];
$receieverforename = $_POST['receieverforename'];
$receieversurname = $_POST['receieversurname'];
$receieveraddress = $_POST['receieveraddress'];
$receieveraddress1 = $_POST['receieveraddress1'];
$receievertown = $_POST['receievertown'];
$receievercounty = $_POST['receievercounty'];
$receievertelephone = $_POST['receievertelephone'];
$receieveremail = $_POST['receieveremail'];

//Find out from the radio buttons who is receiving the item
$recipient = $_POST['recipient'];

// Set Session Variables
$_SESSION['sendertitle'] = $sendertitle;
$_SESSION['senderforename'] = $senderforename;
$_SESSION['sendersurname'] = $sendersurname;
$_SESSION['senderaddress'] = $senderaddress;
$_SESSION['senderaddress1'] = $senderaddress1;
$_SESSION['sendertown'] = $sendertown;
$_SESSION['sendercounty'] = $sendercounty;
$_SESSION['sendertelephone'] = $sendertelephone;
$_SESSION['senderemail'] = $senderemail;

?>

First of all, you didn't even check the data to make sure it was what you wanted before just putting it in the session variables and probably blindly using it on the next page.  Correct me if I'm wrong.  Secondly, you could always loop through $_POST with a foreach() and check the keys against an array of keys that you want extracted:

<?php
$extract = array('test', 'submit', 'orange');
foreach ($_POST as $key=>$value) {
     if (in_array($key, $extract)) {
               $extracted[$key] = $value;
     }
}
?>

Or something like that.

As long as the $_SESSION array keys are the same as the variable names you assign them to, you can use the following code:

 

<?php
$keys = array( "sendertitle", "senderforename" ); //etc.

if( is_array( $keys ) )
{
     foreach( $keys as $key => $value )
     {
           if( isset( $_SESSION[ $value ] ) )
           {
                  ${$value} = $_SESSION[ $value ]; //or $_POST
           }
     }
}
?>

First of all, you didn't even check the data to make sure it was what you wanted before just putting it in the session variables and probably blindly using it on the next page.  Correct me if I'm wrong.  Secondly, you could always loop through $_POST with a foreach() and check the keys against an array of keys that you want extracted:

<?php
$extract = array('test', 'submit', 'orange');
foreach ($_POST as $key=>$value) {
     if (in_array($key, $extract)) {
               $extracted[$key] = $value;
     }
}
?>

Or something like that.

 

I've got a validation script running using JavaScript on the form, So it prevents NULL and VOID Data from being submitted

 

A loop sounds my best bet, I'll give that a try, Thanks

first of all, i'm not sure why you bother extracting them into local variables - you can just use the $_POST variables directly.  second, if you insist on extracting them to local variables, you can just use:

 

extract($_POST);

 

third, you don't need to bother with extracting.  you can just use one line:

 

$_SESSION = $_POST;

 

that will overwrite any elements already in $_SESSION though, so you could just use:

 

$_SESSION = array_merge($_SESSION, $_POST);

 

finally, if you don't want the receiver stuff in the session, you can use a foreach to strip them out before merging the two arrays:

 

foreach ($_POST AS $key => $val)
{
 if (strpos($key, 'receiver') !== FALSE || $key == 'recipient')
 {
   unset($_POST["{$key}"]);
 }
}

 

voila.  8 lines of code.

 

EDIT:  beaten to the punch.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.