Helpful Information
 
 
Category: LDAP Programming
Formatting query results/table

I have seen this posted in the MySQL forums but am new to LDAP/PHP stuff. I want to post data from an LDAP query into a table format for an online phone directory. The results returned are a name and a phone number that is formatted right now as a table with two rows. Like the following:

User1 Number1
User2 Number2
User3 Number3
User4 Number4

I want it to read:
User1 Number1 User2 Number2
User3 Number3 User4 Number4

Any help or suggestions is greatly appreciated. The full code is as follows:



<?php

error_reporting (7);

$surname = trim($_REQUEST['sursearch']);
$ds = ldap_connect("10.1.2.1"); // must be a valid LDAP server!

if ($ds) {
$r = ldap_bind($ds); // this is an "anonymous" bind, typically read only access

// Search surname entry
$sr = ldap_search($ds, "o=bl", "sn=$surname*");
$info = ldap_get_entries($ds, $sr);

for ($i=0; $i<$info["count"]; $i++) {
//This line looks to see if telephonenumber field actually has a number if not it does not post the data
if ($info[$i]["telephonenumber"][0]) {
if ($info[$i]["fullname"][0])
//Put results in a table
echo " <table border='1' cellspacing='1' cellpadding='0'>
<td valign='center' align='left' bgcolor='#ffffff' width=200><font size='3'>" . $info[$i]["fullname"][0] . "</td></font>
<td valign='center' align='center' bgcolor='#ffffff' width=100><font size='3'>" . $info[$i]["telephonenumber"][0] . "</td></font>
";
}
}
}
echo "</table>";
ldap_close($ds);
?>

Try this out let me know if you ned more help



// error_reporting (7);
$surname = trim($_REQUEST['sursearch']);
$ds = ldap_connect("10.1.2.1"); // must be a valid LDAP server!

if ($ds) {
$r = ldap_bind($ds); // this is an "anonymous" bind, typically read only access
// Search surname entry
$sr = ldap_search($ds, "o=bl", "sn=$surname*");
$info = ldap_get_entries($ds, $sr);

### create table first before looping though the results ###
echo "<table border='1' cellspacing='1' cellpadding='0'>";
echo '<tr>'; ### table rows ###
for ($i=0; $i<$info["count"]; $i++) {
if ($i & 2) { ### this adds table rows ###
echo '</tr>';
echo '<tr>';
}
//This line looks to see if telephonenumber field actually has a number if not it does not post the data
if ($info[$i]["telephonenumber"][0]) {
if ($info[$i]["fullname"][0])
//Put results in a table
echo "<td valign='center' align='left' bgcolor='#ffffff' width=200><font size='3'>" . $info[$i]["fullname"][0] . "</td></font>
<td valign='center' align='center' bgcolor='#ffffff' width=100><font size='3'>" . $info[$i]["telephonenumber"][0] . "</td></font>
";
}
}
}
echo '</tr>'; ### table rows ###
echo "</table>";
ldap_close($ds);

That's almost got it! It does return all the results, doesn't skip anything, formats it in two columns but the second column contains blank entries. Looks to be something that hopefully I can figure out. I think I see the issue already. I have entries in eDirectory that do not have a fullname or telephonenumber entry and it skips over them but leaves a blank field in their place.

I will post the fully functional code here as soon as I have it.

Many thanks for putting me on the right track Viper!

Sure np










privacy (GDPR)