Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have an mysql column explode method

here is the code :

$barang2=mysql_query("select * from tbl_barang WHERE INDX=2");
while($c=mysql_fetch_array($barang2))    {              
    $result = "$c[DESKRIPSI]<br />";

    $rows = explode("#", $result);

    for($i = 0; $i < count($rows); $i++) {
        $temp   = explode(':', $rows[$i]);
        $IDK    = $temp[1];
        $ID    = array("ID Kategori : ","Kategori : ","Warna : ","Ukuran : ","B : ");



        //echo '<br />' . $ID . ' : ';
        echo $ID[0]. ' : '. $IDK[0] . '<br />';
        echo $ID[1]. ' : '. $IDK[1] . '<br />';
        echo $ID[2]. ' : '. $IDK[2] . '<br />';
        echo $ID[3]. ' : '. $IDK[3] . '<br />';
        echo $ID[4]. ' : '. $IDK[4] . '<br />';
    }
}

i get the column on mysql where its like IDK:16#K:BAJU#W:HITAM#U:L#B:120G

then trying to get it like

ID Kategori : 16
Kategori : BAJU
Warna : HITAM
Ukuran : L
B : 120G

but i cant call the offset like $IDK[0] and join the first array...

share|improve this question
1  
STOP using mysql_ functions as they have been deprecated. Switch to mysqli_ or PDO. (going with the theme). $result does not have rows in it, it is a single array. There is no need for a for loop here. –  Jay Blanchard Jul 8 at 13:19
    
i try single echo like echo $ID . ' : '. $IDK . '<br />'; but the $ID doesnt comes up with my every single array... –  user3816415 Jul 8 at 13:30

1 Answer 1

You appear to have a delimited data structure in a field in the table. This tends to be horrible to process.

But if you really must use it then I think what you are trying to put out could be done like this:-

$barang2=mysql_query("select * from tbl_barang WHERE INDX=2");
while($c=mysql_fetch_array($barang2))    
{              
    $fields = explode("#", $c[DESKRIPSI]);

    $ID = array("ID Kategori : "=>'',"Kategori : ">'',"Warna : ">'',"Ukuran : ">'',"B : ">'');
    foreach($fields AS $field_key=>$field_value)
    {
        $temp   = explode(':', $field_value);
        switch($temp[0])
        {
            case 'IDK':
                $ID["ID Kategori : "] = $temp[1];
                break;
            case 'K':
                $ID["Kategori : "] = $temp[1];
                break;
            case 'W':
                $ID["Warna : "] = $temp[1];
                break;
            case 'U':
                $ID["Ukuran : "] = $temp[1];
                break;
            case 'B':
                $ID["B : ">''] = $temp[1];
                break;
            default:
                // nothing
                break;
        }

        foreach($ID AS $key=>$value)
        {
            echo $key. $value . '<br />';
        }
    }
}

But it would be better to redesign your database to avoid the issue in the first place.

share|improve this answer
    
it comes to be multiple $key and $value and echo an random number :( –  user3816415 Jul 8 at 15:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.