Friday, 6 January 2017

PHP mailing database search



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:







List of Contacts















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
(
"".
"".
""
);
}

else
{

echo("");
}
}
}

else
{
echo "Invalid user name or password";
}
?>

Name E-mail address List(s)



".$strName."".$contact['EmailAddress']."".$strLists."
Could not find {$email}.




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


Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...