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

私の以前の答えは正しくありませんが、「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 に答える
1

return $images;while の外にある必要があります。そうしないと、サイクルが完了しません。

DBまたはデータの設計にエラーがあるため、コードが機能しないと思います。たとえば、DB の「imagenum」は何を意味するのでしょうか。この値を使用して画像を取得する必要がありますか?

于 2012-10-17T08:28:19.110 に答える