0

I want to create a form using a table where a column generates the questions( nom ) and another one generates the type of the response(type ) that can be (text, date, checkbox, radio etc ...), I was able to generate the questions however I wasn't able to determinate the type. I am really struggling to use the type column in my champs table as a variable of input types in a form.enter image description here

Any help would be extremely appreciated

to clarify more here are my codes:

ChampsModel.php

<?php 
require_once("../config/database.php");

function Champsbyqid($qid){
	$c = Database :: connect();
	$results = array();
	$q = $c -> prepare ("SELECT nom FROM champs WHERE qid=?") ;
	$q -> execute (array($qid));
	while ($data = $q -> fetch()) {
		$results[] = $data;
	}	
	Database :: disconnect();
	return $results;
}

function getType($qid){
	$c = Database :: connect();
	$results = array();
	$q = $c -> prepare ("SELECT type FROM champs WHERE qid=?") ;
	$q -> execute (array($qid));
	while ($data = $q -> fetch()) {
		$results[] = $data;
	}	
	Database :: disconnect();
	return $results;
}


?>

ChampsController.php

<?php 
require_once("../model/champsModel.php");

$champs = Champsbyqid(1);
$type = getType(1);

?>

Champs.php

   <?php
require_once("../controller/champsController.php");

foreach ($champs as $value) {
	foreach ($types as $val) {
		echo $value['nom'].'<form method="POST"><input type='$val['type']'></form>';
	}
	
}


?>		

?>		

2 Answers 2

1

You are generating each question repeatedly for every type retrieved, you don't need the second foreach loop. I would also suggest using a single query to fetch both values, then you can simple use $value['nom'] and $value['type']. But if you need to keep them separate, just use

for ($i = 0; $i < count($champs); $i++) {
  echo $champs[$i]['nom'].'<form method="POST"><input type='$type[$i]['type']'></form>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Beat you by a minute... haha.
vbudo & inarilo thank you both I finally noticed that there is no need for two sql queries and a mixed one was more effective and both your ideas worked thank you very much you don't know how much this is helpful to me thanks !
1

you're going to want to SELECT * FROM WHERE qid = ORDER BY ordre; <-- is this a type that is suppose to be order?. Get the rows as an associative array, in a variable, lets say $value. Then,

<form method="" action="">
while (there are rows) {

    echo $value["nom"].'<input type="'.$value["type"].'" 
    name="whatever"><br />';

   }
</form>

Be weary of reserved words in your programming languages. Some words you can't use as variables.

Comments

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.