var_dump(implode("<br />", $array) . "<br />"); On Mon, Apr 13, 2009 at 7:07 PM, PJ <af.gourmet@xxxxxxxxxxxx> wrote: > Hi Leon & thanks. > It sure is complicated. Jim Lucas example did the trick very nicely (in > my other post - > "extract varying data from array with different formatting" but here I > am learning about other ways & means. > Redoing the arrays means redoing the queries :-( but I'll have a go at > it if I'm to learn anything. I already have a ranking column set up but > am not using it at the moment for the author display. Anyway, I have > enough to keep my neurons busy for a while. > Thanks. > > Leon du Plessis wrote: > > Hi PJ, > > > > You may want to remove the "," before the <br>...That was a slight > > oversight > > on my part....sorry.'bout that...I will leave you to do the fixing, > > but I am > > sure you get the general idea. > > > > Best wishes..Leon > > > > -----Original Message----- > > From: Leon du Plessis [mailto:leon@xxxxxxxxxx] > > Sent: 13 April 2009 06:48 PM > > To: 'PJ' > > Cc: php-general@xxxxxxxxxxxxx > > Subject: RE: what to use instead of foreach > > > > Hi PJ, > > > > Ok, If I understand correctly you can attempt to alter your code as per > > following example (I am breaking it down a little for readability): > > > > a) If you only wish to output the authors, see also Mark Kelly's example, > > You can simply output as many authors you have associated (you will > > need an > > associated array!!: > > > > Or b) I include the following alternative example: > > > > .... > > $string_out = "": > > Foreach ($my_titles as $titles) > > { > > Echo "Title: $titles By:<br>"; > > Foreach($my_authors[$title] as $author) > > $string_out .= "$author, "; /* Building string */ > > > > // Add <br> > > $string_out .= "<br>"; > > > > // Here you would replace your last comma with the "&" you want > > // There are a few ways to do this (like Mark Kelly's), but will try > > // another way (older, maybe less complicated?). > > $final_string = substr($string_out,0,strrpos($string_out,",") - 1); > > $final_string .= " & " . > > substr($string_out,strrpos($string_out,",") + 1); > > } > > .... > > > > So all you need is to modal your data around this, and you should be > > fine. > > You could construct your arrays then as follows as an example: > > > > .... > > $my_titles = array("title1","title2"); > > $my_authors["title1"] = array("a someone","a notherone"); > > $my_authors["title2"] = array("mr. a","mr. b"); > > ... > > > > and so forth...how you construct the data is then very important as > > you can > > then later use it simplify your coding as you progress and as > demonstrated > > below: > > > > In future, where the need justifies it, you can construct your array to > > already contain the needed string you want to output, it may help, but > you > > will sometimes have the same effort in constructing the data for the > > arrays, > > so it is up to you to decide which approach is going to be best: e.g. > > $my_titles = array("title1","title2"); > > $my_authors["title1"] = array("a someone, a notherone & Mr. X"); > > > > Then you can simply echo the array value: > > echo "$my_authors["title1"] . "<br>"; > > > > Hope it is enough info for to work on for now!! > > > > Have fun! > > Leon > > > > -----Original Message----- > > From: PJ [mailto:af.gourmet@xxxxxxxxxxxx] > > Sent: 13 April 2009 04:33 PM > > To: Leon du Plessis > > Cc: php-general@xxxxxxxxxxxxx > > Subject: Re: what to use instead of foreach > > > > Hi Leon, > > Thanks for the suggestion; I'm quite new to all this, so it's a bit > > complicated for my peanut brain. > > I have already tried with several count and for schemes. None work > > because foreach ignores any counters once in the loop. Also, this > > foreach is nested within another foreach; don't know if that affects > > anything. > > I'll try to understand the second suggestion using for. I'll see what > > comes up. > > There are actually several conditions that have to be met: > > 1. if only 1 author = echo "author<br>" > > 2. if 2 authors = echo "author & author1<br>" > > 3. if more than 2 authors = echo "author, author1, author2 & author3<br>" > > That's what makes it a "toughie" > > > > Leon du Plessis wrote: > >> You may try something basic like: > >> > >> $b = 1; > >> foreach ($my_array as $a) > >> { > >> echo " $a "; > >> > >> //Send new line to browser > >> if ($b++ == 3) { echo "<br>"; $b = 1; } > >> } > >> > >> Or there are some different ways to approach this also like: > >> for ($a =& current($my_array); $a; $a = next($my_array)) > >> { > >> //Format 1 > >> echo " $a "; > >> $a = next($my_array); > >> > >> //Format 2 > >> /* you may add checks here to see if $a contains data */ > >> echo " ~ $a ~ "; $a = next($my_array); > >> > >> //Format 3 + NEW LINE > >> /* you may add checks here to see if $a contains data */ > >> echo " ~~ $a ~~<br> "; > >> } > >> > >> This way you have some added control over the iteration through the > >> array, > >> and you can play around with when & how to display what. > >> > >> Regards. > >> > >> -----Original Message----- > >> From: PJ [mailto:af.gourmet@xxxxxxxxxxxx] > >> Sent: 12 April 2009 08:57 PM > >> To: php-general@xxxxxxxxxxxxx > >> Subject: what to use instead of foreach > >> > >> foreach does not allow for different formatting for output... > >> What could be used as a workaround? > >> example: > >> echo $some_result, "<br>"; // will print all results in 1 column > >> echo $some_result, ","; // will print all results comma-separated in > >> 1 row > >> > >> But how do you get result1, result2 & result3 // with <br> at end ? > > > > > -- > unheralded genius: "A clean desk is the sign of a dull mind. " > ------------------------------------------------------------- > Phil Jourdan --- pj@xxxxxxxxxxxxx > http://www.ptahhotep.com > http://www.chiccantine.com/andypantry.php > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >