Dit is mijn database structuur (engels want ik had deze vraag ook op stackoverflow gezet zonder antwoord):
templates:
id - template id
title - name of list (template)
id_company (company list belongs to, irrelevant for this question)
questioncat:
id - id of category
title - name of category
tid - template id that the category belongs to
questions:
id - question id
question - the actual question
catid - category id that the questions belong to
Mijn code:
<?PHP
$conn = new Connection;
$arr = $_POST['lijst'];
$companyid = $_POST['companyid'];
$store = [];
// pull off first arr element
$title = array_shift($arr);
// save title to store
$store['title'] = $title['name'];
// Insert template title and companyid
$inserttemplate = '
INSERT INTO templates (title, id_company) VALUES ("'.$conn->real_escape_string($title["value"]).'","'.$conn->real_escape_string($companyid).'")';
$inserttemplatecon = $conn->query($inserttemplate);
$lastinserted = $conn->inserted_id();
$currCat = '';
foreach($arr as $a) {
$val = $a['value'];
// handle category
if($a['name'] == 'category[]') {
// save cat name
$currCat = $val;
$insertcats = '
INSERT INTO questioncat (title, tid) VALUES ("'.$conn->real_escape_string($currCat).'", "'.$conn->real_escape_string($lastinserted).'")';
$insertcatscon = $conn->query($insertcats);
// init questions array
$store[$currCat] = [];
echo $store[$currCat];
}else {
// add question to question array
$store[$currCat][] = $val;
}
}
?>
Met bovenstaande code kan ik de array $store printen. Als ik wat voorbeeld gegevens invoer is dit het resultaat:
Array
(
[title] => lijsttitle
[Category 1] => Array
(
[0] => Question 1cat1
[1] => Question 2cat1
[2] => Question 3cat1
)
[Category 2] => Array
(
[0] => Question 1cat2
)
[Category 3] => Array
(
[0] => Question 1cat3
)
[Category 4] => Array
(
[0] => Question 1cat4
[1] => Question 2cat4
)
)
Het lukt me nu een lijst op te slaan met de categorieen, alleen de vragen bij die categorieen niet.
Ik heb dit geprobeerd:
<?PHP
foreach($arr as $a) {
$val = $a['value'];
// handle category
if($a['name'] == 'category[]') {
// save cat name
$currCat = $val;
$insertcats = '
INSERT INTO questioncat (title, tid) VALUES ("'.$conn->real_escape_string($currCat).'", "'.$conn->real_escape_string($lastinserted).'")';
$insertcatscon = $conn->query($insertcats);
// init questions array
$store[$currCat] = [];
}else {
// add question to question array
$store[$currCat][] = $val;
}
foreach($store[$currCat] as $question){
echo $question.'<br>';
}
}
?>
$store[$currCat] loopen om eerst is de vragen te laten zien maar met die code krijg ik dit te zien:
Question 1cat1
Question 1cat1
Question 2cat1
Question 1cat1
Question 2cat1
Question 3cat1
Question 1cat2
Question 1cat3
Question 1cat4
Question 1cat4
Question 2cat4
Zoals je ziet klopt dat niet met de array, hoe komt dat? Ik heb ook al geprobeerd $question leeg te gooien binnen de 1e foreach. Volgens mij ben ik er bijna, als ik de vragen juist kan weergeven lukt het me wel ze in de database te inserten.