繁体   English   中英

使用功能在预定义的div中显示mysql查询的结果

[英]Display results of mysql query in a predefined div using a function

我对PHP相当陌生,正在练习一些东西。 我在mySQL中有一个表,我希望获取查询输出(将分为四列)并将其打印到我在网页中定义的div中。 我已经在网页中包含了findInstructor.php页面,这就是我要做的。 我知道这种方法,但是我无法对其进行编码。

<?php

if(isset($_POST['discipline']) && isset($_POST['location']) && isset($_POST['startDate']) 
&& isset($_POST['endDate']) && isset($_POST['level']) 
&& isset($_POST['name']) && isset($_POST['email']) && isset($_POST['age']) )
{
    $discipline = $_POST["discipline"];
    $location = $_POST["location"];
    $startDate = $_POST["startDate"];
    $endDate = $_POST["endDate"];
    $level= $_POST["level"];
    $name = $_POST["name"];
    $email = $_POST["email"];
    $age= $_POST["age"];
    $comments = $_POST["comments"];
    $comments = $_POST["details"];
    $specialty = $_POST["specialty"];
    $rating = $_POST["rating"];


}

if(!empty($discipline) && !empty($location) && !empty($startDate) && !empty($endDate)  
    && !empty($name)&& !empty($email) && !empty($age)){

    function getUserField($firstname, $rating, $location, $specialty){

    $query = "SELECT `instructor_first_name`,`instructor_rating`, `instructor_location`,`instructor_specialty`\n"
. "FROM `instructor` \n"
. "WHERE `instructor_location` LIKE \'$location\'\n"
. "AND `instructor_course_id` LIKE \'$discipline\'\n"
. "AND `instructor_specialty` LIKE \'$specialty\'\n"
. "AND `instructor_ability_level` LIKE \'$level\'\n"
. "AND `instructor_rating` >= $rating";

$result = mysql_query($query);


$num_rows = mysql_num_rows($result);

return mysql_result($result, 0,$firstname, $rating, $location, $specialty);


    }
}
?>

我想模糊地添加结果的div看起来像这样:

<fieldset style="padding:2.5em 6em;">
                <div class="col col-10">
                    <div class="instructor_box">
                        <div class="dp"><img src="../Pictures/1.jpg"/></div>
                        <div class="name">Instructor</div>
                        <div class="info">
                            <div class="info1">
                                Level : <br/> Discipline : <br/> Location : 
                            </div>

                            <div class="info3">
                                Skiing <br/> California, CA <br/> ABC Cerfitified
                            </div>
                            <div class="info4">
                                <button type="button" class="button" onclick="">Contact</button>
                            </div>
                            <div style="clear:both;"></div>
                        </div>


                     </div>       
                </div>
           </fieldset>

我知道这看起来非常糟糕。 但我真的希望有人能帮助我。 另外,我希望使用ajax在div中显示结果。 您能否花点时间告诉我我要去哪里错了?

谢谢你的时间。 :)

好的,一步一步来:
第一:不要再使用mysql-已过时,新的php版本在不久的将来将不支持它。 改用mysqli。 它几乎和mysql一样容易。 有关mysqli的所有信息,请参见php.net。

第二:如何在html页面上显示结果?
让我们从这里开始(经典的“非ajax”方式):

$num_rows = mysql_num_rows($result);  
//now you take out the rows:  
while($row = mysql_fetch_assoc($result) {  
  $instructor_first_name = $row['instructor_first_name'];  
  $instructor_rating = $row['instructor_rating'];  
  // ... and so on for each column of your dataset  
  // print html - I chose a simple example just to show the principle:  
  print '<p>'.htmlspecialchars($instructor_first_name).'</p>';  
  print '<p>'.htmlspecialchars($instructor_rating).'</p>';  
}     

下一个:ajax。
您确切知道ajax的工作原理吗? 是xmlhttprequest和json知名词吗? 如果不是,则必须学习以下内容:原理:通过让js向服务器“询问”一些数据来通过javascript“触发” http请求。 服务器发送数据,例如作为json编码的字符串。 您的客户接收到json,将其评估为eval,然后将“事物”(元素节点和文本节点)放入DOM中。 太短了,无法在一篇文章中详细解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM