0

I have a list with three things, first a title for the entire list, then a user can add categories with questions below each category dynamically.

I need to add these lists to my database and currently have this array when posting (using ajax with serializeArray ):

Array
(
    [0] => Array
        (
            [name] => lijsttitle
            [value] => Title
        )

    [1] => Array
        (
            [name] => category[]
            [value] => category 1
        )

    [2] => Array
        (
            [name] => question[]
            [value] => question 1
        )

    [3] => Array
        (
            [name] => question[]
            [value] => question 2
        )

    [4] => Array
        (
            [name] => question[]
            [value] => question 3
        )

    [5] => Array
        (
            [name] => category[]
            [value] => category 2
        )

    [6] => Array
        (
            [name] => question[]
            [value] => question 1
        )

    [7] => Array
        (
            [name] => question[]
            [value] => question 2
        )

)

All questions and categories are already in the correct order but I want them grouped together, something like this:

Array
(
    [title] => Array
        (
            [value] => Title
        )

    [category 1] => Array
        (
            [question] => question 1
            [question] => question 2
            [question] => question 3
        )

    [category 2] => Array
        (
            [question] => question 1
            [question] => question 2
        )
)

Or maybe another way if there is a better one.

I serialize my form like this:

$( ".lijstbutton" ).on( "click", function( event ) {
  event.preventDefault();
  url = 'includes/createlist.php';

  $lijst = $( '#lijstform' ).serializeArray();

  var posting = $.post(url, {lijst: $lijst});

    posting.done(function( data ) {
     $( ".lijstresult" ).empty().slideDown('fast').append( data );
   });
  });

And to show my current array I have this code in createlist.php :

echo '<pre>';
print_r($_POST['lijst']);
echo '</pre>';

In the end I need all questions linked to a category and all categories linked to the title of the entire list.

So you can have multiple lists each with their categories and questions below those categories.

5
  • And have you tried to write any code to re-organise this array? Commented Nov 14, 2018 at 9:51
  • How do you decide the categories of questions? You don't have a key value pair which can provide details about the category of a question. Commented Nov 14, 2018 at 9:52
  • @BhaumikMehta I know, how can I do that? I read you have to add [] after the name of the input fields, but in my array there is nothing added, just the empty [] Commented Nov 14, 2018 at 9:54
  • 1
    Could you show us the JSON? Commented Nov 14, 2018 at 10:02
  • @Keith I don't think it is JSON, but when I encode it this is what you see: [{"name":"lijsttitle","value":"sdg"},{"name":"category[]","value":"dsg"},{"name":"question[]","value":"dsgsd"},{"name":"category[]","value":"gsdgsd"},{"name":"question[]","value":"gsd"}] Commented Nov 14, 2018 at 10:08

1 Answer 1

1

I'd rearrange the data a bit. You can't user 'question' as a key multiple times in an array. $arr is the POSTed data.

$store = [];

// pull off first arr element
$title = array_shift($arr);
// save title to store
$store['title'] = $title['name'];

$currCat = '';
foreach($arr as $a) {
  $val = $a['value'];
  // handle category
  if($a['name'] == 'category[]') {
    // save cat name
    $currCat = $val;
    // init questions array
    $store[$currCat] = [];
  }
  else {
    // add question to question array
    $store[$currCat][] = $val;
  }
}

print_r($store);
Sign up to request clarification or add additional context in comments.

2 Comments

I get an Internal Server Error 500 from above code. Since the response is used with Ajax I can't see the exact PHP error.
Edited. Had a few typos.

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.