2013-07-25 75 views
0

我曾經工作過,並在某個時間點昨天打破了它,無法弄清楚。直到第二次提交的PHP代碼才運行

我正在從表單輸入生成一個html文件。

我調用一個函數,如果數據被髮布到網頁,但「的foreach」循環不運行第一次提交表單時......,它是1個資產羞澀之後,如果你上傳的3張圖片,它「會顯示2生成的網頁...

function AddClientDB($client, $pth, $project){ 
mysql_connect('localhost', 'dbname', 'pw'); //connect to db 
mysql_select_db('tablename'); //select file 
$indx = $project.".html"; 
$sql="INSERT INTO Clients VALUES (NULL,'$client', '$project', 'http://webpage.net/','$pth','$indx')"; 
mysql_query($sql) or DIE("Problems with the query:<pre>$sql</pre>" . mysql_error()); 
//Create client folder 
    if (!file_exists('uploads/'.$client)) { 
     mkdir('uploads/'.$client, 0777, true); 
     echo "Created Folder for Client: ". $_GET['client']. "<br />"; 
     } 
//Make project folder under client 
    if (!file_exists('uploads/'.$client. '/'. $project)) { 
     mkdir('uploads/'.$client.'/'.$project, 0777, true); 
     } 
$sql="INSERT INTO Projects VALUES (NULL,'$project', '$client',0,0,'$pth')"; 
mysql_query($sql) or DIE("Problems with the query:<pre>$sql</pre>" . mysql_error()); 

$myFile = 'uploads/'.$client.'/'.$project . '/' . $project.".html"; 
$fh = fopen($myFile, 'w') or die("can't open file"); 

//Top part of html page to make 
$stringDataA = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Title</title> 
<link rel="stylesheet" type="text/css" href="http://pixelfirereview.net/styles.css"> 
    <script type="text/javascript">  
    function popDate(){ 
     var dt=new Date(); 
     document.getElementById("dat").innerHTML=dt; 
     } 
     </script> 
</head> 
<body onload="popDate();"> 
<div align="center"> 
    <table width="960" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td><table width="960" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
     <td width="480"></td> 
     <td width="480"><a href="dbview.php" target="_blank"><img src="http://www.pixelfireinc.com/pfp2011/wp-content/uploads/2011/04/PixelfireLogoTopSM1.png" width="340" height="72" border="0" align="right" /></a></td> 
    </tr></table></td></tr><tr><td>&nbsp;</td></tr><tr><td> 
    <table width="960" border="0" cellspacing="0" cellpadding="0"><tr> 
     <td><div align="center"</div></td></tr><tr> 
     <td><div align="center">'; 


//Foreach valid file in the project folder, add it to our $content variable as a string. 
//For some reason, the foreach loops only run the second time the form is submitted...if you hit f5 and continue through the warning about resubmitting data, it then fires the code in the foreach loops.... 


foreach (glob($pth. '*.jpg') as $filename2) { 
      echo "<br />filename2: ". $filename2. "<br />"; 
      // echo "<br />$filename size " . filesize($filename2) . "<br />"; 
      $content = $content . '<span class="m_title">'.$filename2.'</span><br /><p><img src="http://pixelfirereview.net/'.$filename2.'" /><br /><br /><a href="http://pixelfirereview.net/'.$filename2.'"><img src="http://pixelfire.net/clients/images/btn_download.png" width="235" height="50" border="0" align="right" /></a></p><br />'; 
     } 
     foreach (glob($pth. '*.png') as $filename3) { 
     echo "<br />filename3: ". $filename3."<br />"; 
      echo "<br />\n$filename size " . filesize($filename3) . "<br />"; 
      $content = $content . '<br /><span class="m_title"><!-- InstanceBeginEditable name="Project Title" -->'.$filename3.'</span><img src="http://pixelfirereview.net/'.$filename3.'" /> 
      <br /><br /><p><a href="http://pixelfirereview.net/'.$filename3.'"><img src="http://pixelfire.net/clients/images/btn_download.png" width="235" height="50" border="0" align="right" /></a></p><br />'; 
     } 
     foreach (glob($pth. '*.mp4') as $filename4) { 
     echo "<br />filename4: ". $filename4."<br />"; 
      // echo "<br />\n$filename size " . filesize($filename4) . "<br />"; 
      $content= $content. '<br /><span class="m_title">'.$filename4.'</span><div id="mediaplayer'.$filename4.'"></div><script type="text/javascript" src="http://www.pixelfire.net/clients/jwplayer.js"></script><script type="text/javascript">  
       jwplayer("mediaplayer'.$filename4.'").setup({ 
        flashplayer: "http://www.pixelfire.net/clients/player.swf", 
        file: "http://pixelfirereview.net/'.$filename4.'", 
        width: "960", 
        height: "565", 
        autoplay: "false", 
        image: "http://www.pixelfire.net/clients/images/VideoPreview.jpg", 
        repeat: "always", 
        controlbar: "bottom",   
       });  
        </script><br /> 
        <p><a href="http://pixelfirereview.net/'.$filename4.'"><img src="http://pixelfire.net/clients/images/btn_download.png" width="235" height="50" border="0" align="right" /></a></p><br />'; 
     } 
     foreach (glob($pth. '*.wav') as $filename5) {     
      $content = $content . '<span class="m_title">'.$filename5.'</span><div id="mediaplayer'.$filename5.'"></div> <script type="text/javascript" src="http://www.pixelfire.net/clients/jwplayer.js"></script> <br /><script type="text/javascript">   
       jwplayer("mediaplayer'.$filename5.'").setup({ 
        flashplayer: "http://www.pixelfire.net/clients/player.swf", 
        file: "http://pixelfirereview.net/'.$filename5.'", 
        width: "960", 
        height: "565", 
        autoplay: "false", 
        image: "http://www.pixelfire.net/clients/images/VideoPreview.jpg", 
        repeat: "always", 
        controlbar: "bottom",   
       });  
        </script><br /> 
        <p><a href="http://pixelfirereview.net/'.$filename5.'"><img src="http://pixelfire.net/clients/images/btn_download.png" width="235" height="50" border="0" align="right" /></a></p>'; 
     } 


//Create the lower half of the html page 

$stringDataB= '</div></td></tr><tr><td height="20">&nbsp;</td></tr><tr><td><table width="960" border="0" cellspacing="0" cellpadding="0"><tr>    
      <td width="620" valign="top"><table width="550" border="0" cellspacing="0" cellpadding="1"><tr> 
        <td width="125" class="m_main"><div align="right">Last Modified:</div></td> 
       <td width="400"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>       
         <div id="dat" style="margin-top:80px;background:#333333;padding:.5em;" align="left" class="m_main_alt"></div></div></td></tr></table></td></tr></table></td><td width="350" valign="top"> 
       <table width="350" border="0" cellspacing="1" cellpadding="0"><tr> 
        <td></td> 
       </tr></table></td></tr></table></td></tr><tr><td height="100"></td></tr></table></td></tr><tr><td></td></tr><tr><td> 
       <div align="center" class="footer">&copy; 2013 PixelFire Productions<br />  (425) 917-1400  </div></td> </tr> </table></div></body></html>'; 


//Put the pieces together, top html, content, bottom html. 
    $stringData = ''.$stringDataA . $content . $stringDataB.''; 
    fwrite($fh, $stringData); 
} 

它創建HTML頁面和所有,但它第一次運行$內容是空的....如果你遞交表格,按後按F5繼續以瞭解如何重新提交數據的消息,然後其運行在foreach循環和$內容將包含我們把在頂部和HTML的下半部之間的串碼.......

任何想法,爲什麼這可能是?

+8

嗯看到代碼後,我頭痛...... – Alvaro

+0

你有代碼:'mysql_connect('localhost','dbname','pw'); mysql_select_db( '表名'); //選擇文件''mysql_connect()'的第二個參數應該是用戶名,'mysql_select_db()'應該是數據庫的名稱。 –

+0

^我的錯誤,當我拿出我的真實密碼/用戶名錶的名稱,它的代碼是正確的...似乎$內容變量只是在第二次運行時填充......無法弄清楚...... – user1108224

回答

0

通過將它分解爲幾個函數並強制執行順序來實現它。

相關問題