Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should probably take a look at this answer first <a href="https://stackoverflow.com/questions/9200966/sql-array-search/9201135#9201135">SQL Array Search</a> (how to store such an information in the database). If you still want to use coma separated list, the best way is probably using <a href="http://php.net/manual/en/function.explode.php" rel="nofollow noreferrer"><code>explode()</code></a>, <a href="http://php.net/manual/en/function.array-map.php" rel="nofollow noreferrer"><code>array_map()</code></a> and <a href="http://php.net/manual/en/function.implode.php" rel="nofollow noreferrer"><code>implode()</code></a>:</p> <pre class="lang-php prettyprint-override"><code>function generateLink( $tag){ $tag = htmlspecialchars( trim( $tag)); $str = "&lt;a href=\"/tags/$tag\"&gt;$tag&lt;/a&gt;"; } $tags = explode( ',', $row['tags']); $new_tags = array_map( 'generateLink', $tags); echo implode( ', ', $new_tags); // Of course you can chain it to: echo implode( ', ', array_map( 'generateLink', explode( ',', $row['tags']))); </code></pre> <p>However if you use correct database design you should this <code>SELECT</code> (displaying one product):</p> <pre class="lang-sql prettyprint-override"><code>SELECT tags.name FROM tags_products INNER JOIN tags ON tags_products.tag_id = tags.id WHERE tags_products,product_id = ? </code></pre> <p>And with php:</p> <pre class="lang-php prettyprint-override"><code>$q = $db-&gt;query( 'mentioned select'); $result = array(); while( $row = $q-&gt;fetch_assoc()){ $result[] = genereateLink( $row['name']); } </code></pre> <p>And in case of mass listing of product to increase performance use this select with <a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat" rel="nofollow noreferrer"><code>GROUP_CONCAT</code></a>:</p> <pre class="lang-sql prettyprint-override"><code>SELECT products.id, products.name, GROUP_CONCAT(tags.name ASC SEPARATOR ', ') AS tags FROM products LEFT JOIN tags_products ON tags_producsts.product_id = products.id INNER JOIN tags ON tags_products.tag_id = tags.id GROUP BY products.id </code></pre> <p>And apply the first mentioned code on <code>$row['tags']</code> :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload