質問

Suppose that in MySQL I have a row called "favourite_websites". If there are more then 1, then they are separated by comma ( , ).

Example user 1 has "favourite_websites" set as "facebook.com"

Example user 2 has it set as "facebook.com,google.com"

Example user 3 has it set as "facebook.com, google.com"

What I need is to loop each user and then get each their website separately as

<a href='http://[website]'>[website]</a>

If there are more then 1 website in the cell then it should echo smt like this

<a href='http://[website]'>[website]</a>
<a href='http://[website]'>[website]</a>
役に立ちましたか?

解決

Convert your string to array by using explode : documentation.

Example :

<?php
    $favourites = "facebook.com, google.com";
    $favourites_array = explode(',', $favourites);
    foreach($favourites as $website){
        $website = trim($website); //remove space characters
        echo '<a href="http://'.$website.'">'.$website.'</a>';
    }
?>

他のヒント

First set your input element name as an array name, like myname[];

while posting the data,

$var1 = explode(",",$_POST['myname']);

now your query goes here,

mysql_query('INSERT INTO tblname (fieldname) values ($var1)');
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top