[smarty]用simplexml_load_file編輯XML

利用simplexml_load_file函式快速的編輯XML檔案
這樣一般的網站(尤其是FLASH)根本就不用資料庫了
這邊的例子是一個人才招募的頁面的XML

比較要注意的只有edit_recruit.php中的第19行
當節點不在要新增時,必須先指定給他一個新父節點

再來就是第22行
如果同一個節點(body)有很多項目時
第一個不能用body[0]的方式新增值進去

edit_recruit.php:

IsLogined()){
      $this->ActionForward('top');
  	}

    $form = $_REQUEST;
    $id = GetInputValue("id");
    $this->AssignForm("id",$id);


    if(isset($_POST['change_bannerad'])) {
	    // チェック変更
	    $cxml = simplexml_load_file(XML_DIR.'recruit.xml');
	    $max = count($cxml->recruit);
	    $id = $id?(int)$id-1:$max;
	    $cxml->recruit[(int)$id] = "";
		$cxml->recruit[(int)$id]->menu = $form["menu"];
		$cxml->recruit[(int)$id]->title = $form["title"];
		$cxml->recruit[(int)$id]->body = $form["txt0"];
		$cxml->recruit[(int)$id]->body[0]["txt"] = "業務内容";
		$cxml->recruit[(int)$id]->body[1] = $form["txt1"];
		$cxml->recruit[(int)$id]->body[1]["txt"] = "応募資格";
		$cxml->recruit[(int)$id]->body[2] = $form["txt2"];
		$cxml->recruit[(int)$id]->body[2]["txt"] = "勤務地";
		$cxml->recruit[(int)$id]->body[3] = $form["txt3"];
		$cxml->recruit[(int)$id]->body[3]["txt"] = "勤務時間";
		$cxml->recruit[(int)$id]->body[4] = $form["txt4"];
		$cxml->recruit[(int)$id]->body[4]["txt"] = "給与";
		$cxml->recruit[(int)$id]->body[5] = $form["txt5"];
		$cxml->recruit[(int)$id]->body[5]["txt"] = "待遇";
		$cxml->recruit[(int)$id]->body[6] = $form["txt6"];
		$cxml->recruit[(int)$id]->body[6]["txt"] = "休日";
		$cxml->recruit[(int)$id]->body[7] = $form["txt7"];
		$cxml->recruit[(int)$id]->body[7]["txt"] = "応募方法";
		$cxml->recruit[(int)$id]->body[8] = $form["txt8"];
		$cxml->recruit[(int)$id]->body[8]["txt"] = "送付先";
		$cxml->recruit[(int)$id]->body[9] = $form["txt9"];
		$cxml->recruit[(int)$id]->body[9]["txt"] = "お問合わせ";
		$cxml->recruit[(int)$id]->body[10] = $form["txt10"];
		$cxml->recruit[(int)$id]->body[10]["txt"] = "";
		$cxml->recruit[(int)$id]["sort"] = $form["sort"];
		$cxml->recruit[(int)$id]["visable"] = $form["visable"];
		//$profile_xml = new SimpleXMLElement($xml);
		$cxml->asXML(XML_DIR.'recruit.xml');
		$this->ActionForward('recruit_mst');
    }

    //XML
	$cxml = $id?simplexml_load_file(XML_DIR.'recruit.xml'):simplexml_load_file(XML_DIR.'def_recruit.xml');
	$id--;
	$thisRecruit["menu"] =  $cxml->recruit[(int)$id]->menu;
	$thisRecruit["title"] =  $cxml->recruit[(int)$id]->title;
	$thisRecruit["txt0"] =  $cxml->recruit[(int)$id]->body[0];
	$thisRecruit["txt1"] =  $cxml->recruit[(int)$id]->body[1];
	$thisRecruit["txt2"] =  $cxml->recruit[(int)$id]->body[2];
	$thisRecruit["txt3"] =  $cxml->recruit[(int)$id]->body[3];
	$thisRecruit["txt4"] =  $cxml->recruit[(int)$id]->body[4];
	$thisRecruit["txt5"] =  $cxml->recruit[(int)$id]->body[5];
	$thisRecruit["txt6"] =  $cxml->recruit[(int)$id]->body[6];
	$thisRecruit["txt7"] =  $cxml->recruit[(int)$id]->body[7];
	$thisRecruit["txt8"] =  $cxml->recruit[(int)$id]->body[8];
	$thisRecruit["txt9"] =  $cxml->recruit[(int)$id]->body[9];
	$thisRecruit["txt10"] =  $cxml->recruit[(int)$id]->body[10];
	$thisRecruit["sort"] =  $cxml->recruit[(int)$id]["sort"];
	$thisRecruit["visable"] =  $cxml->recruit[(int)$id]["visable"];


	$this->AssignForm("list",$thisRecruit);
  }

}
?>

edit_recruit.tpl:

{include file="include/header.tpl"}
{include file="include/menu.tpl"}

RECRUITの編集


名称(MENU用)
名称(TEXT用)
業務内容
応募資格
勤務地
勤務時間
給与
待遇
休日
応募方法
送付先
お問合わせ
注意
表示状態
表示順

{include file="include/footer.tpl"}