我正在尝试将两组结果格式化以显示在类似的表中。一个是MySQL查询,一个是ldap查询。唯一的问题是一个结果用foreach循环显示,另一个是计算结果,我不知道如何使用另一个工作。
/* This is the MySQL query. I am pulling the info and creating
* a table with the results. The #$header array is the columns
* of the table. Then foreach result, I put the $results of my
* query in the same order as my columns. */
$results = db_query("SELECT * FROM table WHERE `".$searchParams."`='".$input."'");
$header = array(
t('First Name'),
t('Last Name'),
t('Passport Number'),
t('VISA Type'),
t('Step in Process')
);
$rows = array();
foreach($results as $result){
$rows[] = array(
$result-> first_name,
$result -> last_name,
$result -> gwf,
$result -> visa_type,
$result -> current_stage,
);
}
return theme('table', array('header'=>$header,'rows'=>$rows));
/*Here is how I'm currently displaying my ldap_search() results*/
$ds=ldap_connect("ldap-server");
if ($ds) {
$r=ldap_bind($ds);
$sr=ldap_search($ds, "DC=,DC=co,DC=uk", $search);
$info = ldap_get_entries($ds, $sr)
for ($i=0; $i<$info["count"]; $i++) {
echo "dn is: " . $info[$i]["dn"] . "<br />";
echo "first cn entry is: " . $info[$i]["cn"][0] . "<br />";
echo "first email entry is: " . $info[$i]["mail"][0] . "<br /><hr />";
}
如何更改ldap结果以便我可以使用相同的foreach来创建我使用mysql结果创建的表?如果这很简单,我很抱歉,我仍然试图绕过循环,计数等等。
答案 0 :(得分:1)
有一个小技巧,但基本上你可以用foreach做同样的事情。改变这一行:
for ($i=0; $i<$info["count"]; $i++) {
到此:
foreach ($info as $i => $val) {
if($i == "count")
continue;
echo "dn is: " . $info[$i]["dn"] . "<br />";
echo "first cn entry is: " . $info[$i]["cn"][0] . "<br />";
echo "first email entry is: " . $info[$i]["mail"][0] . "<br /><hr />";
}
继续行是$info["count"]
,这不是一行,你需要在循环时忽略它。
答案 1 :(得分:1)
添加到我的评论中:
您可以在
foreach
内完成所有操作,只需添加索引,例如foreach($results as $index => $result)
例如
foreach($results as $index => $result) {
if (isset($info[$index])) {
// use both $result and $info
}
}
答案 2 :(得分:1)
不百分之百肯定我明白你在寻找什么,但我相信就是这样:
$header = array(
t('DN'),
t('CN'),
t('Email'),
);
foreach ($info as $i => $val) {
//this causes the $info['count'] row to be ignored
if($i == "count")
continue;
$rows[] = array(
$val["dn"],
$val["cn"][0],
$val["mail"][0]
);
}