S
S
Stepan2015-02-11 14:11:59
MySQL
Stepan, 2015-02-11 14:11:59

How to merge values ​​from $_POST to remove unnecessary and write everything to the database?

Initially they come like this

[5] => Array(
            [post_id] => 1038
            [data_type] => photoSignImage 
            [post_data] => image.jpg
        )
    [6] => Array(
            [post_id] => 1038
            [data_type] => photoSignText
            [post_data] => some text
        )

The task is to make one out of 2 arrays. In which post_data has merged strings, and data_type changes to photo. Remove everything else.
Out like this
[5] => Array(
            [post_id] => 1038
            [data_type] => photo
            [post_data] => image.jpg|||some text
        )

I get an insanely lot of code and in the end it still doesn’t work out tell me how to implement

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Barulin, 2015-02-11
@xoma2

If I understand correctly, then at the input in one element, a picture, in the second a comment to it. In this case, everything can be stored in two tables, the first is the main entity id of which appears in the post_id key. The second is a table of photos with the address of the photo in one field and a comment to it in the second. More or less like this:

foreach ($data as $item)
{
  $insert = array();
  if ($item['data_type'] === 'photoSignImage')
  {
    $insert['path'] = $item['post_data'];
  }
  elseif ($item['data_type'] === 'photo')
  {
    $insert['comment'] = $item['post_data'];
  }
  $insert['id'] = $item['post_id'];

  db::insert_record('table', $insert);
}

F
FanatPHP, 2015-02-11
@FanatPHP

Most importantly, never store anything in the database "via |||"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question