mysql テーブルからクエリされたデータを含む配列があり、foreach を使用して配列に特定のデータ (特定の列データ) を表示したいと考えています。
+------+-------+------+-----+
| id | name | sex | age |
+------+-------+------+-----+
| 1 | tom | m | 18 |
| 2 | jane | f | 32 |
| 4 | Doyle | m | 25 |
+------+-------+------+-----+
配列名が $candidates であると仮定すると、$candidates から表示する必要があるのは、列 'name' の名前だけです....
while ループを使用してこれを完全に実行しましたが、データが配列 $candidates に既に存在するため、データベースクエリを再度実行したくありません
コードは次のとおりです。
1. Class function performing the query
<?php
class pubCourses {
function getPubRegCand($org_id, $c_id){
//fetch the list of registered candidates for this course
$query_reg_cand = @mysql_query("select * from public_reg_cand where org_id='".$org_id."'");
$cand = array();
$cand = @mysql_fetch_assoc($query_reg_cand);
return $cand;
}
}
?>
2. Calling the class function
<?php
$obj = new pubCourses();
$candidates = array();
if(isset($_GET['c_id']) && isset($_GET['org_id'])){
$candidates = $obj->getPubRegCand($_GET['org_id'], $_GET['c_id']);
}
?>
3. Foreach statement to display the candidates names
<?php
foreach($candidates as $id=>$cands){
?>
<tr>
<td> </td>
<td> </td>
<td class="bold">Participant's Name: <?php echo ucwords($cands['name']); ?></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<?php
}
?>
上記の foreach ステートメントは、必要な「名前」を表示しているのではなく、最初の行の最初の文字、つまり 1、t、m、1... を表示しています。
これで私を助けてください...ありがとう