在模板页面加载数据

Load data in template page

本文关键字:加载 数据      更新时间:2023-09-26

我正在创建一个网站,我需要在其中销售产品。我被网站的产品页面卡住了。

像亚马逊这样的大型电子商务网站必须有一个产品页面的模板,他们动态加载所有的数据,包括产品的图像,价格,从数据库的评论。我还想创建一个类似的产品页面,在点击产品的链接,它会自动加载存储在数据库中的所有图像,价格和信息,并显示它。

如果点击另一个产品,所有内容都加载在相同的html/php模板页面上,我不必为不同的产品制作不同的页面。我该怎么做呢?

我一直在寻找这个问题的解决方案很多天,但我不知道到底要寻找什么才能得到答案。

创建一个php页面,例如load_products.php,并传递load_products.php?product_id=1等get参数。更改id(根据数据库设置)并从数据库获取相应的产品详细信息。


编辑:详细答案

表:table_products

+--------------------------+---
| id |   name   | details  | ...
+--------------------------+---
| 1  | Product1 | Details1 | ...
| 2  | Product2 | Details2 | ...
            ...
+--------------------------+---

我将向您展示如何使用MySQL数据库。

PHP代码


load_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");
echo "<table>";
while($row=mysql_fetch_array($res))
{
    $id=$row["id"];
    $name=$row["name"];
    echo "<a href='load_products?id=$id'>$name</a>";
}
// 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>";
}

一个非常简单的实现,但完全可以工作,并且您可以在15分钟内在本地主机上看到输出。你可以在此基础上构建。

你可以搜索:从数据库加载图像,javascript, jquery增强网站界面,使用ajax在此之上等等。

请查看改进版本

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)) .
    "'>&nbsp;</td></tr>";
}
//close table
echo "</table>";

load_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>";
}