0

我正在尝试按布局和排序顺序对图像进行排序。我已经做到了,但不完全是我想要的样子。

<div id="sortableImages">
    <ul>
        <?php
            $galleryID = $_SESSION['newGalleryId'];

            $query  = "SELECT * 
                        FROM galleryimage 
                        WHERE galleryId = $galleryID
                        ORDER BY sort 
                        AND layout
                        ASC";
            $result = mysql_query($query);

            while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $sortImageName = $row['OrgImageName']; 
                $sortPath = "../data/gallery/" . $_GET['edit'] . "/images/album/" . $sortImageName;
        ?>
            <li class="sortPhotos" id="recordsArray_<?php echo $row['id']; ?>">
                <img src="<?php echo $sortPath ?>"  />                            
            </li>
        <?php } ?>
    </ul>
</div>

这给了我一个输出

<ul class="ui-sortable">
                    <li class="sortPhotos" id="recordsArray_1392">
                <img src="../data/gallery/208/images/album/front_cover.jpg"  />                            
            </li>
                    <li class="sortPhotos" id="recordsArray_1393">
                <img src="../data/gallery/208/images/album/007.jpg"  />                            
            </li>
                    <li class="sortPhotos" id="recordsArray_1394">
                <img src="../data/gallery/208/images/album/009.jpg"  />                            
            </li>
</ul>

我希望能够让它看起来像

<h2>Layout 1</h2>
<ul class="ui-sortable">
                    <li class="sortPhotos" id="recordsArray_1392">
                <img src="../data/gallery/208/images/album/front_cover.jpg"  />                            
            </li>
</ul>
<h2>Layout 2</h2>
<ul>
                    <li class="sortPhotos" id="recordsArray_1393">
                <img src="../data/gallery/208/images/album/007.jpg"  />                            
            </li>
                    <li class="sortPhotos" id="recordsArray_1394">
                <img src="../data/gallery/208/images/album/009.jpg"  />                            
            </li>
</ul>

我需要一点帮助来弄清楚如何在 PHP 中正确嵌套循环。任何帮助将是一个很大的帮助。谢谢

4

3 回答 3

1
<?php

$galleryID = $_SESSION['newGalleryId'];

$layout1 = $layout2 = "";

$query  = "SELECT * 
            FROM galleryimage 
            WHERE galleryId = $galleryID
            ORDER BY sort 
            AND layout
            ASC";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $sortImageName = $row['OrgImageName']; 
    $sortPath = "../data/gallery/" . $_GET['edit'] . "/images/album/" . $sortImageName;
    if ($sortImageName == 'front_cover.jpg'){
        $layout1 .= 
            "<li class='sortPhotos' id='recordsArray_{$row['id']}'>
                <img src="$sortPath"  />                            
            </li>";
    } else {
        $layout2 .= 
            "<li class='sortPhotos' id='recordsArray_{$row['id']}'>
                <img src="$sortPath"  />                            
            </li>";
    }
} 
?>
<h2>Layout 1</h2>
<ul class="ui-sortable">
    <?=$layout1?>
</ul>
<h2>Layout 2</h2>
<ul>
    <?=$layout2?>
</ul>
于 2012-06-14T22:52:52.913 回答
0

您需要适当地设置一个标志。

<div id="sortableImages">
    <h2>Layout 1</h2>
    <ul class="ui-sortable">

    // Etc...

    $first = true;
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $sortImageName = $row['OrgImageName']; 
        $sortPath = "../data/gallery/" . $_GET['edit'] . "/images/album/" . $sortImageName;

    ?>
        <li class="sortPhotos" id="recordsArray_<?php echo $row['id']; ?>">
             <img src="<?php echo $sortPath ?>"  />                            
        </li>
    <?php 
        if( $first === true) {
            $first = false;
    ?>
    </ul>
        <h2>Layout 2</h2>
    <ul>
    <?php
        }
    } 
?>
    </ul> 
</div>
于 2012-06-14T22:47:42.567 回答
0

$query = "SELECT * FROM galleryimage WHERE galleryId = $galleryID ORDER BY sort AND layout ASC";

应该:

SELECT 
   layout,sort, id, orgImageName 
FROM galleryimage
GROUP BY layout
ORDER BY layout, sort
于 2012-06-14T22:52:38.740 回答