Jump to content

Recommended Posts

I have a MYSQL database in which one field 'ID' consists of numerical values - either one or if two then separated by a semicolon-like this :

187556357 or 54654764;7558648

 

However in my frontend I wish to make a link from both of these numbers separately,So for this use the following code:

<?php
$special=mysql_fetch_array($ret);
$id=$special['ID'];
$pattern='/;/' ;
if (preg_match($pattern,$id))
{
$pc=explode(";",$id);
}
else
{
$pc=$pm;
}
?>

However it shows error in the line having preg_match saying that function must be a string on this line.

 

Link to comment
https://forums.phpfreaks.com/topic/125457-php-explode-function/
Share on other sites

Thanks a lot it did work.

I could successfully make a link with the two numbers but when I want to show them separately on frontend, they show up together like :

Instead of showing up as 1234 5678 they show up as 12345678 as underlined links.When I insert the   character between the two in the php code some space shows up in between but that is also underlined like a link.

Link to comment
https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648587
Share on other sites

How are you echoing them back out?

 

For the first number (say 1234) you'll want:

<?php echo $pc[0]; ?>

 

And for the second you'll want:

<?php echo $pc[1]; ?>

 

So if you wanted them in links:

<?php 
  echo "<a href='your.url/index.php?num=".$pc[0]."'>".$pc[0]."</a> "; 
  echo "<a href='your.url/index.php?num=".$pc[1]."'>".$pc[1]."</a>"; 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648597
Share on other sites

This is my last code.But I get an extra </a> after the value of $pc[0] and that also as a link. The problem is not yet solved.

 

<?php
if (substr_count($pm,";") >= 1){
echo"<td><a href='http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids=$pc[0]'>".$pc[0]."<\a> <a href='http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids=$pc[1]'>".$pc[1]."</a></td></tr>";
}else{
  echo"<td><a href='http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids=$pm'>".$pm."</td></tr>";
}
?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648645
Share on other sites

Problem is here:

 

 

<?php
echo"<td><a href='http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids=$pc[0]'>".$pc[0]."<\a> <a";
?>

 

 

Replace <\a> with

</a>

 

Should solve it. If not, could you please post the output code, and what is stored in $pc by doing:

 

<?php print_r($pc); ?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648657
Share on other sites

Much easier -- just do an explode on the semi-colon and use a foreach to loop through the array. If there is no semi-colon in the string you end up with a one element array. You can also use a temporary array to format the output:

<?php
$pc = explode(';',$pm);
$tmp = array();
foreach ($pc as $l)
  $tmp[] = '<a href="http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids=' . $l . '">' . $l . '</a>';
echo '<td>' . implode(' ',$tmp) . '</td></tr>';
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648658
Share on other sites

 

Replace <\a> with

</a>

 

 

Rightful advice , it did work for me,my problem is solved with this. thanks a lot.

 

However with reference to Ken's loop code of foreach loop I ( second last post) I have two points:

1.When I use the $1 variable it shows an error saying , saying syntax error , unexpected T_LNUMBER, expecting T_VARIABLE or '$' on line 28( the line with foreach ).

However when I change the $1 to an alphanumeric variable like $t1 or an alphabetical name like $t, this error disappears.

2.Instead of the desired output in table like

1234 5678

 

I get this instead in the browser :

1234

5678

like this I get :                      But I want :

Table heading                 

Name        Raman          name          Raman

Job            Teacher      Job              Teacher

Pubmed Id 1234            Pubmed id    1234 5678

5678

 

 

1)

Much easier -- just do an explode on the semi-colon and use a foreach to loop through the array. If there is no semi-colon in the string you end up with a one element array. You can also use a temporary array to format the output:

<?php
$pc = explode(';',$pm);
$tmp = array();
foreach ($pc as $l)
  $tmp[] = '<a href="http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids=' . $l . '">' . $l . '</a>';
echo '<td>' . implode(' ',$tmp) . '</td></tr>';
?>

 

Ken

 

 

Link to comment
https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-649216
Share on other sites

The variable I used was not "$1"  (one), but "$l" (small L).

 

The code snippet you showed above will not produce that output either. My code produces the same output as you your snippet.

 

Your code:

<?php
if (substr_count($pm,";") >= 1){
echo"<td><a href='http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids=$pc[0]'>".$pc[0]."</a> <a href='http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids=$pc[1]'>".$pc[1]."</a></td></tr>";
}else{
  echo"<td><a href='http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids=$pm'>".$pm."</td></tr>";
}
?>

 

My code:

<?php
$pm = '54654764;7558648;vvvvv';
$pc = explode(';',$pm);
$tmp = array();
echo '<table><tr>';
foreach ($pc as $lnk)
  $tmp[] = '<a href="http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=pubmed&list_uids=' . $l . '">' . $l . '</a>';
echo '<td>' . implode(' ',$tmp) . '</td></tr></table>';
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-649342
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.