Javascript 在模板页面中加载数据
我正在创建一个网站,我需要在其中销售产品。我被网站的产品页面卡住了 像amazon.com这样的大型电子商务网站必须有一个产品页面的模板,它们动态加载数据库中的所有数据,包括产品图像、价格、评论。我还想创建一个类似的产品页面,点击产品链接,它会自动加载数据库中存储的所有图像、价格和信息,并显示出来 如果单击另一个产品,所有内容都会加载到相同的Javascript 在模板页面中加载数据,javascript,php,html,css,web,jquery,ajax,Javascript,Php,Html,Css,Web,Jquery,Ajax,我正在创建一个网站,我需要在其中销售产品。我被网站的产品页面卡住了 像amazon.com这样的大型电子商务网站必须有一个产品页面的模板,它们动态加载数据库中的所有数据,包括产品图像、价格、评论。我还想创建一个类似的产品页面,点击产品链接,它会自动加载数据库中存储的所有图像、价格和信息,并显示出来 如果单击另一个产品,所有内容都会加载到相同的html/php模板页面上,我不必为不同的产品创建不同的页面。我该怎么做 我已经寻找解决这个问题的方法很多天了,但我不知道到底要寻找什么才能得到答案 创建一
html/php
模板页面上,我不必为不同的产品创建不同的页面。我该怎么做
我已经寻找解决这个问题的方法很多天了,但我不知道到底要寻找什么才能得到答案 创建一个php页面,比如,
load\u products.php
并传递get参数,如load\u products.php?product\u id=1
。
更改id(根据数据库设置)并从数据库中获取相应的产品详细信息
编辑:详细答案 数据库 表:
表_产品
+--------------------------+---
| id | name | details | ...
+--------------------------+---
| 1 | Product1 | Details1 | ...
| 2 | Product2 | Details2 | ...
...
+--------------------------+---
我将向您展示如何使用MySQL数据库
PHP代码load_products.php
//建立到数据库的连接
$conn=mysql_connect('localhost','username','password');
mysql_select_db('products_database_name');
//展示所有产品
$res=mysql_查询(“从表_产品中选择*);
回声“;
while($row=mysql\u fetch\u数组($res))
{
$id=$row[“id”];
$name=$row[“name”];
回声“;
}
//根据用户的输入单击显示特定产品的详细信息
如果(isset($\u GET[“id”]))
{
$id=$_GET[“id”];
$res=mysql\u查询(“从表中选择*,其中id=$id”);
回声“;
while($row=mysql\u fetch\u数组($res))
{
$details=$row[“details”];
$name=$row[“name”];
回显“$name$details”;
}
回声“;
}
这是一个非常简单的实现,但完全可以工作,您可以在15分钟内在本地主机上看到输出。您可以在此基础上进行构建
您可以搜索:从数据库加载图像、增强网站界面、在此基础上使用等等。请查找改进版 products.php页面
// Establish connection to database
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('products_database_name');
// Display All the Products
$res=mysql_query("SELECT * FROM table_products");
$productsPerRow = 4;
$columnWidth = (int)(100 / $productsPerRow);
$count = 0; //count product displayed
echo "<table>
<tr>";
while($row=mysql_fetch_array($res))
{
$count++;
$id = $row["id"];
$name = $row["name"];
$image = $row["image"]; //image name with extension, upload it in the images folder
echo "<td width='" . $columnWidth ."' >
<img src='images/" . $image ."' border='0'/><br />
<a href='load_product.php?id=$id'>$name</a>
</td>";
if($count%$productsPerRow==0)
echo"</tr><tr>";
}
//close empty cells
if ($count % $productsPerRow > 0) {
echo "<td colspan='" . ($productsPerRow - ($count % $productsPerRow)) .
"'> </td></tr>";
}
//close table
echo "</table>";
//建立到数据库的连接
$conn=mysql_connect('localhost','username','password');
mysql_select_db('products_database_name');
//展示所有产品
$res=mysql_查询(“从表_产品中选择*);
$productsPerRow=4;
$columnWidth=(int)(100/$productsPerRow);
$count=0//计数显示的产品
回声“
";
while($row=mysql\u fetch\u数组($res))
{
$count++;
$id=$row[“id”];
$name=$row[“name”];
$image=$row[“image”];//带扩展名的图像名称,将其上载到图像文件夹中
回声“
";
如果($count%$productsPerRow==0)
回声“;
}
//关闭空单元格
如果($count%$productsError>0){
回声“;
}
//闭桌
回声“;
加载_product.php页面
// Establish connection to database
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('products_database_name');
// Display the details of a particular product based on the input click from user
if(isset($_GET["id"]))
{
$id=$_GET["id"];
$res=mysql_query("SELECT * FROM table_products WHERE id=$id");
echo "<table>";
while($row=mysql_fetch_array($res))
{
$details=$row["details"];
$name=$row["name"];
echo "<tr><td>$name</td><td>$details</td></tr>";
}
echo "</table>";
}
//建立到数据库的连接
$conn=mysql_connect('localhost','username','password');
mysql_select_db('products_database_name');
//根据用户的输入单击显示特定产品的详细信息
如果(isset($\u GET[“id”]))
{
$id=$_GET[“id”];
$res=mysql\u查询(“从表中选择*,其中id=$id”);
回声“;
while($row=mysql\u fetch\u数组($res))
{
$details=$row[“details”];
$name=$row[“name”];
回显“$name$details”;
}
回声“;
}
@Ayush这就是你要找的吗?如果是这样,我可以在一段时间后扩展答案!!这正是我想要的。如果你能把它扩展一下,我将不胜感激。。
// Establish connection to database
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('products_database_name');
// Display the details of a particular product based on the input click from user
if(isset($_GET["id"]))
{
$id=$_GET["id"];
$res=mysql_query("SELECT * FROM table_products WHERE id=$id");
echo "<table>";
while($row=mysql_fetch_array($res))
{
$details=$row["details"];
$name=$row["name"];
echo "<tr><td>$name</td><td>$details</td></tr>";
}
echo "</table>";
}