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>"; Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/ 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>"; Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-681096 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. Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-681102 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. Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-682820 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-682831 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. Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-682835 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. Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-682842 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. Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-682847 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. Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-682851 Share on other sites More sharing options...
JasonLewis Posted November 5, 2008 Share Posted November 5, 2008 Oh, my bad. Sorry. Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-682854 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. Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-684400 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. Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-684407 Share on other sites More sharing options...
raman Posted November 7, 2008 Author Share Posted November 7, 2008 Thanks bobbinsbro Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-684453 Share on other sites More sharing options...
bobbinsbro Posted November 7, 2008 Share Posted November 7, 2008 np. please set topic to solved. Link to comment https://forums.phpfreaks.com/topic/131185-solved-php-implode-function/#findComment-684478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.