3
votes

I made a PHP page that looks up Constant Contact e-mail addresses in a database and returns a table listing their name, e-mail address, and mailing list they are in. You enter the addresses here: Contact Lookup Tool along with your Constant Contact user name and password.

For some reason, only the last row of the results page has a list of mailing lists. The other ones have the word "Array," which I stripped out, so now those rows are blank. Here is a screen shot of what I mean:

http://www.advantage-computer.com/images/ScreenCap.png

They're all in a list, though. Here's the code for search.php. The form submits to that file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <title>List of Contacts</title>
        <style type="text/css">
            .hdr
            {
                margin-bottom: 0px;
                padding-bottom: 0px;
            }
        </style>
    </head>
    <body>
        <table width="75%">
            <tr>
                <td class="hdr">Name</td>
                <td class="hdr">E-mail address</td>
                <td class="hdr">List(s)</td>
            </tr>
            <tr>
                <td colspan="3">
                    <hr style="padding:0; margin:0">
                </td>
            </tr>
            <?PHP
                require_once('./class.cc.php');

                /*VARIABLES*/
                $cc = new cc($_POST['userName'], $_POST['password']);
                if($cc)
                {
                    $strEmails = $_REQUEST['emails'];
                    $aryEmails = explode("\n", $strEmails);

                    $page = (isset($_GET['page'])) ? $_GET['page'] : 'lists';
                    $lists = $cc->get_lists($page);

                    /*METHODS*/
                    foreach ($aryEmails as $email)
                    {       
                        if($lists)
                        {
                            foreach($lists as $k => $v)
                            {
                                $list = $v['Name'];
                                $page = (isset($_GET['page'])) ? $_GET['page'] : 'members';
                                $members = $cc->get_list_members($v['id'], $page);

                                if($members)
                                {
                                    foreach($members as $k => $v)
                                    {
                                        if($v['EmailAddress'] == $email)
                                        {
                                            $strLists .= $list . ", ";
                                        }
                                    }
                                }
                            }
                        }

                        $strLists = str_replace("Array", "", $strLists);
                        $strLists = substr($strLists, 0, -2);

                        $contact = $cc->query_contacts(trim($email));

                        if($contact)
                        {
                            $strName = $contact['Name'];
                            if(is_array($strName))
                            {
                                $strName = "";
                            }

                            echo
                            (
                                "<tr><td>".$strName."</td>".
                                "<td>".$contact['EmailAddress']."</td>".
                                "<td>".$strLists."</td></tr>"
                            );
                        }

                        else
                        {
                            echo("<tr><td colspan='3'>Could not find {$email}.</td></tr>");
                        }
                    }
                }

                else
                {
                    echo "Invalid user name or password";
                }
            ?>
        </table>
    </body>
</html>

Here is the class.cc file: http://advantage-computer.com/tools/class.cc.txt

2
Try to use print_r and check whether the strName and strLists are arrays or not.Sujit Agarwal
@Coding-Freak: Thanks for the reply. When I add echo(gettype($strLists)); above $strLists .= $list . ", "; I get "boolean string string." For some reason, it's going through the loop three times, even though there are only two e-mail addresses. Print_r doesn't print out "Array" anywhere.Richard
Modify this section echo ( "<tr><td>".$strName."</td>". "<td>".$contact['EmailAddress']."</td>". "<td>".$strLists."</td></tr>" ); and print each value individually using print_r to check if its a array.Sujit Agarwal
Coding-Freak: I just replaced each with print_r, but it does the same thing. The first cell under "Lists" is blank, and the second has the list of lists.Richard
A tip is use the magic 'var_dump' to debug your var data. Try to put a 'var_dump' on each iteration and check if your data match your plan.Welington Veiga

2 Answers

1
votes

Firstly, you should break out of that loop once you have matched a record, and you are not using $k so we can remove that from the loop too, eg:

if($members)
  foreach($members as $v)
    if($v['EmailAddress'] == $email)
    {
      $strLists .= $list . ", ";
      break;
    }

I would also add a line in there to debug whats in the $list variable when it is an array:

if (is_array($list))
  var_dump($list);

I would say that whatever is setting "$_list['content']['ContactList']['Name']" in your class is not doing it correctly.

0
votes

Thanks everyone for the replies. My brother found the problem. He changed

foreach ($aryEmails as $email){
    ...
}

to

foreach ($aryEmails as $tmpEmail){ 
    $email = rtrim($tmpEmail);
    ...
}

It appears that it only matched the last line in the text area because there were still carriage returns in the e-mail array left over from the text area. He added rtrim to remove them.