Show Additional Form Data when Result is Isolated

时间:2015-07-28 23:41:45

标签: php database pdo

I have a form that displays the results from a survey into a webpage. At the moment, when a user clicks on the 'View Here' it takes them to the individual entry for that ID. When it is clicked I would like it to also show additional results (also part of the results from the same database).

Any idea how I do this? Just to clarify, it should not show until the ID is clicked to single out that entry - otherwise it shows them all in full one after the other.

$.ajax({
        url: 'js/users.php',
        dataType: 'json',
        success: function(data){
            var id = data[0];              
            var fName = data[1];
            var lName = data[2];

            $('#output').html("<b>id: </b>"+id+"<b> first name: </b>"+fname+"<b> last name: </b>"+lname);
        },
        error: function(data){
            alert(JSON.stringify(data));
        }
    });

Additional code that I would like to show once 'View Here' is clicked (so when the individual entry is shown):

<?php

try {
    $handler = new PDO('mysql:host=localhost;dbname=***', '***', '***');
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo $e->getMessage();
    die();
  }
class guestquestionnaireEntry {
    public $id, $date_submitted, 
        $entry;

        public function __construct()
    {

$this->entry = "

        <table border='1' align='center'>

                 <tr class='tablelist' style='font-size: 8pt;' ><td width='5%' colspan='2'>{$this->ID}</td><td width='40%' colspan='2'><b>Date Received: </b>{$this->date_submitted}</td><td width='45%' colspan='2'>{$this->name}</td><td width='10%'><a href=\"?ID={$this->ID}\">View here</a> </td></tr>

        </table>

";

    }

}

        SELECT DATE_FORMAT(date_submitted, '%m/%d/%Y') AS date_submitted FROM guestquestionnaire


// Checks if the submitted is a number. If so, isolates the ID and adds "where" clause
$id      =   (!empty($_GET['ID']) && is_numeric($_GET['ID']))? " where ID = '".$_GET['ID']."'" : "";
// Add the $id to the end of the string
// A single call would be SELECT * FROM guestquestionnaire where ID = '1'
$query   =   $handler->query("SELECT * FROM guestquestionnaire{$id}");
$query->setFetchMode(PDO::FETCH_CLASS, 'guestquestionnaireEntry');

while($r = $query->fetch()) {
    echo $r->entry, '<br>';
}

?>

1 个答案:

答案 0 :(得分:0)

看看这是否有效。我没有测试过,所以请确保保存到目前为止所有内容的副本。它需要填写数据库凭据:

<强>类:

$('#item')

使用:

<?php
    interface   Database
        {   
            public  static  function SetConnection($host,$username,$password,$database);
        }

    interface   Questionnaire
        {
            public  function Display();
            public  function DisplaySingle();
        }

    class   MySQLConn implements Database
        {           
            protected   static  $host;
            protected   static  $username;
            protected   static  $password;
            protected   static  $database;

            public      static $connect;

            private function __construct()
                {
                }

            public  static  function SetConnection($host = "***",$username = "***",$password = "***",$database = "***")
                {
                    self::$host     =   $host;
                    self::$username =   $username;
                    self::$password =   $password;
                    self::$database =   $database;
                    self::$connect  =   (!isset(self::$connect))? self::Initialize() : self::$connect;
                }

            private static  function Initialize($use = 'PDO')
                {
                    if($use == 'PDO') {
                            try {
                                    $con    =   new PDO('mysql:host='.self::$host.';dbname='.self::$database, self::$username, self::$password);
                                    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                                    return $con;
                                }
                            catch (PDOException $e) {
                                    die($e->getMessage());
                                }
                        }
                    else
                        return new mysqli(self::$host, self::$username, self::$password, self::$database);
                }
        }

    class   guestquestionnaireEntry implements Questionnaire
        {
            public  $id,
                    $date_submitted,
                    $choice,
                    $expectations,
                    $res,
                    $res_information,
                    $res_staff,
                    $further_comments1,
                    $entry;

            public function __construct()
                {
                    if(empty($_GET['focus']))
                        $this->entry = $this->DisplaySingle();
                    else
                        $this->entry = $this->Display();
                }

            public  function DisplaySingle()
                {
                    ob_start(); ?>

                <table border='1' align='center'>
                    <tr class='tablelist' style='font-size: 8pt;' >
                        <td width='5%' colspan='2'><?php echo $this->ID; ?></td>
                        <td width='40%' colspan='2'><b>Date Received: </b><?php echo $this->date_submitted; ?></td>
                        <td width='45%' colspan='2'><?php echo $this->name; ?></td>
                        <td width='10%'><a href="?ID=<?php echo strip_tags($this->ID); ?>&focus=true">View here</a></td>
                    </tr>
                </table>
                <?php
                    $data   =   ob_get_contents();
                    ob_end_clean();

                    return $data;
                }

            public  function Display()
                {
                    ob_start(); ?>
                    <tr style='font-size: 8pt;'>
                        <td width='60%'><a href="?ID=<?php echo $this->ID; ?>">ID</a></td>
                        <td width='40%' colspan='2'><?php echo $this->date_submitted; ?></td>
                    </tr>
                    <tr style='text-align: left; font:arial;'>
                        <td><h3>Date Submitted: <?php echo $this->date_submitted; ?></h3></td>
                    </tr>
                    <table border='1' align='center'>
                        <tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'>
                            <td colspan='3'>Prior to Arrival</td>
                        </tr>
                        <tr style='font-size: 8pt;'>
                            <td width='60%'>What made you choose us for your recent trip? </td>
                            <td width='40%' colspan='2'><?php echo $this->choice; ?></td>
                        </tr>
                        <tr style='font-size: 8pt;'>
                            <td>Did we meet your expectations as advertised? If no, please state why: </td>
                            <td width='40%' colspan='2'><?php echo $this->expectations; ?></td>
                        </tr>
                        <tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'>
                            <td colspan='3'>Making your Reservation</td>
                        </tr>
                        <BR>
                        <tr style='font-size: 8pt;'>
                            <td>Ease of making your reservation: </td>
                            <td width='40%'>$img</td>
                            <td width='5%'><?php echo $this->res; ?></td>
                        </tr>
                        <BR>
                        <tr style='font-size: 8pt;'>
                            <td>Hotel information offered: </td>
                            <td width='40%'><?php echo $img2; ?></td>
                            <td width='5%'><?php echo $this->res_information; ?></td>
                        </tr>
                        <BR>
                        <tr style='font-size: 8pt;'>
                            <td>Warmth and friendliness of staff: </td>
                            <td width='40%'><?php echo $img3; ?></td>
                            <td width='5%'><?php echo $this->res_staff; ?></td>
                        </tr>
                        <BR>
                        <tr style='font-size: 8pt;'>
                            <td colspan='3'>Further Comments: </BR>
                                </BR>
                                <?php echo $this->further_comments1; ?></td>
                        </tr>
                        <BR>
                    </table>
                    <BR>
                    <p>Back to List</p>
                    <?php
                    $data   =   ob_get_contents();
                    ob_end_clean();

                    return $data;
                }
        }
?>