2015-07-19 69 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