views:

64

answers:

3

I am working on a project at the moment, that allows the user to create any number of news headlines, articles and images, the only rule with this system is that a headline must have an article and an image. My question is on my form when I submit I get 2 arrays one is the $_POST and the other is $_FILES.

$_POST

Array
(
 [campaign_title] => Another multiple test
 [campaign_keyword] => Another multiple test
 [introduction] => Another multiple test
 [campaign_headline] => Array
  (
   [0] => Another multiple test headline 1
   [1] => Another multiple test headline 2
  )

 [article] => Array
  (
   [0] => Another multiple test article 1
   [1] => Another multiple test article 2
  )

 [save_multiple] => Save
)

$_FILES

Array
(
 [article_image] => Array
  (
   [name] => Array
    (
     [0] => Intro-artists.gif
     [1] => textbg1.png
    )

   [type] => Array
    (
     [0] => image/gif
     [1] => image/png
    )

   [tmp_name] => Array
    (
     [0] => /private/var/tmp/phpwDAkGJ
     [1] => /private/var/tmp/phpmvrMDg
    )

   [error] => Array
    (
     [0] => 0
     [1] => 0
    )

   [size] => Array
    (
     [0] => 2841
     [1] => 56506
    )

  )

)

Basically the method after submitting the form is the data is saved to a database, the 3 items of the post are saved in one table, the headlines and articles are saved in another table (sent with the id of the row just inserted) and then finally the images are saved, again sent with id of the first saved row.

I am having trouble understanding how I make sure the right images gets saved with the right ID, the DB saves are done by looping through the headlines and articles, but as the images are in a different array I cannot do this and make sure they are getting saved with right foreign id, can I merge the files into the post? Currently the solution I have for the headlines and articles is this,

foreach ($data['campaign_headline'] as $key => $headline) {
    addMailerMultipleRelatedContent($mailerId, $headline, $data['article'][$key]);
}


function addMailerMultipleRelatedContent($mailerId, $headline, $article) {
    extract($data);
    //die(print_r($id));
    $id = addRelatedMultipleContent($data['introduction'], $headline, $article,
      $mailerId, mktime(), mktime());
}

function addRelatedMultipleContent($introduction, $headline, $content,
  $mailer_id, $created_at, $updated_at){
    $query = "INSERT INTO `mailer_content` (`id`, `introduction`, `headline`,
      `content`, `mailer_id`,`created_at`, `updated_at`) VALUES ";
    $query .= "(NULL, '" . makeSafe($introduction) . "', '" .
      makeSafe($headline) . "', '" . makeSafe($content) . "', '" .
      makeSafe($mailer_id) . "', " . makeSafe($created_at) . ", " .
      makeSafe($updated_at) . ");";
    $result = runInsert($query, __FUNCTION__);
    //die(print_r($result));
    return $result;
}

Is there away for me to work with images at the same time?

EDIT:

The HTML form,

<form method="post" action="/admin/editmultiple" enctype="multipart/form-data">
                    <fieldset class="toplined">
                        <label>Campaign Title</label>
                        <input type="text" name="campaign_title" value="<?echo (isset($mailers['mailer_title'])) ?  $mailers['mailer_title'] :  $_POST['campaign_title'];?>" class="extrawideinput" />
                    </fieldset>
                    <fieldset class="toplined">
                        <label>Campaign Type:</label>
                        <label>Multiple</label>
                    </fieldset>
                    <fieldset class="toplined">
                        <label>Campaign Keyword:</label>
                        <div class="forminputblock">
                            <input type="text" name="campaign_keyword" value="<?echo (isset($mailers['mailer_header'])) ?  $mailers['mailer_header'] :  $_POST['campaign_keyword'];?>" class="extrawideinput" />
                        </div>
                    </fieldset>
                    <fieldset class="toplined">
                        <label>Introduction</label>
                        <div class="forminputblock">
                            <input type="text" name="introduction" value="<?echo (isset($mailers['introduction'])) ?  $mailers['introduction'] :  $_POST['introduction'];?>" class="extrawideinput" />
                        </div>
                    </fieldset>
                    <fieldset class="toplined">
                        <label>Headline</label>
                        <div class="forminputblock">
                            <input type="text" name="campaign_headline[]" value="<?echo (isset($mailers['headline'])) ?  $mailers['headline'] :  $_POST['campaign_headline'];?>" class="extrawideinput" />
                        </div>
                    </fieldset> 
                    <fieldset class="toplined">
                        <label>Image:</label>
                        <input type="file" name="article_image[]">
                    </fieldset>
                    <fieldset class="toplined">
                        <label>Story:</label>
                        <div class="forminputblock">
                            <textarea name="article[]" class="js_editable_textarea deeptext" rows="1" cols="1"><?echo (isset($mailers['content'])) ?  $mailers['content'] :  $_POST['article'];?></textarea>
                    </fieldset>
                    <div id="result">

                    </div>
                    <fieldset class="toplined">
                    <a href="" id="makeRequest">+ Add Another New Article</a>
                    </fieldset>
                    <fieldset class="toplined">
                    <input type="submit" name="save_multiple" value="Save" />
                    </fieldset>
                </form>
A: 

EDIT: I've just recoded you an entire example of how it could easily work. (uses jquery as an example)

<?php
echo '<pre>';
print_r($_POST);

echo '</pre>';

?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;

<script type="text/javascript">

$(function() {

 var position = 1;
 $(".add").click(function(){

  var parent = $(this).parent();
  var newField = $(this).parent().clone(true).insertAfter(parent);


  /* title */
  var newName = 'articles['+ position + '][title]';
  newField.children(".name").attr("name", newName);

  /* content */
  newName = 'articles['+ position + '][content]';
  newField.children(".content").attr("name", newName); 

  /* content */
  newName = 'articles['+ position + '][checkbox]';
  newField.children(".checkbox").attr("name", newName); 
  newField.slideDown();

  position++;

 });


});


</script>
<h1>example</h1>
<form action="" method="post">

 <fieldset class="article">
  <label style="display:block">Article title</label>
  <input type="text" name="articles[0][title]" value="" class="name" />

  <label style="display:block">Article content</label>
  <textarea name="articles[0][content]" cols="40" rows="10"  class="content"></textarea>


  <label style="display:block">Checkbox</label>
  <input type="checkbox" name="articles[0][checkbox]" value="1" class="checkbox" />

  <br />
  <a href="#" class="add">add new after</a>
 </fieldset>




 <br />

 <input type="submit" value="submit" name="submit" />

</form>
Kieran Allen
not entirely sure why you have done that, this was a PHP question
sea_1987
becuase your html makes things a lot hard than they need to be.
Kieran Allen
A: 

I'm not sure that you would want to merge the two arrays, as you need to perform different actions on each array.

With the $_FILES array, the uploaded images will be stored in a temporary location, the images need to be moved to a more permanent location before being referenced in your database.

Rob Forrest
A: 

With the same key you use to access the articles sub array, you can access the different fields in the $_FILES array. Obviously you can merge the two arrays, but it isn't necessary for you to work with them.

Also, you should note that you have to copy the actual data from the temporary location to where ever you would like to permanently store it. Make sure to use the [is_uploaded_file()][1] and [move_uploaded_file()][2] methods to prevent potential attacks via file uploads.

[1]: http://www.php.net/manual/en/function.is-uploaded-file.php is_uploaded_file() [2]: http://www.php.net/manual/en/function.move-uploaded-file.php move_uploaded_file()

arikfr