将提交的表单结果显示为URL链接列表

时间:2015-07-19 22:12:17

标签: php mysql forms

首先,我的PHP技能有点受限,因此我在这里向您提问。 我已经构建了一个相当复杂的表单,其中包含多个输入(文本框和下拉列表),这些输入存储在MYsql DB中。提交表单后,它将在新页面上显示为已完成的报表。这些完成的报告每次提交表单时都会显示一个。我的问题是,如何显示报告显示为单个报告的链接列表,而不是完整报告列表。 我希望我已经很好地解释了这个情况。

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>

2 个答案:

答案 0 :(得分:0)

通过查看您的代码,我可以说,这不是PHP的问题,它更像是您从数据库中获取后如何显示结果,

你正在运行循环,从这里开始

<?php}?>

并在此结束

{}

因此,循环内部的所有内容或这些括号viewpage.php内的简单单词会一次又一次地重复从数据库中获取新的结果行,如果您获取style css的查看源,您将看到{{1}文件<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

答案 1 :(得分: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”,您可以通过提供作业请求ID作为URL中的参数来加载给定作业请求的报告,如下所示:

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

现在,您可以通过查找完整的作业请求集并迭代它们来自动生成指向这些页面的链接列表,而不是输出完整报告,只需输出链接:

使用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,我只是提供了简单的方法来帮助您入门。