Re: implode() & explode() in arrays - Newbie

From: Date: Sat, 10 May 2003 22:57:44 +0000
Subject: Re: implode() & explode() in arrays - Newbie
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-147155@lists.php.net to get a copy of this message
Mailing wrote: > I have a db that contains categories. I want to select all categories that that have had new > activity today, and than send email to the subscribers of those categories. > > When I look at the array results they are all in one unseparated line. eg: > SportFoodDrinkHotels....etc and my call on the database would only give me the results of people > with all those categories selected, Eg If I had 2 categories with new activity today, sport and > Bio-Tech, it will only give me the email addresses of people with both those categories, whereas I > need to choose all email address with Sport OR Bio-Tech OR Sport , [and] Bio-Tech. > > I thought that I would need to implode () adding a delimiter and then explode() into an array. > Here is my code: > > $sql_cat ="select catname from category where latest_date = '$today' > "; > $result = @mysql_query($sql_cat) or die(mysql_error()); > while ($row =mysql_fetch_array($result)) > { > > $catname = $row["catname"]; > $catid = $row["catid"]; > $chosen_cat= implode(",", $catname); This is "throwing away" the previous categories on each iteration, and leaving you with only the LAST $catname. There is nothing to implode, really, since $catname is a string, not an array... And, actually, PHP might be taking that string, forcing each character into a separate element, and then giving you: F,o,o,dT,r,a,v,e,l... Or maybe not. I dunno *WHAT* PHP does when you give it a single string when you were supposed to give it an array for implode. That said, don't use implode here at all. Do something like: $catids[] = $catid; This will add each new ID to a list (array) of ids that you are storing. > > } > Huge chunks of code are missing here?.... Or did you just type in $catz out of the blue for fun? :-) At any rate, you could now do something like: $catids_sql = implode(", ", $catids); //So you'd have like "5, 7, 13, 42" as the IDs of the categories //with activity today. Then you could do: select email from subscriptions where catid in ($catids_sql) This will let you get everybody's email that is in those categories. However, if you need to know who is in WHICH categories as you email them, to customize the email, this won't work, since they'll be all jumbled together. You could use: select catid, email from subscriptions where catid ($catids_sql) order by email, catid and then, as you iterate through, if you hit the same email more than one, don't send a message, but tack on which categories have been active in the body of the email. I'm just a bit unclear exactly WHAT you are trying to accomplish here, though... Notification that *some* unspecified category has been active today? Notification that categories X, Y, and Z were active today? Something else entirely? You could even do the whole damn thing in one (1) SQL query which would probably be more efficient. But, depending on how many categories you have, and how many subscribers you have, could actually be less efficient than the de-normalized approach.

« previous php.general (#147155) next »