raman Posted September 23, 2008 Share Posted September 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/ Share on other sites More sharing options...
dave_sticky Posted September 23, 2008 Share Posted September 23, 2008 Try this instead: <?php $special = mysql_fetch_array($ret); $id = $special['ID']; if(substr_count($id,";") >= 1) { $pc = explode(";",$id); } else { $pc = $pm; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648576 Share on other sites More sharing options...
raman Posted September 23, 2008 Author Share Posted September 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648587 Share on other sites More sharing options...
asmith Posted September 23, 2008 Share Posted September 23, 2008 Post your last code. Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648595 Share on other sites More sharing options...
dave_sticky Posted September 23, 2008 Share Posted September 23, 2008 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648597 Share on other sites More sharing options...
raman Posted September 23, 2008 Author Share Posted September 23, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648645 Share on other sites More sharing options...
dave_sticky Posted September 23, 2008 Share Posted September 23, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648657 Share on other sites More sharing options...
kenrbnsn Posted September 23, 2008 Share Posted September 23, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648658 Share on other sites More sharing options...
dave_sticky Posted September 23, 2008 Share Posted September 23, 2008 Yep. That's much better than mine. Less variables, less code... Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-648669 Share on other sites More sharing options...
raman Posted September 24, 2008 Author Share Posted September 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-649216 Share on other sites More sharing options...
Zane Posted September 24, 2008 Share Posted September 24, 2008 variables can't begin with a number... change $1 to something like $var Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-649222 Share on other sites More sharing options...
kenrbnsn Posted September 24, 2008 Share Posted September 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125457-php-explode-function/#findComment-649342 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.