CodeIgniter Forums

Full Version: Decoding a database array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]RaGe10940[/eluser]
Well I am trying to set up a emailing system on my app. Currently I am starting "small" but will be going to a bigger mass emailing system 100+ emails a day kind of thing.

My problem is this :

I encrypt the emails since that is a red flag data at my workplace. So I use codeigniters AES encryption. I am first trying to have en email go out to all activated admins when a new account is requested. Up until now I have been hard coding emails and one of my bosses said well what if your not here any more? Sooooo... they want it to be "dynamically" sending the emails to all the users with a role = 3 (admin) and that are activated.

My model :

Code:
function admin_email()
{
  try
  {
   $sql = "SELECT * from users WHERE status = 'Activated' and role = 3";
   $admin_email = $this -> db -> conn_id -> prepare($sql);
   $admin_email -> execute();

   if ($admin_email)
   {
    if ($admin_email -> rowCount() > 0)
    {
     foreach ($admin_email -> fetchall() as $row)
     {
      $db = $row['email'];
     }
     $email = $this -> encrypt -> decode($db);

     if ($email)
     {
      return $email;
     }
    }
   }
  } catch (PDOException $e)
  {
   error_log($e -> getMessage());
   die("An Error Occurred, Contact System Admin - Err: L_M");
  }
}

The logic behidn this is that I can just select * from the users table filtering by the activated status and role = 3. Send the results of that into a for each loop only looking for the row email.

Once the $db variable gets populated then send that through a decode function that CI offers.

My controller :

Code:
$this -> load -> model('login_model');
            $this -> load -> library('email');
            $this -> load -> library('encrypt');
            
            $emails = $this -> login_model -> admin_email();

            $first = $this -> input -> post('fname');
            $last = $this -> input -> post('lname');
            $email = $this -> input -> post('email');

            $this -> email -> from($email);
            $this -> email -> to($emails);
            $this -> email -> reply_to($email);
            $this -> email -> subject('' . $first . ' ' . $last . ' Account Request');
            $this -> email -> message('{unwrap}Hello this is ' . $first . ' ' . $last . ', I am requesting to be added to the staff log-in.{/unwrap}');

            if (!$this -> email -> send())
            {
                $this -> session -> set_flashdata('email', 'Email Was Not Sent!');
                $this -> request_account();
            } else
            {
                $this -> session -> set_flashdata('login', 'Request Sent!');
                redirect('login_controller/index', 'location');
            }
        }

Here the logic is as follows : load the models and library.

Take the returned data from the model and insert that in the variable : emails (this is the decoded AES 256)

then put the emails into the mail_to function in the email library.

Now this works... BUT and a BIG BUT! -> it only works for the first admin email that is given and that is my account (coder) I also have another account called testing which is activated and an admin and that account does not get emailed.

Is it possible to decode multiple variables pieces of data from different rows?




El Forum

[eluser]RaGe10940[/eluser]
Code:
{
        $sql = "SELECT * from users WHERE status = 'Activated' and role = 3";
        $admin_email = $this -> db -> conn_id -> prepare($sql);
        $admin_email -> execute();
        $emails = array();

        if ($admin_email)
        {
            if ($admin_email -> rowCount() > 0)
            {
                foreach ($admin_email -> fetchall(); as $row)
                {
                    $emails[] = $this -> encrypt -> decode($row['email']);
                }
                return $emails;
            }
        }
    }

This fixed it!

El Forum

[eluser]Unknown[/eluser]
This coding worked. Its short and easy than what i had written the code for it. Mine was very complicated.
<a href="http://www.landscape-consultants.net/">Houston Landscape Architects</a>

El Forum

[eluser]RaGe10940[/eluser]
[quote author="juliebarrino" date="1364448860"]This coding worked. Its short and easy than what i had written the code for it. Mine was very complicated.
<a href="http://www.landscape-consultants.net/">Houston Landscape Architects</a>
[/quote]

I am very glad the code helped you!