PHP、simplexml和foreach

PHP、simplexml和foreach,php,foreach,simplexml,Php,Foreach,Simplexml,这是我第一次在这里问问题,我也是PHP和简单XML的新手,但我决心学习这一点 问题:我的第一个foreach工厂 第二个将publiccss.tpl复制到两个dir中,并忽略其他2 $xml = simplexml_load_file($target_path); if($xml['product'] == 'style'){ $sname = str_replace(' ','_',$xml['sname']); $ver

这是我第一次在这里问问题,我也是PHP和简单XML的新手,但我决心学习这一点

问题:我的第一个foreach工厂

第二个将publiccss.tpl复制到两个dir中,并忽略其他2

    $xml = simplexml_load_file($target_path);
if($xml['product'] == 'style'){
$sname =                                 str_replace(' ','_',$xml['sname']);
$vers = $xml['version'];
$sp = 'library/templates/'.$sname;
if($i = mysql_query("INSERT INTO skins VALUES ('". mysql_insert_id() ."','". str_replace('_',' ',$sname) ."','". $vers ."','".$sp."')")){
if(!is_dir($sp)) mkdir($sp,0777);
foreach($xml->templategroup as $dir){
$dir = str_replace(' ','_',$dir['name']);
$p = 'library/templates/'.$sname.'/'.$dir;
if(!is_dir($p)) mkdir($p,0777);
foreach($xml->templategroup->template as $tpl){
$p = 'library/templates/'.$sname.'/'.$dir.'/'.str_replace(' ','_',$tpl['name']);            
$fp = fopen($p,"wb");
fwrite($fp,$tpl);
fclose($fp);
}
}
}else{
echo mysql_error();
}
}else{
echo 'Not a style';
}
}


<?xml version="1.0" encoding="ISO-8859-1"?>

<style sname="DSkin" version="1.0.0" product="style">
<templategroup name="CSS Templates">
<template name="PublicCSS.tpl" date="" username="Andy?">
#body{}
</template>
</templategroup>
<templategroup name="Forum Home">
<template name="Forumhome.tpl" date="" username="Andy?">
Main index page.
</template>
<template name="Forumhome_L.tpl" date="" username="Andy?">
Level one forums.       
</template>

</templategroup>
</style>    $xml = simplexml_load_file($target_path);
if($xml['product'] == 'style'){
$sname =                                 str_replace(' ','_',$xml['sname']);
$vers = $xml['version'];
$sp = 'library/templates/'.$sname;
if($i = mysql_query("INSERT INTO skins VALUES ('". mysql_insert_id() ."','". str_replace('_',' ',$sname) ."','". $vers ."','".$sp."')")){
if(!is_dir($sp)) mkdir($sp,0777);
foreach($xml->templategroup as $dir){
$dir = str_replace(' ','_',$dir['name']);
$p = 'library/templates/'.$sname.'/'.$dir;
if(!is_dir($p)) mkdir($p,0777);
foreach($xml->templategroup->template as $tpl){
$p = 'library/templates/'.$sname.'/'.$dir.'/'.str_replace(' ','_',$tpl['name']);            
$fp = fopen($p,"wb");
fwrite($fp,$tpl);
fclose($fp);
}
}
}else{
echo mysql_error();
}
}else{
echo 'Not a style';
}
}


<?xml version="1.0" encoding="ISO-8859-1"?>

<style sname="DSkin" version="1.0.0" product="style">
<templategroup name="CSS Templates">
<template name="PublicCSS.tpl" date="" username="Andy?">
#body{}
</template>
</templategroup>
<templategroup name="Forum Home">
<template name="Forumhome.tpl" date="" username="Andy?">
Main index page.
</template>
<template name="Forumhome_L.tpl" date="" username="Andy?">
Level one forums.       
</template>

</templategroup>
</style>
$xml=simplexml\u加载文件($target\u路径);
如果($xml['product']=='style'){
$sname=str_replace(“”,''''xml['sname']);
$vers=$xml['version'];
$sp='library/templates/'。$sname;
if($i=mysql\u查询(“插入到皮肤值(''..mysql\u INSERT\u id()','.'.str\u替换('.''.''.'.'.$sname.','.''.$vers.''.''.'.''),'$sp.'')){
如果(!is_dir($sp))mkdir($sp,0777);
foreach($xml->templategroup作为$dir){
$dir=str_replace('','',$dir['name']);
$p='library/templates/'.$sname./'.$dir;
如果(!is_dir($p))mkdir($p,0777);
foreach($xml->templategroup->templateas$tpl){
$p='library/templates/'.$sname./'.$dir./'.str_replace('''.''.'''.'替换,$tpl['name']);
$fp=fopen($p,“wb”);
fwrite($fp,$tpl);
fclose($fp);
}
}
}否则{
echo mysql_error();
}
}否则{
呼应“不是一种风格”;
}
}
#主体{}
主索引页。
一级论坛。
$xml=simplexml\u加载文件($target\u path);
如果($xml['product']=='style'){
$sname=str_replace(“”,''''xml['sname']);
$vers=$xml['version'];
$sp='library/templates/'。$sname;
if($i=mysql\u查询(“插入到皮肤值(''..mysql\u INSERT\u id()','.'.str\u替换('.''.''.'.'.$sname.','.''.$vers.''.''.'.''),'$sp.'')){
如果(!is_dir($sp))mkdir($sp,0777);
foreach($xml->templategroup作为$dir){
$dir=str_replace('','',$dir['name']);
$p='library/templates/'.$sname./'.$dir;
如果(!is_dir($p))mkdir($p,0777);
foreach($xml->templategroup->templateas$tpl){
$p='library/templates/'.$sname./'.$dir./'.str_replace('''.''.'''.'替换,$tpl['name']);
$fp=fopen($p,“wb”);
fwrite($fp,$tpl);
fclose($fp);
}
}
}否则{
echo mysql_error();
}
}否则{
呼应“不是一种风格”;
}
}
#主体{}
主索引页。
一级论坛。

编写第二个循环的方式是返回到文档的根目录,SimpleXML不知道需要哪个
templategroup
,因此它假设您只需要第一个循环:

$xml->templategroup->template
同:

$xml->templategroup[0]->template
您希望从外部循环遍历当前
templategroup
中的所有
template
元素,您称之为
$dir
,因此您实际需要的应该是:

foreach($dir->template as $tpl){
但是,您将用下面的行覆盖变量
$dir

$dir = str_replace(' ','_',$dir['name']);
通常情况下,将同一变量用于两个不同的目的是不好的,在这种情况下,
$dir
对于第一个目的来说并不是一个好名字,因此我建议您将其更改为:

foreach($xml->templategroup as $templategroup){
以及:

为您提供以下内容的内部循环:

foreach($templategroup->template as $tpl){

现在我;注意:试图在第66行的C:\xampp\htdocs\chris\admin.php中获取非对象的属性警告:为第66行的C:\xampp\htdocs\chris\admin.php中的foreach()提供的参数无效66@ChrisJamesReynolds啊,您正在覆盖变量
$dir
;见编辑后的答案。你应该能够从那里找到答案-调试的关键是理解逻辑。@MMSoP,u是一个和蔼可亲的thanx,它可以工作。但它仍然欺骗了公众,为什么?
foreach($templategroup->template as $tpl){