employee directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
babynerd
Forum Newbie
Posts: 6
Joined: Wed Feb 25, 2004 3:42 pm

employee directory

Post by babynerd »

I have been asked to create an online employee directory. The only probem I am having is I can not get the email address that I retrive from the database to be to be mailto links. I have been able to get it grab the first address in the database but then it just sends that address to everyother row instead of continuing to grab emails and placing them next to each persons name.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hmm, not sure exactly what your problem is. Maybe a code example would help.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

if they're indexed, you can use

Code: Select all

for($i=0;$i<$rows;$i++){
  $sql = "SELECT email FROM table WHERE id = '$i' LIMIT 1";
  $email = mysql_result(mysql_query($sql), 0,0);
  echo <<<EOT
  <a href="mailto:$email">$email</a><br>
EOT;
}
babynerd
Forum Newbie
Posts: 6
Joined: Wed Feb 25, 2004 3:42 pm

Post by babynerd »

pickle wrote:Hmm, not sure exactly what your problem is. Maybe a code example would help.

Header code
<?php
$currentPage = $HTTP_SERVER_VARS["PHP_SELF"];

$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($HTTP_GET_VARS['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $HTTP_GET_VARS['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_empdirdb, $empdirdb);
$query_Recordset1 = "SELECT employeedir.FullName, employeedir.Title, employeedir.PhoneNum, employeedir.EmailAddress, employeedir.Department, employeedir.Program FROM employeedir ORDER BY employeedir.Department, employeedir.Program, employeedir.FullName";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $empdirdb) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($HTTP_GET_VARS['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $HTTP_GET_VARS['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($HTTP_SERVER_VARS['QUERY_STRING'])) {
$params = explode("&", $HTTP_SERVER_VARS['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_Recordset1") == false &&
stristr($param, "totalRows_Recordset1") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_Recordset1 = "&" . implode("&", $newParams);
}
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
$Email=$row_Recordset1['EmailAddress'];

?>

body code

<tr>
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['FullName']; ?></div></td>
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['Title']; ?></div></td>
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['PhoneNum']; ?></div></td>
<td width="17%"> <div class="dir"><?php echo "<a href="mailto:" . $Email . "">" . $Email . "</a>";?> </div>
</td>
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['Department']; ?></div></td>
<td width="15%"> <div class="dir"><?php echo $row_Recordset1['Program']; ?></div></td>
</tr>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Your code is as follows, but inside PHP delimiters for easier reading:

Code: Select all

<?php 
$currentPage = $HTTP_SERVER_VARS["PHP_SELF"]; 

$maxRows_Recordset1 = 10; 
$pageNum_Recordset1 = 0; 
if (isset($HTTP_GET_VARS['pageNum_Recordset1'])) { 
$pageNum_Recordset1 = $HTTP_GET_VARS['pageNum_Recordset1']; 
} 
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1; 

mysql_select_db($database_empdirdb, $empdirdb); 
$query_Recordset1 = "SELECT employeedir.FullName, employeedir.Title, employeedir.PhoneNum, employeedir.EmailAddress, employeedir.Department, employeedir.Program FROM employeedir ORDER BY employeedir.Department, employeedir.Program, employeedir.FullName"; 
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1); 
$Recordset1 = mysql_query($query_limit_Recordset1, $empdirdb) or die(mysql_error()); 
$row_Recordset1 = mysql_fetch_assoc($Recordset1); 

if (isset($HTTP_GET_VARS['totalRows_Recordset1'])) { 
$totalRows_Recordset1 = $HTTP_GET_VARS['totalRows_Recordset1']; 
} else { 
$all_Recordset1 = mysql_query($query_Recordset1); 
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1); 
} 
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1; 

$queryString_Recordset1 = ""; 
if (!empty($HTTP_SERVER_VARS['QUERY_STRING'])) { 
$params = explode("&", $HTTP_SERVER_VARS['QUERY_STRING']); 
$newParams = array(); 
foreach ($params as $param) { 
if (stristr($param, "pageNum_Recordset1") == false && 
stristr($param, "totalRows_Recordset1") == false) { 
array_push($newParams, $param); 
} 
} 
if (count($newParams) != 0) { 
$queryString_Recordset1 = "&" . implode("&", $newParams); 
} 
} 
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1); 
$Email=$row_Recordset1['EmailAddress']; 

?> 

body code 

<tr> 
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['FullName']; ?></div></td> 
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['Title']; ?></div></td> 
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['PhoneNum']; ?></div></td> 
<td width="17%"> <div class="dir"><?php echo "<a href="mailto:" . $Email . "">" . $Email . "</a>";?> </div> 
</td> 
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['Department']; ?></div></td> 
<td width="15%"> <div class="dir"><?php echo $row_Recordset1['Program']; ?></div></td> 
</tr>
I'm assuming the table is output in a loop for each person? If so, is it possible that $Email isn't being reset on every iteration of the loop? The rest of the user's attributes would be updated as the row changes. If that's not the case, try unsetting the $Email variable just before it is set:

Code: Select all

<?php 
$currentPage = $HTTP_SERVER_VARS["PHP_SELF"]; 

$maxRows_Recordset1 = 10; 
$pageNum_Recordset1 = 0; 
if (isset($HTTP_GET_VARS['pageNum_Recordset1'])) { 
$pageNum_Recordset1 = $HTTP_GET_VARS['pageNum_Recordset1']; 
} 
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1; 

mysql_select_db($database_empdirdb, $empdirdb); 
$query_Recordset1 = "SELECT employeedir.FullName, employeedir.Title, employeedir.PhoneNum, employeedir.EmailAddress, employeedir.Department, employeedir.Program FROM employeedir ORDER BY employeedir.Department, employeedir.Program, employeedir.FullName"; 
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1); 
$Recordset1 = mysql_query($query_limit_Recordset1, $empdirdb) or die(mysql_error()); 
$row_Recordset1 = mysql_fetch_assoc($Recordset1); 

if (isset($HTTP_GET_VARS['totalRows_Recordset1'])) { 
$totalRows_Recordset1 = $HTTP_GET_VARS['totalRows_Recordset1']; 
} else { 
$all_Recordset1 = mysql_query($query_Recordset1); 
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1); 
} 
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1; 

$queryString_Recordset1 = ""; 
if (!empty($HTTP_SERVER_VARS['QUERY_STRING'])) { 
$params = explode("&", $HTTP_SERVER_VARS['QUERY_STRING']); 
$newParams = array(); 
foreach ($params as $param) { 
if (stristr($param, "pageNum_Recordset1") == false && 
stristr($param, "totalRows_Recordset1") == false) { 
array_push($newParams, $param); 
} 
} 
if (count($newParams) != 0) { 
$queryString_Recordset1 = "&" . implode("&", $newParams); 
} 
} 
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
//***************
// Change here
//***************
unset($Email);
$Email=$row_Recordset1['EmailAddress']; 

?> 

body code 

<tr> 
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['FullName']; ?></div></td> 
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['Title']; ?></div></td> 
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['PhoneNum']; ?></div></td> 
<td width="17%"> <div class="dir"><?php echo "<a href="mailto:" . $Email . "">" . $Email . "</a>";?> </div> 
</td> 
<td width="17%"> <div class="dir"><?php echo $row_Recordset1['Department']; ?></div></td> 
<td width="15%"> <div class="dir"><?php echo $row_Recordset1['Program']; ?></div></td> 
</tr>
babynerd
Forum Newbie
Posts: 6
Joined: Wed Feb 25, 2004 3:42 pm

Post by babynerd »

Pikcle,

You assumption is correct. all of the other fields in the table are changing execpt for the email one.
babynerd
Forum Newbie
Posts: 6
Joined: Wed Feb 25, 2004 3:42 pm

Post by babynerd »

I tried the unset line that you suggested but it still comes up the same. How would I reset $email on every loop?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Have you tried not putting the email address in a variable, but rather just echoing $row_Recordset1['Email'] ? To reset the variable, just call unset($Email) at the beginning of every iteration of the loop.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
babynerd
Forum Newbie
Posts: 6
Joined: Wed Feb 25, 2004 3:42 pm

Post by babynerd »

Thxs for all the help it's working now.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What exactly fixed your problem? I'm curious to know.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
babynerd
Forum Newbie
Posts: 6
Joined: Wed Feb 25, 2004 3:42 pm

Post by babynerd »

I took out the $email var and just did this to the body:

Code: Select all

<?php echo "<a href="mailto:" . $row_Recordset1['EmailAddress'] . "">" . $row_Recordset1['EmailAddress'] . "</a>";?>
Post Reply