RE: [PHP] [NEWBIE] Trying to combine array into one string
Try loading he email address to an array from your query; $addressarray[] = $address; and when done extracting the rows from your database, parse them into a comma separated string using $listofaddresses = implode(, ,$addressarray); HTH, Warren Vail -Original Message- From: Dave [mailto:[EMAIL PROTECTED] Sent: Tuesday, February 15, 2005 7:37 AM To: php-general Subject: [PHP] [NEWBIE] Trying to combine array into one string PHP General, 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. 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. 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? 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); while ($tantoData = mysql_fetch_array($tantoResult)) { $tantoEmail = $tantoEmail . , . $tantoData; } mail($tantoEmail, $subject, $mailcontent, $addHeaders); --code-- Any help would be much appreciated. -- Dave Gutteridge [EMAIL PROTECTED] Tokyo Comedy Store http://www.tokyocomedy.com/english/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [NEWBIE] Trying to combine array into one string
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. no guarantee it works: $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) or die('Query failed: ' . mysql_error()); while ($tantoData = mysql_fetch_array($tantoResult, MYSQL_ASSOC)) { $tantoEmail[] = $tantoData['email']; } if (count($tantoEmail) 0) { mail(implode(',',$tantoEmail), $subject, $mailcontent, $addHeaders); } mysql_free_result($tantoResult); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [NEWBIE] Trying to combine array into one string
Use implode: http://nl2.php.net/manual/en/function.implode.php On 15 Feb 2005, at 16:37, Dave wrote: PHP General, 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. 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. 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? 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); while ($tantoData = mysql_fetch_array($tantoResult)) { $tantoEmail = $tantoEmail . , . $tantoData; } mail($tantoEmail, $subject, $mailcontent, $addHeaders); --code-- Any help would be much appreciated. -- Dave Gutteridge [EMAIL PROTECTED] Tokyo Comedy Store http://www.tokyocomedy.com/english/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] [NEWBIE] Trying to combine array into one string
Dave mailto:[EMAIL PROTECTED] on Tuesday, February 15, 2005 7:37 AM said: 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. http://php.net/implode There's also the .= operator which concatenates. HTH, Chris. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [NEWBIE] Trying to combine array into one string
Hello Dave, Tuesday, February 15, 2005, 8:37:02 AM, you wrote: D while ($tantoData = mysql_fetch_array($tantoResult)) D { D $tantoEmail = $tantoEmail . , . $tantoData; D } D mail($tantoEmail, $subject, $mailcontent, $addHeaders); Everything looked good up to here. $tantoEmail = $tantoEmail . , . $tantoData['email']; I'm not that great at SQL syntax, but this should work. That'll at least get your e-mail addresses in there. The next problem is that, and for purposes of demonstration we'll say you pulled out three addresses, you're going to have a comma before the first e-mail address. i.e. ,address1,address2,address3 because the first time through the while, $tantoEmail is empty, then you put a comma and the first address. So, you'd need to $tantoEmail = ltrim($tantoEmail, ,); that. You can further shorten that to: $tantoEmail .= , . $tantoData['email']; but you'd still need to ltrim that to get rid of the first comma. Another way to do this to avoid having to worry about that pesky comma. while ($tantoData = mysql_fetch_array($tantoResult)) { if ($tantoEmail != ) $tantoEmail .= , . $tantoData['email']; else $tantoEmail = $tantoData['email']; } -- Leif (TB lists moderator and fellow end user). Using The Bat! 3.0.2.3 Rush under Windows XP 5.1 Build 2600 Service Pack 2 on a Pentium 4 2GHz with 512MB -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [NEWBIE] Trying to combine array into one string
Dave wrote: PHP General, 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. 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. 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? 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); while ($tantoData = mysql_fetch_array($tantoResult)) { $tantoEmail = $tantoEmail . , . $tantoData; } $tantoData is an array so you can't just concat it into a string. change the body of the while loop to something like: if (!isset($tantoEmail)) { $tantoEmail = $tantoData['email']; } else { $tantoEmail .= , . $tantoData['email']; } or do something like $tantoEmail = array(); while ($tantoData = mysql_fetch_array($tantoResult)) { $tantoEmail[] = $tantoData['email']; } if ($tantoEmail = join(',',$tantoEmail)) { mail($tantoEmail, $subject, $mailcontent, $addHeaders); } mail($tantoEmail, $subject, $mailcontent, $addHeaders); --code-- Any help would be much appreciated. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [NEWBIE] Trying to combine array into one string
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 /prehr /\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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [NEWBIE] Trying to combine array into one string [SOLVED]
Matt, Warren, Gareth, Chris, Jochem, Ospinto, Richard, Thank you all for your helpful suggestions. implode() is the command I needed, and I've applied syntax similar to Matt's suggestion. My problem is now solved. Your responses are all much appreciated. -- Dave Gutteridge [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php