{"id":2269,"date":"2020-12-26T11:55:02","date_gmt":"2020-12-26T10:55:02","guid":{"rendered":"https:\/\/digitalia.culturanuova.net\/blog\/?p=2269"},"modified":"2021-03-07T18:28:00","modified_gmt":"2021-03-07T17:28:00","slug":"php-separate-all-items-in-a-mysql-fields","status":"publish","type":"post","link":"https:\/\/digitalia.culturanuova.net\/blog\/?p=2269","title":{"rendered":"php\/msql keywords as hashtags"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">php separate all items in a mysql field<\/h3>\n\n\n\n<p>You can use the <strong>explode<\/strong> syntax, as in the following exapmple:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$keywords = $row&#91;'keywords'];\nforeach (explode(',', $keywords) as $key) {\n    echo \"&lt;span&gt;&lt;a href=\"\\hashtag.php?tag=$key\\\"&gt;{$key}&lt;\/a&gt;&lt;\/span&gt;\";\n}<\/code><\/pre>\n\n\n\n<p>In the example we have a mysql field (keywords) with many items comma separated (such as : &#8220;truth, soul, body, mind&#8221; and so on). <\/p>\n\n\n\n<p>And we obtain to have as many links from each item toward a specific target, as they are (that is: 3 links if you have 3 items, 7 links fi you have 7 ones).<\/p>\n\n\n\n<p>In this way you can get a system of hashtags for your database keywords.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">other steps<\/h3>\n\n\n\n<p>You need another file, let we call them hashtag.php.<\/p>\n\n\n\n<p>The content of hashtag.php could be something like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php  \n \/\/hashtag.php  \n if(isset($_GET&#91;\"tag\"]))  \n {  \n      $tag = preg_replace('\/(?&lt;!\\S)#(&#91;0-9a-zA-Z]+)\/', '', $_GET&#91;\"tag\"]);\n\n\/\/to beautify and stylize, but not necessary BEGIN\n      $title=$tag;\n      include \"$root\/intell\/header-intell.inc\";      \n\/\/to beautify and stylize, but not necessary END      \n\n      $connect = mysqli_connect(\"localhost\", \"&#91;mysql user]\", \"&#91;mysql password]\", \"&#91;mysql database]\");  \n      \n      mysqli_set_charset($connect, 'utf8mb4');  \/\/ procedural style\n      \n      $query = \"SELECT * FROM &#91;your table] WHERE &#91;your fields with tags] LIKE '%\".$tag.\"%';  \n      $result = mysqli_query($connect, $query);  \n      if(mysqli_num_rows($result) > 0)  \n      {  \n           while($row = mysqli_fetch_array($result))  \n           {  \n                echo \"&lt;h2>$row&#91;title]&lt;\/h2>\n                &lt;blockquote>&lt;p>$row&#91;text]&lt;\/p>&lt;\/blockquote>\n                &lt;p>&lt;i>$row&#91;author]&lt;\/i>&lt;br \/>\";\n                $keywords = $row&#91;'keywords'];\n                foreach (explode(',', $keywords) as $key) {\n                if(trim($row&#91;\"keywords\"])==''){echo \"\";} else{echo \"&lt;span>&lt;a href=\\\"hashtag.php?tag=$key\\\">{$key}&lt;\/a>&lt;\/span>\";}\n                }\n                echo \"&lt;\/p>\";\n           }  \n      }  \n      else  \n      {  \n           echo '&lt;p>No Data Found&lt;\/p>';  \n      }  \n }  \n ?>  <\/code><\/pre>\n\n\n\n<p>Afterwards obviously you could adjust the css according to your needs.<\/p>\n\n\n\n<p>Of course you can have as many other php files as you want, where you usually store you database content, in which you can add the kewwords as hashtags, with a code like the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"&lt;\/p>&lt;p class=\\\"keywords\\\">\";\n$keywords = $row&#91;'keywords'];\nforeach (explode(',', $keywords) as $key) {\n    if(trim($row&#91;\"keywords\"])==''){echo \"\";} else{echo \"&lt;span>&lt;a href=\\\"hashtag.php?tag=$key\\\">{$key}&lt;\/a>&lt;\/span>\";}\n}\necho \"&lt;\/p>\";}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>php separate all items in a mysql field You can use the explode syntax, as in the following exapmple: In the example we have a mysql field (keywords) with many items comma separated (such as : &#8220;truth, soul, body, mind&#8221; and so on). And we obtain to have as many links from each item toward &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/digitalia.culturanuova.net\/blog\/?p=2269\" class=\"more-link\">Leggi tutto<span class=\"screen-reader-text\"> &#8220;php\/msql keywords as hashtags&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[579,566],"tags":[653,652,11,17],"class_list":["post-2269","post","type-post","status-publish","format-standard","hentry","category-database","category-webmaster","tag-hastags","tag-keywords","tag-mysql","tag-php"],"_links":{"self":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2269"}],"version-history":[{"count":14,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2269\/revisions"}],"predecessor-version":[{"id":2390,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2269\/revisions\/2390"}],"wp:attachment":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}