首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >mysql数据库PHP表

mysql数据库PHP表
EN

Stack Overflow用户
提问于 2014-07-04 22:40:34
回答 1查看 63关注 0票数 0

我目前在mysql上有一个数据库,里面有一些产品,我正在将它们打印到一个表中,如您所见:

好吧,现在没问题,但是..。我想为产品名称设置特定的图像。例如,我会在名称"Jeans Fit“(在css中悬停)的顶部,它将显示产品图像。如何将某些图片设置为产品名称?不是随机的,我想选择每一个,如果你能在这一点上指导我的话…

下面是我打印表的代码:

代码语言:javascript
复制
<?php

// to prevent undefined index notice
$action = isset($_GET['action']) ? $_GET['action'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";

if($action=='add'){
    echo "<div>" . $name . " foi adicionado ao carrinho</div>";
}

if($action=='exists'){
    echo "<div>" . $name . " já existe no carrinho</div>";
}

require "libs/DbConnect.php";


// page is the current page, if there's nothing set, default is page 1
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// set records or rows of data per page
$recordsPerPage = 6;

// calculate for the query LIMIT clause
$fromRecordNum = ($recordsPerPage * $page) - $recordsPerPage;
// select all data
$query = "SELECT 
            id, name,price 
        FROM 
            products
        ORDER BY 
            id 
        LIMIT 
            {$fromRecordNum}, {$recordsPerPage}";

            /*
            page and its LIMIT clause looks like:
            1 = 0, 5
            2 = 5,10
            3 = 10,15
            4 = 15, 20
            5 = 20, 25
            */

$stmt = $con->prepare( $query );
$stmt->execute();

//this is how to get number of rows returned
$num = $stmt->rowCount();


if($num>0){
    echo "<table border='0'>";//start table

        // our table heading
        echo "<tr>";
            echo "<th class='textAligncenter'>Nome Produto</th>";
            echo "<th>Preço (EUR)</th>";
            echo "<th>Acção</th>";
        echo "</tr>";

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            extract($row);

            //creating new table row per record
            echo "<tr>";
                echo "<td>{$name}</td>";
                echo "<td class='textAlignRight'>{$price}</td>";
                echo "<td class='textAlignCenter'>";
                    echo "<a href='addToCart.php?id={$id}&name={$name}' class='customButton'>";
                        echo "<img src='images/add-to-cart.png'  class='imagem2'title='Adicionar ao carrinho' />";
                    echo "</a>";
                echo "</td>";
            echo "</tr>";
        }

    echo "</table>";
}

// no products in the database
else{
    echo "No products found.";
}
EN

回答 1

Stack Overflow用户

发布于 2014-07-04 22:47:12

您可以尝试更改:

发自:

代码语言:javascript
复制
<a href='addToCart.php?id={$id}&name={$name}' class='customButton'>

至:

代码语言:javascript
复制
<a href='addToCart.php?id={$id}&name={$name}' onmouseover="document.images['image'].src='images/your_image.jpg' class='customButton'>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24576672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档