dongmei5168 2011-11-02 11:05
浏览 31
已采纳

PHP和JavaScript产品/图像查看器

I have this web page I'm developing that displays multiple images of one product in a sort of gallery. However I'm having great trouble trying to have the thumbnail images appear as the main image when clicked.

Here's a link to the page in question: http://www.carbondelight.co.uk./prodview.php?id=50

I'm using the bxslider (bxslider.com) for the thumbnail slider, and my initial plan was to simply write some JavaScript that would change the main image to the relevant thumbnail image. Athough this is where I become stuck, as I can't figure out a good way to achieve this.

I have looked at ways like setting all main images to display:none; bar one image and then altering their display attr via javascipt like Sohtanaka does in his tutorial(http://www.sohtanaka.com/web-design/css-multiple-image-viewer-thumbnails/) and I've looked at changing the src location of the main image with JavaScript.

I will say that I'm no pro with PHP or JavaScript so my code is in no way shape or form efficient, so I'd like to focus on simply getting the question at hand solved rather than changing all the code to make it more efficient. However if I have to rewrite some of the queries to make this work then I'm all ears.

How can I get the thumbnails and main images working?

NOTE: I have cleaned up the code and taken out some simple styling classes to make it more readable. And if it's a benefit I can include the CSS as well.

Here's my code:

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

$sql = "SELECT * FROM partTable WHERE partID='$id'";
$result = performQuery($sql);

$sql2 = "SELECT car FROM carTable WHERE carID='$result[6]'";
$result2 = performQuery($sql2);

$sql3 = "SELECT category FROM categoryTable WHERE categoryID='$result[7]'";
$result3 = performQuery($sql3);

$sql4 = "SELECT medImg, lrgImg FROM imageTable WHERE partID='$id'";
$result4 = mysql_query($sql4);

$rows = mysql_num_rows($result4);

$sql5 = "SELECT smlImg FROM imageTable WHERE partID='$id'";
$result5 = mysql_query($sql5);

$rows5 = mysql_num_rows($result5);

$row = mysql_fetch_row($result4);
echo "<a id='single_image' href='$row[1]'><img src='$row[0]' /></a>"; 

for ( $j = 1 ; $j < $rows ; $j++)
{
    $row = mysql_fetch_row($result4);
    echo "<a id='single_image' href='$row[1]'><img style='display:none;' src='$row[0]'/></a>"; 
}


echo "<div id='slider1'>";

for ( $j = 0 ; $j < $rows5 ; ++$j)
{
    $row = mysql_fetch_row($result5);
    echo "<div class='pv-thumb'><a href='javascript:void(0)' onclick='superalert()'><img  src='$row[0]'  /></a></div>";
}

echo "</div>";
  • 写回答

1条回答 默认 最新

  • doufei0933 2011-11-02 13:01
    关注

    OK, so this is a JavaScript question. To make life easy for yourself, get yourself a copy of jQuery, and then you can do this:

    // Wait for document ready
    $(document).ready(function() {
        // Attach to all thumbnails (give all images a class of 'thumbnail')
        $('img.thumbnail').click(function() {
            // Let us assume your thumbs and medium images are thus:
            // Thumb: /admin/image/proSml/96.jpg
            // Medium: /admin/image/proMed/96.jpg
    
            // Reset source of main image based on thumbnail
            var thumbSrc = $(this).attr('src');
            var medSrc = thumbSrc.replace('proSml', 'proMed');
            $('#mainimage').attr('src', medSrc);
        });
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP