0

Continuing this question:

I have this string:

$table_summary = '
    [Category Name 1=>Title x:Description y,Title q:Description n,Title r:Description m,]

    !£|£!

    [Category Name 2=>Title z:Description a,Title j:Description k,Title p:Description f,]

    !£|£!
';

As you see, there is a separator in the string as !£|£!, and I have 2 tables:

products_spec_subjects
id     product_id     val

products_spec_details
id     title     description    subject_id

I want to store the mentioned string into these tables, while category name will be stored into the 1st table, and it's details like title and description will be stored into the 2nd table and will be linked to the 1st table using subject_id as foreign key.

so after all, the mentioned string should be stored into db like (e.x $product_id=12):

products_spec_subjects
1      12      Category Name 1
2      12      Category Name 2


products_spec_details
1    Title x     Description y      1
2    Title q     Description n      1
3    Title r     Description m      1
4    Title z     Description a      2
5    Title j     Description k      2
6    Title p     Description f      2

Here is my incomplete code so far:

$technical_specifications_arr = explode('!£|£!', $table_summary);
        unset($technical_specifications_arr[count($technical_specifications_arr) - 1]);
        foreach($technical_specifications_arr as $main_value){
            $i = 0;
            $this_arr = explode('=>', $main_value);
            foreach($this_arr as $value){
                $cat_name = substr($this_arr[0], 1);
                $details = substr($this_arr[1], 0, -1);
                    if($i == 0){
                    $tech_main_insert = mysql_query("INSERT INTO products_spec_subjects (product_id, val) VALUES ('$product_id', '$cat_name')") or die(mysql_error());  
                    $this_main_id = mysql_insert_id($link);
                    }
                $i++;
                $another_arr = explode(',', $details);
                unset($another_arr[count($another_arr) - 1]);
                foreach($another_arr as $another_val){

                    $final = explode(':', $another_val);
                    foreach($final as $final_value){
                        // now I can't separate the titles and description here
                    }
                }   
            }   
        }

I appreciate any help.

5
  • what is the output of your code? i.e. what do you get when you echo out $final_value? Commented Jun 19, 2013 at 11:21
  • some f***ed up strings which has actually nothing to do with what I need! before the last foreach: foreach($another_arr as $another_val){ everything is OK, it inserts the main cats good Commented Jun 19, 2013 at 11:25
  • I'm not sure why you would need unset()s in your code, maybe they are the problem Commented Jun 19, 2013 at 11:28
  • they are needed since we have an extra !£|£! at the end of string Commented Jun 19, 2013 at 11:29
  • oh I see. @user2460294 's snippet looks promising. Commented Jun 19, 2013 at 11:33

1 Answer 1

2

You don't need to iterate the $final loop. Just pull your values out:

            foreach($another_arr as $another_val){  
                $final = explode(':', $another_val);                    
                // $final[0] = title
                // $final[1] = desc
                echo $final[0].' - '.$final[1]."<br />\n";
            }
Sign up to request clarification or add additional context in comments.

1 Comment

there is a problem here, it echoes twice the times that this should be printed, since it's also in the middle of the top foreach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.