我有一个数组包含从MySQL表中查询的数据,我想在数组中使用foreach显示特定数据(特定列数据)。PHP:使用foreach在多维数组中显示特定数据
+------+-------+------+-----+
| id | name | sex | age |
+------+-------+------+-----+
| 1 | tom | m | 18 |
| 2 | jane | f | 32 |
| 4 | Doyle | m | 25 |
+------+-------+------+-----+
假设数组名是$候选人,我需要从$显示考生是刚刚从科拉姆“名”的名字....
我已经这样做了完美的使用而循环,但我不希望再次运行数据库查询,因为数据在阵列$候选人
这里已经存在是代码:
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 ...
请帮助我这个...谢谢
这似乎更像是一个数据问题?尝试做'print_r($ candidates);'在你的foreach之上,看'name'字段是否包含全名? – Gavin
向我们展示你的查询....看起来像从db中获取数组时出现错误 – bipen
只需从参数中删除'$ id =>'。所以它应该是'foreach($ candidate作为$ cands){echo $ cands ['name'];}' – kpotehin