我目前正在開發一個項目,允許用戶創建任意數量的新聞標題,文章和圖片,這個系統的唯一規則是標題必須有文章和圖片。我的問題是在我的表單上,當我提交時,我得到2個數組,其中一個是$_POST
,另一個是$_FILES
。合併PHP數組
$ _ 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 ) ) )
基本上提交表單的數據保存到數據庫後,該方法中,後的3個項目被保存在一個表格中,標題和文章被保存在另一個表格中(與剛剛插入的行的ID一起發送),然後保存圖像,再次以第一次保存的ID發送d行。
我很難理解我如何確保正確的圖像被保存爲正確的ID,DB保存是通過循環播放標題和文章完成的,但由於圖像位於不同的陣列中,我無法做到這一點,確保他們得到正確的外國身份證保存,我可以將文件合併到帖子?目前我的頭條和文章的解決方案是這樣的,
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;
}
我有沒有同時使用圖像?
編輯:
HTML表單,
<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>
請,發送的HTML代碼的片段。通常關鍵是表單域的命名。 – 2010-07-13 09:55:40
請看我的編輯 – 2010-07-13 10:03:11