0

我想使用带有预定义 div、布局等的单个页面作为基础,这样当从其他地方单击产品时,它会将该产品信息加载到页面上?

他们这样做的方式就是我会坐在这里直到 2020 年左右仍然在页面上输入产品信息。

编辑*************

function product ()
{
    $get = mysql_query ("SELECT id, name, description, price, imgcover FROM products WHERE size ='11'");
    if (mysql_num_rows($get) == FALSE)
    {
        echo " There are no products to be displayed!";
    }
    else
    {
        while ($get_row = mysql_fetch_assoc($get))
        {
            echo "<div id='productbox'><a href=product1.php>".$get_row['name'].'<br />
            '.$get_row['price']. '<br />
            ' .$get_row['description']. '<br />
            ' .$get_row['imgcover']. "</a></div>" ;
        } 
    }
}

此外,我对该代码的一个问题是该<a href>标签仅转到 product1.php。任何想法如何使该链接指向空白产品布局页面,该页面将填充用户刚刚单击的产品信息,基本上链接到空白布局页面上的自身。

谢谢任何帮助都会很棒!谢谢马克西

4

3 回答 3

0

由于您没有代码,因此这是执行此操作的一般方法。您想要的是产品页面的模板

  1. 查询数据库
  2. 将数据加载到变量中
  3. 制作一个脚本,将变量中的数据打印到产品页面中

一些脚本.php

<?php
$productid = $_REQUEST['productid']; //Of course do sanitation 
                                     //before using get,post variables

//though you should be using mysqli_* functions as mysql_* are depreciated
$result = mysql_query("select * from sometable where id='{$productid}"); 

$product = mysql_fetch_object($result);

include("productpage.php");

产品页面.php

<div class="Product">
  <div class="picture"><img src="<?php echo $product->imghref;?>" /></div>
  <div class="price"><?php echo $product->price;?></div>
</div>

等等等等第四个。包含的脚本使用当前在调用函数范围内的任何变量

如果您打算将产品加载到同一页面而不进行其他页面加载,则需要使用 ajax。这是使用 XHR 请求从服务器返回数据的 javascript 代码。您可以使用纯 javascript 或 jQuery 之类的库来通过使用 $.ajax 调用来简化执行 xhr 请求的过程。

于 2013-07-22T16:35:46.823 回答
0

我知道这个问题在 4 年前就被问过了,但由于没有答案被标记为正确,我想我可能会加入。

首先,让我们从升级mysql和使用mysqli——我个人最喜欢的,你也可以使用PDO。您是否尝试过使用$_GET拉取您想查看的任何产品的 id,然后将它们全部一起显示或一次显示一个?

它可能看起来像这样:

<?php // start by creating $mysqli connection
$host = "localhost";
$user = "username";
$pass = "password";
$db_name = "database";

$mysqli = new mysqli($host, $user, $pass, $db_name);

if($mysqli->connect_error)
{
    die("Having some trouble pulling data");
    exit();
}

假设连接成功,我们继续检查是否设置了 ID。在这种情况下,我通过假定为id. 你可以让它更复杂,或者在这里采取不同的方法。

if(isset($_GET['id']))
{
    $id = htmlentities($_GET['id']);

    $query = "SELECT * FROM table WHERE id = " . $id;
}
else
{
    // if no ID is set, just bring all the results down
    // then you can modify how, and which table the results
    // are being used.
    $query = "SELECT * FROM table ORDER BY id"; // the query can be changed to whatever you would be prefer
}

一旦我们决定了一个查询,我们就会开始查询数据库以获取信息。我有三个步骤:

检查查询 > 检查表中的记录 > 遍历角色并为每个角色创建一个对象。

if($result = $mysqli->query($query))
{
    if($result->num_rows > 0)
    {
        while ($row = $result->fetch_object())
        {
            // you can set up your element here
            // you can set it up in whatever way you want
            // to see your product being displayed, by simply
            // using $row->column_name
            // each column becomes an object here. So your id
            // column would be pulled using $row->id
            echo "<h1>" . $row->name . "</h1>";
            echo "<p>" . $row->description . "</p>";
            echo "<img src=" . $row->image_path . ">";
            // etc ...
        }
    }
    else
    {
        // if no records match the selected ID
        echo "Nothing to see here...";
    }
}
else
{
    // if there's a problem with the query
    echo "A slight problem with your query.";
}
$mysqli->close(); // close connection for safety
?>

我希望这能回答你的问题,如果你仍然被这个问题困扰,可以帮助你。这是你可以用 MySQLi 和 PHP 做的事情的基本框架,你总是可以使用一些 Ajax 来使页面更具交互性和用户友好性。

于 2017-07-24T05:08:15.677 回答
0

需要在 Javascript 或 JQuery 中通过点击向页面添加内容。

  1. 您可以使用 ajax 调用从 php 页面检索所需的数据,语法在这里
  2. 或者您也可以在 JQuery 中使用 .load() 函数将 php 页面加载到 div 内容,语法在这里
于 2017-07-24T05:27:35.597 回答