简体   繁体   中英

Form input type php

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. 在此处输入图片说明

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>'; } } ?> ?> 

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.

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>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM