0

hope we are having a great week of coding and experimenting!

I'm working on a CMS-like project and need some help to get my data storage working properly. The problem at the moment is that I'm not sure if I should be using PHP objects, arrays, or both, and I'm not too sure of how to go about using them in an instance like this. (I'm kinda new to php / mysql and some guidance and examples would be great!)

So, I have a db called "Project X" with a table called "users". I've created a column named "UserData" in which I intend to store a user's page settings and content.

Currently, the user types names of the "pages" they want in a CSV-formatted input field, which gets saved to their UserPages field in the db and then for each page, a form is displayed like so:

$UserPages = null;
if ($stmt = $con->prepare("SELECT UserPages FROM users WHERE Username=?")) {
    $stmt->bind_param("s", $Username);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = mysqli_fetch_assoc($result);
    $UserPages = $row['UserPages'];
    $Pages = explode(',', $UserPages);
    while($data = mysqli_fetch_array($result)){
        $UserPages .= $data['UserPages'];
        $badchars = array("[", "]","'" );
        $UserPages = str_replace($badchars , '', $UserPages);
    }
}
foreach ($Pages as $Page) {
    echo "<form action=" .$_SERVER['PHP_SELF']. " method='POST' id=" . $Page . ">";
    $PageForm = "<h3>" . $Page . "</h3>
    <hr/>

        <h4>Title: </h4><input type='text' name='".$Page."[]' value='".$UserData['Title']."'><br/>
        <h4>Subtitle: </h4><input type='text' name='".$Page."[]' value='".$UserData['Subtitle']."'><br/>
        <input type='submit' value='Save' name='".$Page."' id='save'>";
    print_r($PageForm);
    echo "</form>";

    }

?>

The forms display like so:

<form action="/phplogin/v2/edit.php" method="POST" id="Home">
    <h3>Home</h3>
    <hr>
    <h4>Title: </h4><input type="text" name="Home[]" value="Title"><br>
    <h4>Subtitle: </h4><input type="text" name="Home[]" value="Subtitle"><br>
    <input type="submit" value="Save" name="Home" id="save">
</form>

Now, this does work (kinda), but I need to correctly (and dynamically) get and store the data from these various "Pages" and store them as arrays within an object, which is then JSON encoded and stored back in the UserData column.

The code I'm using at the moment fails miserably, only replacing the data in UserData field with "Save" lol...

if(isset($_POST[$Page])) {
    $data= $_POST[$Page];
    $stmt = $con->prepare("UPDATE users SET UserData = ? WHERE Username = ?");
    $stmt->bind_param('ss', $data, $Username);
    $stmt->execute();
    $stmt->close();
    header('Location: '.$_SERVER['PHP_SELF']);
    die;
}

So I guess my main question is, how to get the data for each page into it's own array, and then combine the arrays + json encode and store.

A pic of it:

https://i.sstatic.net/o7BkX.png

2
  • And what is the exact problem? Commented Jun 6, 2016 at 7:22
  • @u_mulder the problem is that I'm not sure how to create an PHP object out of these dynamicly created forms Commented Jun 6, 2016 at 7:30

1 Answer 1

1

Does the data need to be stored while you create the dynamic forms, or after a user submits the form?

You can retrieve the data from a submit which is in the $_POST variable. Then you can store it in an object.

$object = new stdClass();
$object->page = $_POST['home'];

You can also cast the array as an object.

$object = (object) $array;

I think what you want is to create a multidimensional array within the form input like this.

foreach($pages as $page)
   echo "<form action=" .$_SERVER['PHP_SELF']. " method='POST' id="pagesform">";

   echo "<h4>Title: </h4><input type='text' name='"pages.[$page]."[]' value='".$UserData['Title']."'><br/>

Then iterate through the post variable and store it as an object.

$pages = $_POST['pages'];
$object = new stdClass();

foreach($pages as $key => $value) {
   $object->pages[$key] = $value; 
}

I advise you not to store JSON in your database, but rather store each value of the array in your database and json_encode it when needed. You can store each field of the object individually in your database within the foreach loop.

Sign up to request clarification or add additional context in comments.

3 Comments

This should be a comment, not an answer.
I cannot comment -__-
Hey Nico, thanks for the info. Could you please be more specific, maybe provide example of how to do this with my submit code? (I have updated my question)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.