Create links with tags registered in the database

0

I have a field in MySQL with the following content:

Alesso; Anitta; Billboard; Marshmello; Poo Bear

I need to get each of the values separately and create a link like this:

<a href="#">Alesso</a>

I can not read each value individually.

    
asked by anonymous 16.08.2017 / 22:57

4 answers

2

You need to break string into a array , use the explode method of PHP : Explode :

$arr = 'Alesso; Anitta; Billboard; Marshmello; Poo Bear';
$EXP = explode(';', $arr);
foreach($EXP as $link)
{
      echo '<a href="#">'.$link.'</a>'.'<br>';
}
    
16.08.2017 / 23:02
1

If this field is always in this pattern, you can do it in mysql with CONCAT and REPLACE, eg:

SELECT
    -- Aqui você substitui o x.nomes pela sua tabela.campo
    CONCAT('<a href="#">' , REPLACE(x.nomes, '; ', '</a><a href="#">') , '</a>') AS links
FROM
    -- Aqui você substitui pela sua tabela
    (SELECT 'Alesso; Anitta; Billboard; Marshmello; Poo Bear' AS nomes) AS x;
    
16.08.2017 / 23:36
1

example - ideone

$str = 'Alesso; Anitta; Billboard; Marshmello; Poo Bear';
//para evitar de retirar espaços em nomes compostos
$output  = str_replace("; ", ";", $str);
$result = explode(';', $output);
foreach($result as $link)
{
  echo "<a href='#'>".$link."</a>\n";
}
  • str_replace - replaces all occurrences of the search string with the replacement string
  • explode - splits a major string into smaller parts based on a divider character
17.08.2017 / 00:51
0

The best way is to use a repeat system

$sql = "select coluna from tabela";

$result = mysqli_query($conexao, $sql);

while($dados = mysqli_fecth_array($result)) {
 echo "<a href=".base64_encode($dados['coluna']).".php>".$dados['coluna']."</a>";
}

Remember that a base64 encrypted link was created. The advantage is when there are "" spaces, which is not accepted as a url by apache, they are encrypted. "

To decrypt use

base64_decode($string);
    
17.08.2017 / 02:35