I'm working on a user authorization script and have everything working except for the admin function "Email Users".
The script works fine as long as no users have been deleted from the system. Once a user has been deleted, it leaves a "hole" in the user ID, making it harder to loop through them to pull out email addresses.
To work around it, I used arrays and simply looped through them instead, but I'm still getting the same problem.
I'm certain it is an array looping error because the function gets all of the email addresses and adds them to the array without a problem (the instruction $number_of_users = count($user_list); returns an accurate number).
I've spent several hours trying to figure it out, but I'm still a bit new to PHP. Any suggestions?
Here is the function:
function email_users($from, $subj, $message, $contact) {
mysql_connect($this->server, $this->db_user, $this->db_pass);
mysql_select_db($this->database);
$query1 = mysql_query("select * from data");
$num_users = mysql_num_rows($query1);
$count = 0;
$id_count = 0;
$user_list = array();
while ($count <= $num_users) {
$query2 = mysql_query("select id, email from data where id = '$id_count'");
$result = mysql_num_rows($query2);
if ($result > 1) {
$id_count++; }
else {
list($data_id, $email) = mysql_fetch_row($query2);
if ($contact == 1) {
$query3 = mysql_query("select contact, admin from login where id = '$data_id'");
list($cont, $admin) = mysql_fetch_row($query3);
if ($cont == 1) {
array_push( $user_list, $email ); } }
if ($contact == 2) {
$query3 = mysql_query("select contact, admin from login where id = '$data_id'");
list($cont, $admin) = mysql_fetch_row($query3);
if ($admin == 2) {
array_push( $user_list, $email ); } }
if ($contact == 3) {
array_push( $user_list, $email ); }
$id_count++;
$count++; }
}
$number_of_users = count($user_list);
if ($number_of_users == 0) {
return false; }
for ($i = 0; $i <= $number_of_users; $i++) {
@mail($user_list[$i], $subj, "-----\nThis message has been sent by $from, an Administrator of the $this->co_name Membership System.\n-----\n\n$message", "From: $this->admin"); }
return $number_of_users;
}