2011-09-23 68 views
0

我想讓我的XML文件的URL動態,所以多人可以使用我的網站和一次查詢數據。我命令這樣做,我在我的xmlfile前插入一個隨機數字php變量。出於某種原因,我在嘗試使用此變量時遇到了創建和寫入xml文件的問題。當我使用像'wine.xml'這樣的靜態URL時,它可以正常工作。插入變量作爲我的URL在PHP中的問題fopen()

  $fp = fopen($randnumb.'wine.xml', 'w'); 
      fwrite($fp, $string); 
      fclose($fp); 
+0

什麼是'$ randnumb'的價值?在fopen調用之前立即執行'var_dump($ randnumb)'來查看。 –

+0

Marc - $ randnumb是通過$ randnumb = rand()生成的隨機數; – user961946

+0

你得到什麼樣的錯誤?當你運行它時會發生什麼? – JohnD

回答

0

我可能是錯的(因此,任何一個讓我們指正),如果你有一個XML,並想讓很多人爲什麼你必須做出的多個副本讀它?

是不是服務器應該做這個工作服務文件到很多人?如果我錯了,還有其他的東西你嘗試,然後這個PHP只能正常工作。這樣你就不必在php中查找錯誤。

<?php 

$fileName = rand().'file.xml'; 
$fp = fopen($fileName, 'w'); 
fwrite($fp, 'Hello!'); 
fclose($fp); 
?> 

<?php 
$handle = fopen($fileName, "rb"); 
$contents = fread($handle, filesize($fileName)); 
print_r($contents); 
fclose($handle); 
?> 
var winexml=loadXMLDoc("<?=$randnumb?>wine.xml"); 

是否<?爲你工作?導致wamp請求<?PHP(必須是php.ini)

爲什麼你在loadxmldoc參數中有第二個參數?! 這項工作:

<?PHP 
$dbq="\""; 
echo 'var winexml=loadXMLDoc(',$dbq,$randnumb,'wine.xml',$dbq,');'; 
?> 
+0

1.我的XML文件是從SQL表動態創建的。因此,創建的每個xml文件都是針對每個用戶請求定製的。 – user961946

+0

我想通了。我沒有正確地把SESSION_START();功能在每個頁面的頂部。我添加了該功能,並且一切正常。 – user961946

0

好吧,我明白了。我不知道你最終的xml文件顯示的偏好是什麼,但是這個腳本有可能讓你完成你的工作的東西,只是根據你的需要進行調整。

index.html,然後getXml.php

<html> 
<head> 
<script type="text/javascript"> 

var request = false; 
try { 
    request = new XMLHttpRequest(); 
} catch (trymicrosoft) {       
    try { 
    request = new ActiveXObject("Msxml2.XMLHTTP"); 
    } catch (othermicrosoft) { 
    try { 
     request = new ActiveXObject("Microsoft.XMLHTTP"); 
    } catch (failed) {     
     request = false;  
    } 
    } 
} 

if (!request) 
    alert("Error initializing XMLHttpRequest!"); 
</script> 

<script type="text/javascript"> 

var fileOption; 
var fileName; 

    function runPhp(makeFile) 
    { 
     var url = "getXml.php"; 
     fileOption = makeFile; 
     var params = "makeFile=" +makeFile+""; 
     request.open("POST", url, true); 

     //Some http headers must be set along with any POST request. 
     request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); 
     request.setRequestHeader("Content-length", params.length); 
     request.setRequestHeader("Connection", "close"); 

     request.onreadystatechange = updatePage; 
     request.send(params); 

    }//////////////////// 

    function getXml() 
    { 
     if(fileName==null){alert('please click create file first');return;} 
     var url = fileName; 
     var params = null; 
     request.open("POST", url, true);  
     request.setRequestHeader("Connection", "close");  
     request.onreadystatechange = displayFile; 
     request.send(params); 
    }//////////////////// 

    //You're looking for a status code of 200 which simply means okay. 
    function updatePage() { 
    if (request.readyState == 4) { 
     if (request.status == 200) 
     { 
      if(fileOption==1) 
       {fileName=request.responseText; return;} 
      document.getElementById('divResults').innerHTML=request.responseText; 
      document.getElementById('textareaResults').innerHTML=request.responseText; 
     } 
     else{ 
     //alert("status is " + request.status); 
     } 
    } 
    } 

     function displayFile() { 
    if (request.readyState == 4) { 
     if (request.status == 200) 
     {  
      document.getElementById('textareaResults').innerHTML=request.responseText; 
      document.getElementById('divResults').innerHTML='File loaded in text area above.'; 
     } 
     else{ 
     //alert("status is " + request.status); 
     } 
    } 
    } 

</script> 
</head> 
<body > 


<span style="background-color:blue;color:yellow;" 
onClick="runPhp(0)"/> 
Click for Xml Results.<br> 
(<font color=pink>I prefer this one!!!</font>) 
</span><br><br> 

<span style="background-color:blue;color:yellow;" 
onClick="runPhp(1)"/> 
Click to create an xml file.<br> 
</span> 

<span style="background-color:blue;color:yellow;" 
onClick="getXml(1)"/> 
Click to read the xml file.<br> 
</span> 

<textarea rows="10" cols="88" id="textareaResults"> 
</textarea> 
<br><br> 
<pre><div id="divResults"></div></pre> 
<br><br> 

</body> 
</html> 


<?PHP 
mysql_connect('localhost', 'root',''); 
mysql_select_db("mysql"); 
$query="select * from help_category;"; 

$resultID = mysql_query($query) or die("Data not found."); 

    $xml_output = "<?xml version=\"1.0\"?>\n"; 
$xml_output .= "<records>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 
    $row = mysql_fetch_assoc($resultID); 
    $xml_output .= "\t<record>\n"; 
    $xml_output .= "\t\t<help_category_id>" . $row['help_category_id'] . "</help_category_id>\n"; 
    $xml_output .= "\t\t<name>" . $row['name'] . "</name>\n"; 
    $xml_output .= "\t\t<parent_category_id>" . $row['parent_category_id'] . "</parent_category_id>\n"; 
    $xml_output .= "\t</record>\n"; 
} 

$xml_output .= "</records>"; 

if($_POST['makeFile']==0) 
    echo $xml_output; 
else 
    { 
$fileName = rand().'file.xml'; 
$fp = fopen($fileName, 'w'); 
fwrite($fp, $xml_output); 
fclose($fp); 
$dbq="\""; 
echo $fileName; 
} 

?>