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
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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.