1

我需要根据返回的 4 行向我的页面添加一个图像(mysql 中的保存路径 - 服务器上文件中的图像)。从 db 将始终返回 4 行。

我的问题是它在所有 4 个图像持有者中都显示了相同的图像(带有 3 个缩略图的主图像)。我已使用以下内容添加图像...

首先我的php函数-

//Get all images within a specific album...
function get_images($vehicleid) {
    //$vehicle_id = (int)$vehicle_id;

    $images = array();

    $image_query = mysql_query("SELECT * FROM `images` WHERE `images`.`vehicle_id`='$vehicleid'");

    $result = $image_query;

    if (!$result){
       echo 'Error in loading vehicle adverts. Please refresh page and try again';
       exit();
    }

    while($images_row = mysql_fetch_assoc($image_query)) {
        $images[] = array(
            'imageid' => $images_row['image_id'],
            'branch' => $images_row['branch'],
            'vehicleid' => $images_row['vehicle_id'],
            'timestamp' => $images_row['timestamp'],
            'ext' => $images_row['ext'],
            'imagenum' => $images_row['imagenum']
        );

        //This returns main image and 3 thumbs, but only image from first record is returned..
                     return $images;
    }
        //return $images;
                    //When runned from here, main image is from 1st, then 2nd, 3rd etc row returned, but it loads each rows image as a main and the thumbs the same image...
}

我用来加载图像的代码如下......

<?php
    //Get images...
$images = get_images($vehicleid);

if (empty($images)) {
    ?>

    <table id="thumbnails">
        <tr>
            <td>                                        
                <div>
                <a href="testdrive.php" style="text-decoration:none"><img src="uploads/noimage.jpg" width="600px" height="400px" title="Sorry, No Image Available Yet" /></a>
                </div>                    
            </td>
            <td>
                <table width="350px">
                    <tr height="50px">
                    </tr>
                    <tr>
                        <td>
                            <div id="mainimages" align="center" style="font-size:14px">
                                <?php echo $year, ' ', $manufacturer, ' ', $modelrange, ' ', $modelder, '.', '<br />', '<br />', 'This beautiful ', $colour, ' ',$manufacturer, ' only has ', $mileage, ' kilometres  with a service history and is selling for ', '<strong>ONLY R ', $price, '. </strong>', '<br />', '<br />', 'It features ', $comments, '<br />', '<br />', 'This vehicle is available NOW from our ', '<a href="testdrive.php" style="text-decoration:none">', 'Tata - ', $branch, ' branch.</a>'; ?> 
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <div id="mainimages" align="center" style="font-size:14px">
                                 <?php //MAIN IMAGE HERE FROM RECORD 1 (Note from upload folder, not thumbs as below)...
                                 echo '<a href="delete_image.php?vehicle_id='.$vehicleid.'"><img src="images/delete.png" title="Delete Advert" /></a>'; 
                                 ?>
                            </div>
                        </td>
                    </tr>
                </table>
            </td>
            </tr>
            <tr>
            <td>
                <img src="uploads/thumbs/noimage.jpg" title="Sorry, No Image Available Yet" width="100px" height="100px" /><img src="uploads/thumbs/noimage.jpg" title="Sorry, No Image Available Yet" width="100px" height="100px" /><img src="uploads/thumbs/noimage.jpg" title="Sorry, No Image Available Yet" width="100px" height="100px" />

                <p style="height:10px"></p>
            </td>
        </tr>
    </table>

    <?php
        //echo '<img src="uploads/noimage.jpg" />';
        } else {
            //Display images thumbs...
            foreach ($images as $image) {
    ?>

    <table id="thumbnails">
        <tr>
            <td>
                <?php 
                    //Add image title to the right of main image...
                    echo '<a href="testdrive.php" style="text-decoration:none"><img src="uploads/', $image['vehicleid'], '/', $image['imageid'], '.', $image['ext'], '" title="Uploaded ', date('D M Y / h:i', $image['timestamp']), ' - ', $year, ' ', $manufacturer, ' ', $modelrange, ' ', $modelder, '.', 'This beautiful ', $colour, ' ',$manufacturer, ' only has ', $mileage, ' kilometres and is selling for ', 'ONLY R ', $price, '.', '" width="600px" height="400px" /></a>'; 
                ?>
                </div>
            </td>
            <td>
                <table width="350px">
                    <tr height="50px">
                    </tr>
                    <tr>
                        <td>
                            <div id="mainimages" align="center" style="font-size:14px">
                                <?php 
                                echo $year, ' ', $manufacturer, ' ', $modelrange, ' ', $modelder, '.', '<br />', '<br />', 'This beautiful ', $colour, ' ',$manufacturer, ' only has ', $mileage, ' kilometres with a service history and is selling for ', '<strong>ONLY R ', $price, '. </strong>', '<br />', '<br />', 'It features ', $comments, '<br />', '<br />', 'This vehicle is available NOW from our ', '<a href="testdrive.php" style="text-decoration:none">', 'Tata - ', $branch, ' branch.</a>'; 
                                ?> 
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <div id="mainimages" align="center" style="font-size:14px">
                                 <?php
                                 echo '<a href="delete_image.php?vehicle_id='.$vehicleid.'"><img src="images/delete.png" title="Delete Advert" /></a>'; 
                                 ?>
                            </div>
                        </td>
                    </tr>
                </table>
            </td>
            </tr>
            <tr>
            <td>
                <?php //THIS WHERE I NEED TO START LOADING THE CORRECT IMAGES - THIS SHOULD BE IMAGE RETURNED FROM RECORD 2...
                echo '<a href="testdrive.php" style="text-decoration:none"><img src="uploads/thumbs/', $image['vehicleid'], '/', $image['imageid'], '.', $image['ext'], '" title="Uploaded ', date('D M Y / h:i', $image['timestamp']), ' - ', $year, ' ', $manufacturer, ' ', $modelrange, ' ', $modelder, '.', 'This beautiful ', $colour, ' ',$manufacturer, ' only has ', $mileage, ' kilometres and is selling for ', 'ONLY R ', $price, '.', '" onclick="showImage("uploads/', $image['vehicleid'], '/', $image['imageid'], '.', $image['ext'], '" width="100px" height="100px")" /></a>';

    //THIS SHOULD BE IMAGE RETURNED FROM RECORD 3...
                echo '<a href="testdrive.php" style="text-decoration:none"><img src="uploads/thumbs/', $image['vehicleid'], '/', $image['imageid'], '.', $image['ext'], '" title="Uploaded ', date('D M Y / h:i', $image['timestamp']), ' - ', $year, ' ', $manufacturer, ' ', $modelrange, ' ', $modelder, '.', 'This beautiful ', $colour, ' ',$manufacturer, ' only has ', $mileage, ' kilometres and is selling for ', 'ONLY R ', $price, '.', '" onclick="showImage("uploads/', $image['vehicleid'], '/', $image['imageid'], '.', $image['ext'], '" width="100px" height="100px")" /></a>';

    //THIS SHOULD BE IMAGE RETURNED FROM RECORD 4...
                echo '<a href="testdrive.php" style="text-decoration:none"><img src="uploads/thumbs/', $image['vehicleid'], '/', $image['imageid'], '.', $image['ext'], '" title="Uploaded ', date('D M Y / h:i', $image['timestamp']), ' - ', $year, ' ', $manufacturer, ' ', $modelrange, ' ', $modelder, '.', 'This beautiful ', $colour, ' ',$manufacturer, ' only has ', $mileage, ' kilometres and is selling for ', 'ONLY R ', $price, '.', '" onclick="showImage("uploads/', $image['vehicleid'], '/', $image['imageid'], '.', $image['ext'], '" width="100px" height="100px")" /></a>';
                ?>

                <p style="height:10px"></p>
            </td>
        </tr>
    </table>

    <?php
            }
        }
    }
   ?>

任何人都知道如何将正确的图像返回给他们各自的 html 持有者?

谢谢各位。

4

2 回答 2

1

return $images;必须在 while 之外,否则循环永远不会完成。

我想代码不起作用,因为数据库或数据设计中存在错误。例如,数据库中的“imagenum”是什么意思?你应该使用这个值来检索图像吗?

于 2012-10-17T08:28:19.110 回答
1

我之前的回答不正确,但仍然有效,因为您从不使用“imagenum”。并且返回还必须在一段时间之外。

无论如何,您始终获得相同图像的原因是您始终使用相同的数组元素。

尝试:

$i=0;
foreach ($images as $image) {
    If($i=0) { 
?>

<table id="thumbnails">
    <tr>
        <td>
            <?php 
                //Add image title to the right of main image...
                echo '<a href="testdrive.php" style="text-decoration:none"><img src="uploads/', $image['vehicleid'], '/', $image['imageid'], '.', $image['ext'], '" title="Uploaded ', date('D M Y / h:i', $image['timestamp']), ' - ', $year, ' ', $manufacturer, ' ', $modelrange, ' ', $modelder, '.', 'This beautiful ', $colour, ' ',$manufacturer, ' only has ', $mileage, ' kilometres and is selling for ', 'ONLY R ', $price, '.', '" width="600px" height="400px" /></a>'; 
            ?>
            </div>
        </td>
        <td>
            <table width="350px">
                <tr height="50px">
                </tr>
                <tr>
                    <td>
                        <div id="mainimages" align="center" style="font-size:14px">
                            <?php 
                            echo $year, ' ', $manufacturer, ' ', $modelrange, ' ', $modelder, '.', '<br />', '<br />', 'This beautiful ', $colour, ' ',$manufacturer, ' only has ', $mileage, ' kilometres with a service history and is selling for ', '<strong>ONLY R ', $price, '. </strong>', '<br />', '<br />', 'It features ', $comments, '<br />', '<br />', 'This vehicle is available NOW from our ', '<a href="testdrive.php" style="text-decoration:none">', 'Tata - ', $branch, ' branch.</a>'; 
                            ?> 
                        </div>
                    </td>
                </tr>

                <tr>
                    <td>
                        <div id="mainimages" align="center" style="font-size:14px">
                             <?php
                             echo '<a href="delete_image.php?vehicle_id='.$vehicleid.'"><img src="images/delete.png" title="Delete Advert" /></a>'; 
                             ?>
                        </div>
                    </td>
                </tr>
            </table>
        </td>
        </tr>
        <tr>
        <td>
            <?php $i=1; } else {//THIS WHERE I NEED TO START LOADING THE CORRECT IMAGES - THIS SHOULD BE IMAGE RETURNED FROM RECORD 2...
            echo '<a href="testdrive.php" style="text-decoration:none"><img src="uploads/thumbs/', $image['vehicleid'], '/', $image['imageid'], '.', $image['ext'], '" title="Uploaded ', date('D M Y / h:i', $image['timestamp']), ' - ', $year, ' ', $manufacturer, ' ', $modelrange, ' ', $modelder, '.', 'This beautiful ', $colour, ' ',$manufacturer, ' only has ', $mileage, ' kilometres and is selling for ', 'ONLY R ', $price, '.', '" onclick="showImage("uploads/', $image['vehicleid'], '/', $image['imageid'], '.', $image['ext'], '" width="100px" height="100px")" /></a>';

            ?>



<?php
        }
    }
}
?>
            <p style="height:10px"></p>
        </td>
    </tr>
</table>

或类似的东西(在stackoverflow编辑器中编写代码很困难)。

于 2012-10-17T08:43:56.253 回答