KeepCool KeepCool - 10 months ago 39
MySQL Question

Add or modify part of an array as a mysql string

I currently have a column in mysql database that stores a string with delimiters so I can convert it into an array in php.

An example string looks like this:


It essentially divides it into 3 major groups with
, and 3 smaller ones with
(if there's a cleaner way, let me know):

  1. 1st number is an ID number for an article from a different table

  2. 2nd is just a number for indenting purposes

  3. 3rd is for visible or not (0 or 1).

I will have an admin section where we'll be able to re-order the major groups (i.e., move group 3 to position 2) and modify specific numbers from the sub-groups (e.g. change 72,1,0 to 72,2,0) I'm not sure how I can accomplish this.

How do I loop through these modifications while keeping the order (or new order) when reinserting into the database?

I was thinking of adding a another number to my string that would determine the position of each major group? Something like this:


But how do I loop through this and move things around?

Any help would be greatly appreciated.

Answer Source

I was interested with your question. So i create this. It may not like what you desired.

    $string = "23,1,1|72,2,0|16,3,1|";
    $explode = explode("|", $string);

    $a = array();
    for($x = 0;$x<count($explode)-1;$x++)
        $a[] = $explode[$x];
        $b[] =explode(',',substr($explode[$x], strpos($explode[$x], ",") + 1));   

        echo $b[$y][0]. '=>'. $a[$y] . '<br>';