Amjad
Amjad

Reputation: 2100

Saving dynamic values into database

First my code is below

$address_value_array = array($_POST['rta_pic_pc'], $_POST['rta_pic_house']);
foreach ($address_value_array as $value) {

    Test::save(array(null, $value, null, $value));
}

In my system user can have more than one address to one of my clients profile, therefore when saving I'll have to check if user have entered one two or more than two address in how should i use foreach loop on that occasion to enter value into database.

I've tried with $key value set as well but get errors.

$address_value_array = array($_POST['rta_pic_pc'], $_POST['rta_pic_house']);
foreach ($address_value_array as $key => $value) {

    Test::save(array(null, $value[$key], null, $value[$key]));
}

If you can help me how to insert that values into database

btw rta_pic_pc = Postcode, rta_pic_house = house no

Kind Regards

enter image description here

Upvotes: 1

Views: 152

Answers (3)

Amjad
Amjad

Reputation: 2100

Hello folks I've found solution to my answer

What i did since i had a hidden variable that stores the id of already saved addresses i used that to loop and then catch the value inside the loop if that is not making sense let me put the code down here

HTML:

<?php
    $counter = 1;
    while ($person_address = $person_addresses->fetch(PDO::FETCH_OBJ)) {
        $address_row = Address::findByID($person_address->address_id);

?>

<div class="row">
    <div class="col-md-6">
        <div class="form-group" id="div_pic_pc<?php echo $counter; ?>">
            <label class="control-label">Postcode<span class="required" aria-required="true" > * </span></label>
            <input type="hidden" name="rta_pic_person_add_id[]" value="<?php echo $address_row->id; ?>" />
            <input type="text" value="<?php echo $address_row->postcode; ?>" name="rta_pic_pc<?php echo $counter; ?>" id="rta_pic_pc<?php echo $counter; ?>" class="form-control input-sm" />
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-group" id="div_pic_house<?php echo $counter; ?>">
            <label class="control-label">House Number<span class="required" aria-required="true" > * </span></label>
            <input type="text" value="<?php echo $address_row->house_no; ?>" name="rta_pic_house<?php echo $counter; ?>" id="rta_pic_house<?php echo $counter; ?>" class="form-control input-sm" />
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-6">
        <div class="form-group" id="div_pic_flat<?php echo $counter; ?>">
            <label class="control-label">Flat Number</label>
            <input type="text" value="<?php echo $address_row->flat_no; ?>" name="rta_pic_flat<?php echo $counter; ?>" id="rta_pic_flat<?php echo $counter; ?>" class="form-control input-sm" />
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-group" id="div_pic_street<?php echo $counter; ?>">
            <label class="control-label">Street</label>
            <input type="text" value="<?php echo $address_row->street; ?>" name="rta_pic_street<?php echo $counter; ?>" id="rta_pic_street<?php echo $counter; ?>" class="form-control input-sm" />
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-6">
        <div class="form-group" id="div_pic_town<?php echo $counter; ?>">
            <label class="control-label">Town</label>
            <input type="text" value="<?php echo $address_row->town; ?>" name="rta_pic_town<?php echo $counter; ?>" id="rta_pic_town<?php echo $counter; ?>" class="form-control input-sm" />
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-group" id="div_pic_county<?php echo $counter; ?>">
            <label class="control-label">County</label>
            <input type="text" value="<?php echo $address_row->county; ?>" name="rta_pic_county<?php echo $counter; ?>" id="rta_pic_county<?php echo $counter; ?>" class="form-control input-sm" />
        </div>
    </div>
</div>

<?php
        $counter++;
    endwhile; 
?>

PHP:

$counter = 1;
foreach ($_POST['rta_pic_person_add_id'] as $value) {

    $postcode = htmlspecialchars($_POST['rta_pic_pc' . $counter]);
    $houseno = htmlspecialchars($_POST['rta_pic_house' . $counter]);
    $flatno = htmlspecialchars($_POST['rta_pic_flat' . $counter]);
    $street = htmlspecialchars($_POST['rta_pic_street' . $counter]);
    $town = htmlspecialchars($_POST['rta_pic_town' . $counter]);
    $county = htmlspecialchars($_POST['rta_pic_county' . $counter]);

    Address::save(array($value, $flatno, $houseno, $street, $town, $postcode, $county));

    $counter++;
}

Upvotes: 0

superphonic
superphonic

Reputation: 8074

Given your comments and me not really knowing what your save function is expecting, how about:

Test::save(array(null, $_POST['rta_pic_pc'], null, $_POST['rta_pic_house']));

Upvotes: 1

lopandpe
lopandpe

Reputation: 523

I think that like this you put also the $key, but I am not sure:

$address_value_array = [];
array_push ( $address_value_array , $_POST['rta_pic_pc'], $_POST['rta_pic_house'] );

Upvotes: 1

Related Questions