javascript 如何使用 MySQL 数据库中的数据填充 HTML 列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7457917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fill HTML list using data from a MySQL database?
提问by oudouz
I have an html webpage, and it contains a unordered list .So i want to fill this list using items that are stored in a MySQL database, an example item is provided to be clear.
我有一个 html 网页,它包含一个无序列表。所以我想使用存储在 MySQL 数据库中的项目来填充这个列表,提供了一个示例项目来明确。
<ul class="content">
<li class="class1">
<p id="id1">paragraph1</p>
<div class="class1sub">
<p>paragraph1sub</p>
</div>
</li>
So i want to retrieve the whole list items from the database and display it on the webpage. The problem is that each list item is stored in the database besides its CSS classes and IDs to be styled. Here is an example of a couple of items:
所以我想从数据库中检索整个列表项并将其显示在网页上。问题在于,除了要设置样式的 CSS 类和 ID 之外,每个列表项都存储在数据库中。这是几个项目的示例:
liclass - id - paragraph1 - divclass - paragraph2
liclass - id - 第 1 段 - divclass - 第 2 段
So I'm thinking of maybe using JavaScript/PHP and MySQL ... but no starting code on my mind...any help ?
所以我在考虑使用 JavaScript/PHP 和 MySQL ......但我没有想到开始代码......有什么帮助吗?
回答by J-Bangin'
PHP is definitely the way to get this done. I've used the CodeIgniterphp framework in a lot of my projects. It's very easy to set up and use, but feel freed to shop around to find what's best for you. I'll try not to be too CodeIgniter specific here, but if you do go this route you can check out this video on basic database function with CodeIgniter.
PHP 绝对是完成这项工作的方式。我在很多项目中都使用了CodeIgniterphp 框架。它非常易于设置和使用,但您可以自由地货比三家,找到最适合您的产品。我会尽量不要在此处对 CodeIgniter 过于具体,但如果您确实走这条路,您可以查看有关使用 CodeIgniter 的基本数据库功能的视频。
Basically whatever you choose to do there are going to have few basic parts. Some config area where you type in you Database name, and username and password.
基本上无论你选择做什么,都会有几个基本部分。您在其中键入数据库名称、用户名和密码的一些配置区域。
Example Database Config file:
示例数据库配置文件:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "myusername";
$db['default']['password'] = "mypassword";
$db['default']['database'] = "mydatabse";
$db['default']['dbdriver'] = "mysql";
Now that the php knows how to talk to your database it's time to get the data and send it to be displayed.
既然 php 知道如何与您的数据库对话,就该获取数据并将其发送以供显示了。
This next part is more specific to CodeIgniter, mostly because that's what I have the most experience with, but many of the other frameworks will be something similar. The next three code snips will be represent three different files. The idea is to abstract/remove the backend database stuff from the front end content. This is referred to as Model-View-Controller(MVC)
下一部分更具体地针对 CodeIgniter,主要是因为这是我最有经验的,但许多其他框架将是类似的。接下来的三个代码片段将代表三个不同的文件。这个想法是从前端内容中抽象/删除后端数据库内容。这被称为模型-视图-控制器(MVC)
Run your query (Model):
运行您的查询(模型):
function getData()
{
$query = $this->db->get('myTable');
return $query->result();
}
Set that data and send it to your view(Contorller):
设置该数据并将其发送到您的视图(控制器):
function addReceipt()
{
$data = array();
if($query = $this->my_model->getData())
{
$data['content'] = $query;
}
$this->load->view('my_view', $data);
}
Lastly display your data by looping through records and using php echo(View):
最后通过循环记录并使用 php echo(View) 来显示您的数据:
<ul class="content">
<?php if(isset($content)) : foreach($content as $row) : ?>
<li class="<?php echo $row->liclass; ?>">
<p id="<?php echo $row->ID; ?>"><?php echo $row->paragraph1; ?></p>
<div class="<?php echo $row->divclass; ?>">
<p><?php echo $row->paragraph2; ?></p>
</div>
</li>
<?php endforeach; ?>
</ul>
回答by Tebello Makara
This would be interesting if you showed your php code here: but this is what you should do
如果你在这里展示你的 php 代码,这会很有趣:但这是你应该做的
<?php
mysql_connect('hostname','username','password');
mysql_select_db('dbname');
$sql = "select *from tablename";
$run = mysql_query($sql);
echo "<table>";
echo "<tr><th>NAME</th><th>SURNAME</th><th>GENDER</th><th>BIRTHDAY</th></tr>";
$i = 1;
while($row = mysql_fetch_array($run)){
if($i%2 == 0) $bgcolor = 'lightblue';else $bgcolor = 'white';
$i++;
echo "<tr style='backgroundcolor".$bgcolor."'><td>NAME</td><td>".$row['SURNAME']."</td><td>".$row['GENDER']."</td><td>".$row['BIRTHDAY']."</td></tr>";
}
echo "</table>";
?>