我的问题是:如何在foreach循环内(或同时)匹配多个数组(两个,甚至更多)
$cat = $bdd->query('SELECT cat_name FROM categories');
$categories_list = $cat->fetchAll(PDO::FETCH_COLUMN);
$cat_id = $bdd->query('SELECT cat_id FROM categories');
$categories_id = $cat_id->fetchAll(PDO::FETCH_COLUMN);
$arrcatid = array($categories_list,$categories_id);
echo '<form method="post" action="accès/create_topic_post.php" autocomplete="off">';
echo '<label for="sujet">Sujet :';
echo '<input type="text" name="sujet" id="sujet" required autofocus>';
echo '</label>';
echo '<label for="cat">Catégories :';
echo '<select name="topic_name">';
foreach ($categories_list as $cat_name){
echo "<option value=\"$cat_name\">$catname</option>";
}
$cat_id->closeCursor();
$cat->closeCursor();
echo '</select>';
echo '</label>';
echo '<input type="submit" value="Envoyer">';
echo '</form>';
var_dump($arrcatid);
因此,目标是将数组0与数组1 cat_name && cat_id
非常感谢:D
答案 0 :(得分:3)
如何在sql查询中同时获取名称和id?
$cat = $bdd->query('SELECT cat_name, cat_id FROM categories');
答案 1 :(得分:1)
我用过:
$cat = $bdd->query('SELECT cat_name,cat_id FROM categories');
$categories_list = $cat->fetchAll(PDO::FETCH_KEY_PAIR);
并为foreach:
foreach ($categories_list as $cat_id => $cat_name){
echo "<option value=\"$cat_name\">$cat_id</option>";