2015-07-19 67 views
0

首先,我的PHP技能有點兒有限,因此我的問題在這裏。 我已經構建了一個存儲在MYsql數據庫中的具有多個輸入(文本框和下拉列表)的相當複雜的表單。表單提交後,它會在新頁面上顯示爲已完成的報告。每次提交表格時,這些完成的報告將在下一個下一個顯示。我的問題是,如何獲取顯示的報告,以顯示單個報告的鏈接列表,而不是完整報告的列表。 我希望我已經足夠好地解釋了這種情況。將提交的表單結果顯示爲URL鏈接列表

Code snippit from viewpage.php 


<html> 
<head> 

<body> 

<?php 
       mysql_connect("localhost","user","passwrd"); 
       mysql_select_db("dtbase"); 
       $order = "SELECT * FROM jobrequest" ; 
       $result = mysql_query($order); 
       while ($row=mysql_fetch_array($result)){ 


?>   
<link rel="stylesheet" href="css/style.css" type="text/css" /> 
</head> 
<body> 
<div style="padding:15px 0px 0px 100px;"> 
<table cellpadding="0" cellspacing="0" border="0" style="vertical-align:middle;width: 1139px; background-color:#213568; height:36px;"> 
    <tr> 
     <td class="topbar">Client Request Form</td> 
     <td style="width:900px;"></td> 
     <td class="topbar"><a style="color:#ffffff;" href="logout.php?logout">Logout</a></td> 
    </tr> 
</table> 
<div class="main-wrap"> 
<div class="content"> 
<table cellpadding="0" cellspacing="0" border="0" style="width: 1137px; background-color:#ffffff; height:5px;"> 
    <tr> 
     <td></td> 
    </tr> 
</table> 
<table cellpadding="0" cellspacing="0" border="0" > 
    <tr> 
           <td style="vertical-align:top; width:5px;"></td> 
     <td style="vertical-align:top;"><?php include("includes/clientchoices.php"); ?></td> 
     <td style="vertical-align:top; padding:0px 5px 15px 5px;"> 
       <table border="0" cellpadding="0" cellspacing="0"> 
       <tr> 
           <td style="vertical-align:top; width:1002px;"> <h1> Dashboard</h1></td> 
       </tr> 
       <tr> 
           <td style="vertical-align:top; background-color:#f5f5f5;"><h2>Job Request Form</h2></td> 
       </tr> 
        <tr> 
           <td style="vertical-align:top; background-color:#ffffff; height:5px;"> </td> 
       </tr> 
       <tr> 
           <td> 
        <div class="form"> 
        <table cellspacing="0" cellpadding="0" border="0" style="width:998px"> 
         <tr> 
          <td style="width:1002px; border:solid 1px #000000; padding:10px 0px 10px 0px;"><center><img src="../../images/spectra_logotop.jpg" alt="Spectra" title="Spectra" width="735" height="120" style="padding:5px;"></center> 
          </td> 
         </tr> 
         <tr> 
          <td> 
           <div style="padding:10px 0px 10px 0px;"> 
            <table cellpadding="0" cellspacing="0"> 
             <tr> 
              <td class="headingsa">Project Leader:</td><td class="answersa"><div class= "typesectiona"><?php echo ($row['project_leader']); ?></div></td> 
              <td class="headingsb">Contact Number:</td><td class="answersb"><div class= "typesectionb"><?php echo ($row['contact_number']); ?></div></td> 
              <td class="headingsc">Company Details:</td><td class="answersc"><div class= "typesectionc"><?php echo ($row['company_details']); ?></div></td> 
             </tr> 
            </table> 
           </div> 
          </td> 
         </tr> 
         <tr> 
          <td> 
           <table cellpadding="0" cellspacing="0"> 
            <tr> 
             <td class="headings5">Contact Person On Site:</td><td class="answers5"><div class= "typesection5"><?php echo ($row['contactperson_onsite']); ?></div></td> 
             <td class="headings6">Contact Details:</td><td class="answers6"><div class= "typesection6"><?php echo ($row['contact_no']); ?></div></td> 
             <td class="headings7">Date:</td><td class="answers7"><div class= "typesection7"><?php echo ($row['date']); ?></div></td> 
            </tr> 
           </table> 
          </td> 
         </tr> 
         <tr> 
          <td> 
           <table cellpadding="0" cellspacing="0"> 
            <tr> 
             <td class="headings1">Job/Order Number:</td><td class="answers1"><div class= "typesection1"><?php echo ($row['job_order_number']); ?></div></td> 
             <td class="headings2">Document Number:</td><td class="answers2"><div class= "typesection2"><?php echo ($row['doument_number']); ?></div></td> 
             <td class="headings3">QCP:</td><td class="answers3"><div class= "typesection3"><?php echo ($row['qcp']); ?></div></td> 
             <td class="headings4">Page No:</td><td class="answers4"><div class= "typesection4"><?php echo ($row['pageno']); ?></div></td> 
            </tr> 
           </table> 
          </td> 
         </tr> 

        <table cellspacing="0" cellpadding="0" border="0"> 
         <tr> 

          <td width="15px"></td> 
          <td><a class="othersubmitsLink" href="actionpdf.php">Email to Spectra</a></td> 
         </tr>           
        </table> 
        </div> 
        </td> 
       </tr> 
      </table> 
     </td>   
    </tr> 
</table> 
</div> 
</div> 
</div> 
<?php 
} 
?> 
</body> 
</html> 
+0

到目前爲止你做了什麼努力,任何代碼片段? – Shehary

+0

感謝您的建議。我通常使用mysqli而不是mysql。但是在這種情況下,我正在使用現有的mysql數據庫,並且不想混淆現有的代碼。但是我確實找到了解決我的問題的方法。謝謝 –

回答

0

您將需要一個單獨的PHP腳本,用於顯示根據提供的ID的報告。這個單獨的腳本會是這個樣子:

使用的mysqli

<?php 
    $conn = new mysqli("localhost", "user", "passwrd", "dtbase"); 
    $jrQry = $conn->prepare("SELECT * FROM jobrequest WHERE jobrequest_id = ?"); 
    $jrQry->bind_param('i', $_GET['jobrequest_id']); 
    $jrQry->execute(); 
    $jobrequestResult = $jrQry->get_result(); 
    $jobrequest = $jobrequestResult->fetch_assoc(); 

    // At this point, $jobrequest will contain the jobrequest record you want to display. 
?> 
<!-- HTML FOR REPORT GOES HERE, USING $jobrequest VARIABLE TO SHOW THE DATA --> 

注意,我在這個例子中使用mysqli的,如果這是不適合你可以使用舊式的mysql命令,但由於許多原因(其中安全主管),我強烈建議不要這樣做。

使用MySQL的

<?php 
    mysql_connect("localhost","user","passwrd"); 
    mysql_select_db("dtbase"); 
    $order = "SELECT * FROM jobrequest WHERE jobrequest_id = " . (int)$_GET['jobrequest_id']; 
    $result = mysql_query($order); 
    $jobrequest = mysql_fetch_array($result); 

    // At this point, $jobrequest will contain the jobrequest record you want to display. 
?> 
<!-- HTML FOR REPORT GOES HERE, USING $jobrequest VARIABLE TO SHOW THE DATA --> 

保存此頁爲「viewjobrequest。PHP的」,你就可以通過在URL作爲參數供應jobrequest ID來加載一個給定的作業請求的報告,像這樣:通過查找

http://address_of_site/viewjobrequest.php?jobrequest_id=X 

現在就可以自動生成的鏈接,這些頁面的列表你的全套jobrequests和遍歷它們,而是在輸出的完整報告,只是輸出的鏈接:

使用的mysqli

<?php 
    $conn = new mysqli("localhost", "user", "passwrd", "dtbase"); 
    $jrQry = $conn->prepare("SELECT * FROM jobrequest WHERE jobrequest_id = ?"); 
    $jrQry->bind_param('i', $_GET['jobrequest_id']); 
    $jrQry->execute(); 
    $jobrequestResult = $jrQry->get_result(); 
?> 
    <ul> 
<?php 
    while ($jobrequest = $jobrequestResult->fetch_assoc()) 
    { 
?>  
     <li> 
     <a href="viewjobrequest.php?jobrequest_id=<?php echo $jobrequest['jobrequest_id']; ?>"> 
      View job request #<?php echo $jobrequest['jobrequest_id']; ?> 
     </a> 
     </li> 
<?php 
    } 
?> 
    </ul> 

使用MySQL的

<?php 
    mysql_connect("localhost","user","passwrd"); 
    mysql_select_db("dtbase"); 
    $order = "SELECT * FROM jobrequest WHERE jobrequest_id = " . (int)$_GET['jobrequest_id']; 
    $result = mysql_query($order); 
?> 
    <ul> 
<?php 
    while ($jobrequest = mysql_fetch_assoc($result)) 
    { 
?>  
     <li> 
     <a href="viewjobrequest.php?jobrequest_id=<?php echo $jobrequest['jobrequest_id']; ?>"> 
      View job request #<?php echo $jobrequest['jobrequest_id']; ?> 
     </a> 
     </li> 
<?php 
    } 
?> 
    </ul> 

注:我刻意忽略你的HTML的大部分,您可以根據需要添加儘可能多的額外的HTML,我只是提供了基本框架,讓你開始。

0

通過看你的代碼,我可以說,這不是用PHP的問題,它更像你如何顯示從數據庫中獲取後的結果,

您運行的循環,從這裏

開始
<?php while ($row=mysql_fetch_array($result)){ ?> 

,並在這裏結束

<?php}?> 

所以內環路或簡單單詞我的一切這些括號{}重複一遍又一遍地從數據庫中提取新結果行,如果您查看viewpage.php的查看源,則會看到style css文件<link rel="stylesheet" href="css/style.css" type="text/css" />重複,試想一下,如果要獲取10行結果,則還要加載css文件10倍。

所以你的問題的答案(最可能的解決方案)是;

你的MySQL查詢

<?php 
      mysql_connect("localhost","user","passwrd"); 
      mysql_select_db("dtbase"); 
      $order = "SELECT * FROM jobrequest" ; 
      $result = mysql_query($order); 
      $totalrows = mysql_num_rows($result); //Check Total Number of Rows To Check if Data Exist or Not 
?> 

,然後在你的HTML,如果有任何行首先檢查匹配MySQL查詢您的WHERE條款,數據庫存在。

//Set an if else statement here so incase if no data exist. 
<?php if($totalrows > 0) { 
//If row(s) exist run your while loop here 
    <?php while ($row=mysql_fetch_array($result)){ ?> 
    //Show the result here from database 

    //Close your loop 
    <?php } ?> 
//Close your if condition 
<?php } else { ?> 
    //Display a message here if there is no data to show 

    //Close your else bracket 
<?php } ?> 

通過上面的解釋,我調整了你的HTML,以期望的方式顯示結果;

<table cellspacing="0" cellpadding="0" border="0" style="width:998px"> 
    <tr> 
     <td style="width:1002px; border:solid 1px #000000; padding:10px 0px 10px 0px;"><center><img src="../../images/spectra_logotop.jpg" alt="Spectra" title="Spectra" width="735" height="120" style="padding:5px;"></center></td> 
    </tr> 
    <tr> 
     <td> 
      <?php if($totalrows > 0) { 
      <div style="padding:10px 0px 10px 0px;"> 
       <table cellpadding="0" cellspacing="0"> 
        <tr> 
         <td class="headingsa">Link to Report</td> 
        </tr> 
        <?php while ($row=mysql_fetch_array($result)){ ?> 
        <tr> 
        //You have to replace `nameoffile.php` with file in which you want to display report and correct this (as i assumed it) if it's wrong `$row['id']` 
         <td><a href="nameoffile.php?id=<?php echo $row['id'];?>">Open Report</a></td> 
        </tr> 
        <?php } ?> 
       </table> 
      </div> 
      <?php } else { ?> 
      <div style="padding:10px 0px 10px 0px;"> 
       <table cellpadding="0" cellspacing="0"> 
        <tr> 
         <td>There is No Result To Show</td> 
        </tr> 
       </table> 
      </div> 
      <?php } ?> 
     </td> 
    </tr>           
</table> 

注: MySQL將很快被棄用,考慮開始使用的mysqli或PDO

+0

這個答案不會做OP所要求的,也就是顯示一個鏈接列表,每個鏈接鏈接到特定提交的填充報告副本。 –

+0

你是對的,我誤解了這個問題 – Shehary

+0

我糾正了答案,並希望我這次做得正確。謝謝指出 – Shehary