-1
\$\begingroup\$

So on my checkout page when I push final checkout I want to store my users address and shipping address into a database. I am wondering if this could be shorter. Currently, users have multiple options:

  1. Logged in where they use same or other shipping address.
  2. Not logged in and they want to create account with same or different shipping address.
  3. Not logged in where they create a guest account again with same or different address.

I know I can put my $_POST values direct in the $createlines but its just for making it clear now as I am still coding it.

Just wondering if theres some sort of function to get all values and fields from my database.

if (isset($_POST['final_checkout'])) {
$createuser = new User();
$createaddress = new Address();
$createshipping = new Shipping_address();




   if (!empty($logged_in)) {
        /*als same addres is aangevinkt bij ingelogde user*/
        if ($_POST['shipping'] == 'same' ) {

            $oldaddress = trim($_POST['oldaddress']);
            $oldcity = trim($_POST['oldcity']);
            $oldpostal = trim($_POST['oldpostal']);
            $oldcountry = trim($_POST['oldcountry']);

            $createshipping->user_id = $logged_in->id;
            $createshipping->address = $oldaddress;
            $createshipping->city = $oldcity;
            $createshipping->postal = $oldpostal;
            $createshipping->country = $oldcountry; //in tabel addresses nog kolom country maken en dan inputfield ook readonly stap 1

        /*als different adres is aangevinkt bij ingelogde user*/
        } else{

            $othershippingaddress = trim($_POST['create_otheraddress']);
            $othershippingcity = trim($_POST['create_othercity']);
            $othershippingpostal = trim($_POST['create_otherpostal']);
            $othershippingcountry = trim($_POST['create_othercountry']);
            $createshipping->user_id = $logged_in->id;
            $createshipping->address = $othershippingaddress;
            $createshipping->city = $othershippingcity;
            $createshipping->postal = $othershippingpostal;
            $createshipping->country = $othershippingcountry;
        }/*end else shippingaddress*/
    /*if user is !logged in*/
    }else{
        /*create account*/
        if ($_POST['optradio'] == 'create') {
            /*variabelen maken van de post*/
            $newusername = trim($_POST['create_username']);
            $newpassword = trim($_POST['create_password']);
            $newfirst = trim($_POST['create_first']);
            $newlast = trim($_POST['create_last']);
            $newmail = trim($_POST['create_mail']);
            $newaddress = trim($_POST['create_address']);
            $newcity = trim($_POST['create_city']);
            $newpostal = trim($_POST['create_postal']);
            $newcountry = trim($_POST['create_country']);
            /*usertabel invullen*/
            $createuser->username = $newusername;
            $createuser->first_name = $newfirst;
            $createuser->last_name = $newlast;
            $createuser->password = $newpassword;
            $createuser->user_mail = $newmail;
            $createuser->user_image = 'guest';
            $createuser->role_id = '4';
            /*adresses tabel invullen*/
            $createaddress->address = $newaddress;
            $createaddress->postal = $newpostal;
            $createaddress->city = $newcity;
            $createaddress->country = $newcountry;
            $createaddress->user_id = '';

            /*shipping adres same or different in de create account*/
            if ($_POST['shipping'] == 'same') {
                /*als same addres is aangevinkt bij nieuwe user*/
                $oldaddress = $newaddress;
                $oldcity = $newcity;
                $oldpostal = $newpostal;
                $oldcountry = $newcountry;
                /*shipping addres invoeren*/
                $createshipping->user_id = $createuser->id;
                $createshipping->address = $oldaddress;
                $createshipping->city = $oldcity;
                $createshipping->postal = $oldpostal;
                $createshipping->country = $oldcountry; //in tabel addresses nog kolom country maken en dan inputfield ook readonly stap 1


            } else{
                /*als different adres is aangevinkt bij nieuwe user*/
                $othershippingaddress = trim($_POST['create_otheraddress']);
                $othershippingcity = trim($_POST['create_othercity']);
                $othershippingpostal = trim($_POST['create_otherpostal']);
                $othershippingcountry = trim($_POST['create_othercountry']);
                /*shipping addres invoeren*/
                $createshipping->user_id = $logged_in->id;
                $createshipping->address = $othershippingaddress;
                $createshipping->city = $othershippingcity;
                $createshipping->postal = $othershippingpostal;
                $createshipping->country = $othershippingcountry;
            }/*end else different addres*/
        }else{
            /*als guestaccount is aangevinkt*/
            $guestnumber = User::number_guest();
            $newusername = 'guest' . $guestnumber;
            $newpassword = '';
            $newfirst = trim($_POST['create_first']);
            $newlast = trim($_POST['create_last']);
            $newmail = trim($_POST['create_mail']);
            $newaddress = trim($_POST['create_address']);
            $newcity = trim($_POST['create_city']);
            $newpostal = trim($_POST['create_postal']);
            $newcountry = trim($_POST['create_country']);

            /*usertabel invullen*/
            $createuser->username = $newusername;
            $createuser->first_name = $newfirst;
            $createuser->last_name = $newlast;
            $createuser->password = $newpassword;
            $createuser->user_mail = $newmail;
            $createuser->user_image = 'guest';
            $createuser->role_id = '4';
            /*hier al saven om id te krijgen*/
            /*adresses tabel invullen*/
            $createaddress->address = $newaddress;
            $createaddress->postal = $newpostal;
            $createaddress->city = $newcity;
            $createaddress->country = $newcountry;
            $createaddress->user_id = '';


            if ($_POST['shipping'] == 'same') {
                /*als same addres is aangevinkt bij guest*/
                $oldaddress = $newaddress;
                $oldcity = $newcity;
                $oldpostal = $newpostal;
                $oldcountry = $newcountry;
                /*shipping addres invoeren*/
                $createshipping->user_id = $createuser->id;
                $createshipping->address = $oldaddress;
                $createshipping->city = $oldcity;
                $createshipping->postal = $oldpostal;
                $createshipping->country = $oldcountry; //in tabel addresses nog kolom country maken en dan inputfield ook readonly stap 1


            } else{
                /*als different adres is aangevinkt bij guest*/
                $othershippingaddress = trim($_POST['create_otheraddress']);
                $othershippingcity = trim($_POST['create_othercity']);
                $othershippingpostal = trim($_POST['create_otherpostal']);
                $othershippingcountry = trim($_POST['create_othercountry']);
                /*shipping addres invoeren*/
                $createshipping->user_id = $createuser->id;
                $createshipping->address = $othershippingaddress;
                $createshipping->city = $othershippingcity;
                $createshipping->postal = $othershippingpostal;
                $createshipping->country = $othershippingcountry;
            }/*end elseif (guestaccount ->sameaddres)*/
        }/*end else (guestaccount)*/




    } /*end else (!logged in)*/
}/*end isset post*/
\$\endgroup\$
13
  • \$\begingroup\$ Your code indentation seems to be unbalanced (starting here: if (!empty($logged_in)){), can you fix that please. \$\endgroup\$ Jan 25, 2020 at 16:25
  • \$\begingroup\$ just checked but indention seems ok to me the if ´(!empty($logged_in)){` is the start and everything is supposed to be in it or what u want me to do exactly \$\endgroup\$
    – wjdev
    Jan 25, 2020 at 16:36
  • \$\begingroup\$ reformatted with the help of php storm so should be good no thx for reply tho \$\endgroup\$
    – wjdev
    Jan 25, 2020 at 16:40
  • \$\begingroup\$ There's still a hanging } as you can see. That should be aligned with the code formatted section. It's either one too much from a missing opening brace, or you missed the code balance correctly. \$\endgroup\$ Jan 25, 2020 at 16:48
  • 1
    \$\begingroup\$ You should implement methods to set a bulk of properties for the various instances you're populating. \$\endgroup\$ Jan 25, 2020 at 18:13

1 Answer 1

1
\$\begingroup\$

In a sutuation when a block of code is present in both branches of an if statement, you can move this common part out of this statement. for example,

if (some condition) {
    $a = $x;
    $b = $y;
    $a = $z;
} else {
    $a = $x;
    $b = $y;
    $a = null;
}

it can be rewritten to

$a = $x;
$b = $y;
if (some condition) {
    $a = $z;
} else {
    $a = null;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.