2013-07-25 78 views
0

我不知道的術語是什麼,但基本上我有一個使用「標籤爲」系統網站,目前您可以點擊標籤,它需要用戶URLS的其他元素?

topics.php?tags=example 

我的問題是什麼樣的腳本或編碼將需要能夠添加額外的鏈接?

topics.php?tags=example&tags=example2 

topics.php?tags=example+example2 

這裏是我的地盤如何鏈接到標籤的代碼。

header("Location: topics.php?tags={$t}"); 

<a href="topics.php?tags=<?php echo $fetch_name->tags; ?>"><?php echo strtolower($fetch_name->tags);?></a> 

感謝任何提示或提示。

回答

0

topics.php?tags=example&tags=example2
會在後端中斷;

您必須將數據分配給一個變量:

topics.php?tags=example+example2

看起來不錯,你可以通過+符號訪問它在後端explode它:

//toplics.php 
<?php 
    ... 
    $tags = urlencode($_GET['tags']); 
    $tags_arr = explode('+', $tags); // array of all tags 

    $current_tags = ""; //make this accessible in the view; 
    if($tags){ 
     $current_tags = $tags ."+"; 
    } 
    //show your data 
?> 

編輯: 你可以創建前端標籤:

<a href="topics.php?tags=<?php echo $current_tags ;?>horror"> 
    horror 
</a> 
+1

我不確定加號是不錯的選擇,如果我正確地記得它是一個特殊的url字符。 –

+1

是的,加號是一個空格的特殊url字符:) – Akdr

+0

感謝您的文章,我已經使用$ _GET ['tags']部分設置了它,我唯一的支持是讓它如此,如果我有 電影恐怖 作爲標籤,我可以在「電影」,然後點擊「恐怖」,它將其添加到URL。 我用+作爲例子,因爲我見過使用該系統。 – user2571547

4

你真的無法通過標籤兩次作爲GET參數雖然你可以把它作爲一個數組

topics.php?tags[]=example&tags[]=example2 

假設這是你想嘗試

$string = "topics.php?"; 
foreach($tags as $t) 
{ 
    $string .= "tag[]=$t&"; 
} 
$string = substr($string, 0, -1); 

我們通過陣列串聯重複什麼值給我們的$字符串。最後一行除去額外的&符號的最後一次迭代

後會出現另外還有一點看起來更有點髒,但可能是更好的根據自己的需要

$string = "topics.php?tag[]=" . implode($tags, "&tag[]="); 

注意只是做另一種選擇確保標籤數組不是空的

+0

這就是我要發佈的內容 –

+2

構建這樣一個URL查詢字符串的「正確」方法是使用['http_build_query()'](http://php.net/manual/en/function.http-build- query.php):'http_build_query(array('tags'=> array('example-tag-1','example-tag-2')));' – feeela

+0

感謝您的方便功能,我從來沒有遇到過羞恥它之前 –