[smarty]用simplexml_load_file編輯XML

LINEで送る
[`evernote` not found]

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

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

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

edit_recruit.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
class edit_recruitModel extends tinyModel {
  function Process(){
  	// cookies確認
  	if(!$this->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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{include file="include/header.tpl"}
{include file="include/menu.tpl"}
<br/>
<div id="contents">
  <p> RECRUITの編集 </p><br/>
  <form name="recruitform" method="post" action="edit_recruit?id={$id}" enctype="multipart/form-data" >
  <table style="width:500px;">
	<tr>
		<td width="100px"> 名称(MENU用) </td>
		<td><input type="text" name="menu" value="{$list.menu}" size="72"></td>
	</tr>
	<tr>
		<td> 名称(TEXT用) </td>
		<td><input type="text" name="title" value="{$list.title}" size="72"></td>
	</tr>
	<tr>
		<td> 業務内容 </td>
		<td><input type="text" name="txt0" value="{$list.txt0}" size="72"></td>
	</tr>
	<tr>
		<td> 応募資格 </td>
		<td><input type="text" name="txt1" value="{$list.txt1}" size="72"></td>
	</tr>
	<tr>
		<td> 勤務地 </td>
		<td><input type="text" name="txt2" value="{$list.txt2}" size="72"></td>
	</tr>
	<tr>
		<td> 勤務時間 </td>
		<td><input type="text" name="txt3" value="{$list.txt3}" size="72"></td>
	</tr>
	<tr>
		<td> 給与 </td>
		<td><input type="text" name="txt4" value="{$list.txt4}" size="72"></td>
	</tr>
	<tr>
		<td> 待遇 </td>
		<td><input type="text" name="txt5" value="{$list.txt5}" size="72"></td>
	</tr>
	<tr>
		<td> 休日 </td>
		<td><input type="text" name="txt6" value="{$list.txt6}" size="72"></td>
	</tr>
	<tr>
		<td> 応募方法 </td>
		<td><input type="text" name="txt7" value="{$list.txt7}" size="72"></td>
	</tr>
	<tr>
		<td> 送付先 </td>
		<td><textarea id="txt8" name="txt8" rows="3" cols="45">{$list.txt8}</textarea></td>
	</tr>
	<tr>
		<td> お問合わせ </td>
		<td><input type="text" name="txt9" value="{$list.txt9}" size="72"></td>
	</tr>
	<tr>
		<td> 注意 </td>
		<td><textarea id="txt10" name="txt10" rows="3" cols="45">{$list.txt10}</textarea></td>
	</tr>
	<tr>
		<td> 表示状態 </td>
		<td><select name="visable">
			<option value="0" {if $list.visable==0} selected{/if}>しない</option>
			<option value="1" {if $list.visable==1} selected{/if}>する</option>
		</select></td>
	<tr>
		<td> 表示順 </td>
		<td><input type="text" name="sort" value="{$list.sort}" size="3"></td>
	</tr>
  </table>
    <div style="margin-top:5px"><input type="submit" name="change_bannerad" value="確認"></div>
  </form>
</div>
<br/>
{include file="include/footer.tpl"}