Jump to content

Help with comma delimited array PHP/MySQL


kcjinc

Recommended Posts

I'm new to coding and have spent hours and hours trying to figure this out and hope someone can help me.

 

I have a database table named "catholic_music" that looks like this:

 

+--------------------------------------------------------------------------------------------------+
| ID | artist_name | artist_song | genre_1 | genre_2 | genre_3 | genre_4 | img_link | release_date |
+--------------------------------------------------------------------------------------------------+
|  1 | Sonkight    | Child       | trance  | rock    |         |         | img1.jpg | 07-07-04     |
|  2 | Equator     | Brothers    | jazz    | pop     | rock    |         | img2.jpg | 07-07-04     |
|  3 | Avila       | Justify     | rock    |         |         |         | img3.jpg | 07-07-04     | 
+--------------------------------------------------------------------------------------------------+

 

What I want to do is create a comma delimited list of the genres from the genres columns (i.e. genre_1, genre_2, genre_3, genre_4) and echo the result on the page.

 

Here is what I used but it's not working:

_______________________________________

 

 

<?
function enumget($field="genre_1,genre_2,genre_3,genre_4",$table="catholic_music") {
    $result=mysql_query("SHOW COLUMNS FROM `$table` LIKE '$field'");
    if(mysql_num_rows($result)>0){
        $row=mysql_fetch_row($result);
        $options=explode("','", preg_replace("/(enum|set)\('(.+?)'\)/","\\2", $row[1]));
    } else {
        $options=array();
    }
    return $options;
}
?>
<? echo $options; ?>

 

_______________________________________

 

 

I also tried this but it didn't work:

_______________________________________

 

 

<?
function enumget($field="rock,pop,trance,jazz",$table="catholic_music") {
    $result=mysql_query("SHOW COLUMNS FROM `$table` LIKE '$field'");
    if(mysql_num_rows($result)>0){
        $row=mysql_fetch_row($result);
        $options=explode("','", preg_replace("/(enum|set)\('(.+?)'\)/","\\2", $row[1]));
    } else {
        $options=array();
    }
    return $options;
}
?>
<? echo $options; ?>

 

Any help would be greatly appeciated. Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/77633-help-with-comma-delimited-array-phpmysql/
Share on other sites

I believe mysql has a distinct or unique  you can use try a query like

<?php 
$q = "Select DISTINCT genere_1, genere_2, genere_3, genere_4 from `catholic_music";
$r = mysql_query($q) or die(mysql_error());
if(mysql_num_rows($r) >0){
$data = mysql_fetch_array($r);
print_r($data);
?>

there is no need in this case for a while loop as we have no chance for greater than 1 row, I do not know if the sql syntax is right, i know it is that or unique, so from this just recall ti from $data['g1'] $data['g2'], etc the print_r will show you that

 

Edit:

I found the right way to use distinct now

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.