raman Posted November 3, 2008 Share Posted November 3, 2008 I have a Mysql database in which I have a field which has more than one data entry which I have stored as 45GH6YT;HYT87686;9009JUYH In the browser however I want to give a link for each of the entries and show them separated by a space.I am trying to use the implode function but donot get the desired output. I have tried implode(' ',$pc) as well as implode(' ',$pc) , but I don't get the separation.This is my loop.: echo"<tr> <th BGCOLOR='#99CCFF' width='20' >Pubmed Id </th>"; $pc=explode(";",$pm); echo"<td>"; foreach($pc as $t) { $tmp=array(); $tmp[]='<a href="http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids='.$t.'">'.$t.'</a>'; echo ''.implode(" ",$tmp) .''; // $rety=implode(' ',$tmp); // echo "$rety"; } echo"</td></tr>"; Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 3, 2008 Share Posted November 3, 2008 Do it outside the loop, and also declare your $tmp array outside the loop, otherwise you'll just be overwriting it each time. echo"<tr> <th BGCOLOR='#99CCFF' width='20' >Pubmed Id </th>"; $pc=explode(";",$pm); echo"<td>"; $tmp=array(); foreach($pc as $t) { $tmp[]='<a href="http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids='.$t.'">'.$t.'</a>'; } echo implode(" ", $tmp); echo"</td></tr>"; Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 3, 2008 Share Posted November 3, 2008 Why are you storing multiple pieces of information in a single field? Chances are, you should be using another table. Quote Link to comment Share on other sites More sharing options...
raman Posted November 5, 2008 Author Share Posted November 5, 2008 I am not storing multiple pieces of information but different ids for the same entry in a field separated by a semi-colon(. ***************************** I want to overwrite it every time,that's why I have put it in the loop,so that everytime it goes to the next row of the table it is overwritten. **************************** MY PROBLEM REMAINS UNSOLVED. Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted November 5, 2008 Share Posted November 5, 2008 your problem is that $tmp is always an array that contains only 1 element, so imploding it around a " " does nothing - the single element becomes a string as is. this does what i think you want: <?php $pm = "45GH6YT;HYT87686;9009JUYH"; echo"<tr> <th BGCOLOR='#99CCFF' width='20' >Pubmed Id </th>"; $pc=explode(";",$pm); echo"<td>"; foreach($pc as $t) { // $tmp=array(); $tmp='<a href="http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids='.$t.'">'.$t.'</a>'; echo $tmp." "; // implode(" ",$tmp); // $rety=implode(' ',$tmp); // echo "$rety"; } echo"</td></tr>"; ?> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 5, 2008 Share Posted November 5, 2008 What? How can it only contain one element. <?php $string = "45GH6YT;HYT87686;9009JUYH"; $pc=explode(";",$string); echo"<td>"; $tmp=array(); foreach($pc as $t) { $tmp[]='<a href="http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids='.$t.'">'.$t.'</a>'; } echo implode(" ", $tmp); ?> It will contain 3 elements, and that code will display the links separated by a space. I tested it. Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted November 5, 2008 Share Posted November 5, 2008 yes, done your way. but he specified that he doesn't want to define $tmp outside the loop, because he wanted the contents of $tmp overwritten each time the loop iterates. look up 1 post before mine. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 5, 2008 Share Posted November 5, 2008 That makes no sense. Both of our codes produce the exact same output... I want to overwrite it every time,that's why I have put it in the loop,so that everytime it goes to the next row of the table it is overwritten. What? Sorry, but really... This doesn't make sense to me. Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted November 5, 2008 Share Posted November 5, 2008 i agree with out completely. i was just explaining why his way wasn't working for him. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 5, 2008 Share Posted November 5, 2008 Oh, my bad. Sorry. Quote Link to comment Share on other sites More sharing options...
raman Posted November 7, 2008 Author Share Posted November 7, 2008 THE CODE given by bobbinsbro works FINE FOR ME but I am still wondering why the implode function did not work for me as I wanted it to work inside the loop and overwritten everytime it came to the next row of the MYsql table. If someone can tell this , nice but anyway thanks to bobbinsbro. Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted November 7, 2008 Share Posted November 7, 2008 well, you redeclared $tmp as an empty array() in every iteration of the loop. then you set element 0 to contain the <a></a> tags. then you imploded only 1 element around the space (" ") character. implode does not append the $glue onto the end of each element, but rather performs a concatenation of all elements, separating them by the $glue. so when you only have 1 element, the concatenation is simply: $oneElement = $oneElement; and not: $oneElement = $oneElement.$glue; and so, declaring $tmp as an array was doing nothing for you. so i dropped that, and now each iteration of the loop, the string variable $tmp gets overwitten, like you wanted, and a space character is concatenated to it on echo, instead of the unsuccessful implode. that's as thorough an explanation as i can manage. Quote Link to comment Share on other sites More sharing options...
raman Posted November 7, 2008 Author Share Posted November 7, 2008 Thanks bobbinsbro Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted November 7, 2008 Share Posted November 7, 2008 np. please set topic to solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.