Jump to content

[SOLVED] php implode function


raman

Recommended Posts

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('&nbsp',$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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.