royalflush5
royalflush5

Reputation: 57

Generating an HTML table from an array saved in $_SESSION in PHP

I have an array saved to a session (which, I know isn't ideal, but its what I have to work with), with a form that lets me add "cars" to it, and it generates a random string to identify each separate car. Heres what I'm using to save the car, after the user fills out the form:

    $car = new carModel;
    $car->setMake($_POST['make']);
    $car->setModel($_POST['model']);
    $car->setYear($_POST['year']);
    $car->save();

And the save() function I'm using:

private $guid;  
public function __construct() {
  session_start();
  # get a random string and set it guid
  $this->guid = uniqid();
}
public function save() {
  # save the data passed to this into the session, use $guid as an identifier to look up later
  $_SESSION[$this->guid] = (array) $this;
}

So after that happens, I get this array:

session array

What I really have no idea what to do is essentially do it backwards, and take the data from $_SESSION and make a form that has the value field set to the data for each car. I've tried using foreach a couple of times, but I can't seem to get it right. Then I also get to the problem of saving it. I think I'd have to write a new save function, what do you think?

Upvotes: 0

Views: 141

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50787

You can't re-reference that GUID value from the session because you have no idea what it is. But you can change how the session stores those array's so you can more easily access them.

In order to achieve this we need to add another index to our multi-dimensional array.

$_SESSION['cars'][$this->guid]

Now we have a cars variable in our session which should be easy enough to loop over:

foreach($_SESSION['cars'] as $guid => $car){
    echo '<input type="text" name="cars['.$guid.'][\'carModelmake\']" value="'.$car['carModelmake'].'"/><br/>';
    echo '<input type="text" name="cars['.$guid.'][\'carModelmodel\']" value="'.$car['carModelmake'].'"/>';
    echo '<input type="text" name="cars['.$guid.'][\'carModelyear\']" value="'.$car['carModelmake'].'"/>';
}

This will loop over each car from the cars array in the session and make a group of input fields with each car make and model. Now we can loop over cars in your $_POST array

foreach($_POST['cars'] as $guid => $car){
    $_SESSION['cars'][$guid]['carModelmake'] = $car['carModelmake'];
    $_SESSION['cars'][$guid]['carModelmodel'] = $car['carModelmodel'];
    $_SESSION['cars'][$guid]['carModelyear'] = $car['carModelyear'];
} 

Notice above we're not actually using your class, it needs to be modified because it generates a new GUID each time, let's see how we fix this:

public function __construct($guid = false){
    $this->guid = isset($guid) ? $guid : uniqid();
}

public function save(){
    //note we have another index of 'cars' here now
    $_SESSION['cars'][$this->guid] = (array) $this;
}

This will allow us to pass an existing guid to the constructor function or generate a new one if we're creating a new entry. Now we can change how our loop over our post data will look:

foreach($_POST['cars'] as $guid => $car){
    $carModel = new carModel($guid);
    $carModel->setMake($car['carModelmake']);
    $carModel->setModel($car['carModelmodel']);
    $carModel->setYear($car['carModelyear']);

    $carModel->save();
} 

Hope this helps you.

Upvotes: 1

Related Questions