Announcement

Collapse
No announcement yet.

[SOLVED] Email array

Collapse
This topic is closed.
X
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    $check_sql3 = "SELECT e.email"
    . " FROM employees e"
    . " JOIN email_notifications n on e.emp_id=n.emp_id"
    . " WHERE n.email1 = '1'";
    sc_lookup(rs, $check_sql3);

    if (isset({rs[0][0]})) // Row found
    {
    //{rs} = array();
    //$mail_to = implode(";",array());
    $mail_to = implode(";",'{rs}');
    }
    sc_lookup gives you a two dimensional array, so you can't just implode the array you have to 'flatten' it first.

    To get you the semicolon separated list you need, you have two choices (MySQL or PHP).

    MySQL
    Code:
    $check_sql3 = "SELECT GROUP_CONCAT(e.email SEPARATOR ';')"
       . " FROM employees e"
       . " JOIN email_notifications n on e.emp_id=n.emp_id"
       . " WHERE n.email1 = '1'";
    sc_lookup(rs, $check_sql3);
    
    if (isset({rs[0][0]}))     // Row found
    {
        $mail_to = {rs[0][0]};
    }
    PHP (array_map function)
    Code:
    $check_sql3 = "SELECT e.email"
       . " FROM employees e"
       . " JOIN email_notifications n on e.emp_id=n.emp_id"
       . " WHERE n.email1 = '1'";
    sc_lookup(rs, $check_sql3);
    
    if (isset({rs[0][0]}))     // Row found
    {
        $tmp_email = array_map(function($item) { return $item[0]; } , $rs); //returns an one dimensional array with the email addresses
        $mail_to = implode(';', $tmp_email); // now you can implode
    }
    Or PHP (foreach loop)
    Code:
    $check_sql3 = "SELECT e.email"
       . " FROM employees e"
       . " JOIN email_notifications n on e.emp_id=n.emp_id"
       . " WHERE n.email1 = '1'";
    sc_lookup(rs, $check_sql3);
    
    if (isset({rs[0][0]}))     // Row found
    {
        $tmp_mail = array();
        foreach($rs as $address)
        {
            $tmp_email[] = $address; // fill the array with the email addresses
        }
        if(count($tmp_mail) > 0)
        {
            $mail_to = implode(';', $tmp_email); // now you can implode
        }
    }
    Now it's up to you.

    jsb
    Last edited by jsbinca; 09-11-2014, 12:18 PM.

    Comment


    • #17
      @jsbinca

      Code:
      $check_sql3 = "SELECT GROUP_CONCAT(e.email SEPARATOR ';')"
         . " FROM employees e"
         . " JOIN email_notifications n on e.emp_id=n.emp_id"
         . " WHERE n.email1 = '1'";
      sc_lookup(rs, $check_sql3);
      
      if (isset({rs[0][0]}))     // Row found
      {
          $mail_to = {rs[0][0]};
      }
      The GROUP_CONCAT did the trick! Thank you so much! I can't believe it was that simple. At least I learned a lot about arrays through all this. Thank you RHS for helping me understand the logic behind it all. You guys are a valuable resource on these forums!

      Comment


      • #18
        Closed as solved
        /Giuseppe

        Professional Scriptcase Services
        Some Customers opinions

        Comment

        Working...
        X