[英]Extract data from Database and display in Modal
I have a small family tree website that I am creating. 我有一个正在创建的小型家谱网站。 I have each persons information in a mysql database which I want to display inside a modal that pops up when a person clicks on any person.
我在mysql数据库中拥有每个人的信息,我想在一个人单击任何人时弹出的模态中显示该信息。
No I have all the HTML and CSS up and running for the modal part. 不,我已为模式部分启动并运行了所有HTML和CSS。 I also know some basic php to write a fairly easy script to extract information from the database which I did (pasted below).
我也知道一些基本的PHP可以编写一个相当简单的脚本来从我所做的数据库中提取信息(粘贴在下面)。 However im having 2 problems right now:
但是我现在有两个问题:
Every person on the tree is written as : 树上的每个人都写成:
<a href="#" class="md-trigger" data-modal="modal-3">Tywin Lannister</a>
and my sql statement looks like: 和我的SQL语句看起来像:
select * from family_table where name = Tywin Lannister;
I would like to know how would I substitute hard coding the name to just make 1 php script work for all names which would display only their information. 我想知道如何将硬编码替换为仅使1个php脚本适用于仅显示其信息的所有名称。
My html for the names is already given above. 我的名字的html已经在上面给出了。 My modal HTML is below:
我的模式HTML如下:
<div class="md-modal md-effect-1" id="modal-3">
<div class="md-content">
<h3>Person Information</h3>
<div>
<ul>
<li><strong>Name:</strong>Tywin Lannister.</li>
<li><strong>DOB:</strong> 28th July 1994.</li>
<li><strong>BirthPlace:</strong> Chicago.</li>
<li><strong>Occupation:</strong> Student.</li>
<li><strong>About:</strong> The Persons information will go here. Probably dynamic and deruved from a database.</li>
<li><strong>Contact:</strong> Contact information with FB, twitter and email address.</li>
</ul>
<button class="md-close">Close me!</button>
</div>
</div>
</div>
My php script is as follows: 我的PHP脚本如下:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * from family_table';
mysql_select_db('family');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Name :{$row['Name']} <br> ".
"Nickname : {$row['Nickname']} <br> ".
"Age : {$row['Age']} <br> ".
"DOB : {$row['DOB']} <br> ".
"About : {$row['About']} <br> ".
"Contact : {$row['Contact Information']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
There are two options for you: 1. When loading the page you need to load all of the modal windows of the people and just give them an I'd by what you decide. 有两个选项供您选择:1.加载页面时,您需要加载人员的所有模式窗口,并根据您的决定给他们一个“我”。 That was the easy method.
那是简单的方法。 2. That is the harder but better method, You can use ajax to load the person info.
2.这是更困难但更好的方法,您可以使用ajax加载人信息。 You do that by creating a php file that receives a person id or something and than echo its information.
您可以通过创建一个php文件来实现这一点,该文件接收一个人的id或某物,然后回显其信息。 Here is an example:
这是一个例子:
.ajax({
url : "URL to post the data to",
type: "POST",
data : person_id,
success: function(data)
{
//$('#id of a div inside the modal').html(data);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.