You are here: Re: [PHP] [NEWBIE] Trying to combine array into one string « PHP « IT news, forums, messages
Re: [PHP] [NEWBIE] Trying to combine array into one string

Posted by Richard Lynch on 09/29/52 11:08

Dave wrote:
> The Situation:
> I'm retrieving some email addresses from a database. I want to be
> able assemble all the addresses into one string that I can use in the
> mail() command. At no time should there ever be more than about 5 email
> addresses collected from the database. I want all addresses to appear in
> the "To:" field so everyone receiving the email will know who else has a
> copy.

I believe some mailers may have trouble with more than one address in "To:"

You may want to put the first address in "To: " and the rest in "Cc: "

Or you can put an address in the "To: " field to keep a log of all
outgoing emails, or send it to some account that just deletes all email.

Only "Bcc: " will hide email addresses.

> The Problem:
> I can't quite figure out how to take the results of the
> mysql_query() and assemble them into a string. In my experiments so far,
> I either get a "mysql_data_seek(): Offset 0 is invalid for MySQL result
> index" error, or my mail function sends to a blank address.

The data_seek error is telling you that there were NO results at all in
that data set, I do believe...

> What I've Tried So Far:
> My own code is obviously flawed in syntax in logic, so I searched
> the manual and this list's archives under the terms "array",
> "concatenate", "convert", "append" and "string" in various combinations.
> However, while I learned some interesting tips on a variety of topics, I
> suspect I'm missing some essential description that will lead me to the
> command I need.
>
> The Question:
> How do I take the contents of an array and turn them into a single
> string?

http://php.net/implode

will take a bunch of stuff in one array and turn them into a string.

> For Reference:
> Here is the code I have. I know it's flawed, but hopefully it will
> give an indication of what I'm trying to achieve:
> --code--
> $tantoQuery = "SELECT forum_members.emailAddress AS email FROM
> forum_members, event_tanto WHERE forum_members.ID_MEMBER =
> event_tanto.tanto AND event_tanto.event ='" . $show . "'";
>
> $tantoResult = mysql_query($tantoQuery);

For debugging purposes, print out $tantoResult:

echo "tantoResult: '$tantoResult'<hr />\n";

It will either be "Resource #1" (or #2, #3, ...) if the query succeeded,
or false -- which will appear as nothing when you print it which is why
I've got that label and the apostrophes there.

> while ($tantoData = mysql_fetch_array($tantoResult))
> {
> $tantoEmail = $tantoEmail . "," . $tantoData;

Also print out $tantoData:

echo "tantoData: '$tantoData'<hr />\n";

You will quickly find out that it's an Array -- which is the ARRAY
returned from mysql_fetch_array. [Fancy that, a function that ends in
'array' returning an array :-)]

All you'll see, however is 'Array' which is not real useful.

So use var_dump($tantoData) to see what's *IN* the array:

echo "<pre>";
var_dump($tantoData);
echo "</pre><hr />\n";

Now you'll see that it is a one-element array with an index of
'emailAddress' to it.

So what you need in your code is something like:
$tantoResult .= $tantoData['emailAddress'];

Except that you'd need a comma separator like this:
$tantoResult .= ", " . $tantoData['emailAddress'];

But then you've got a bogus extra comma on the front of the whole result,
and that's no good.

You could strip it off with http://php.net/substr, but it's probably more
clean to do like this:

$tantoResult = array();
while ($tantoData = mysql_fetch_array($tantoResult))
{
$tantoResult[] = $tantoData['emailAddress'];
}

So now you've built up this array of the email addresses, and you can use
that implode function:

$tantoEmails = implode(", ", $tantoResult);

Note that implode only puts commas BETWEEN the elements it splices
together, so you won't have the extra comma at the end.

> }
>
> mail($tantoEmail, $subject, $mailcontent, $addHeaders);
> --code--
>
> Any help would be much appreciated.

You're on the right track, but when your script isn't doing what you
think, add in some print statements to see what your data actually looks
like.

Often you can then figure out where you went wrong.

Be sure to print out the data early and often -- Sometimes the "problem"
you see is really caused every early on in the script.

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация