简体   繁体   English

在数组中打开和关闭div

[英]Opening and closing divs in an array

I am displaying a list from mysql. 我正在显示来自mysql的列表。 (http://www.jetcleandrives.co.uk/Areas_Covered.php) (http://www.jetcleandrives.co.uk/Areas_Covered.php)

Here is the code, 这是代码,

if ($rows['area_shire'] != $area_shire && isset ($rows['area_shire'])){   
        $area_shire = $rows['area_shire'];
            $area_shire_url = str_replace(' ', '_', $area_shire);
        echo '<div class="area_name"><h2><a href=Driveway_Cleaning_'.$area_shire_url.'>'.$area_shire.'</a></h2></div><br>';

    }  

    echo '<div class "areas_column">';     
    if ($rows['area_district'] != $area_district && isset ($rows['area_district'])){
        $area_district = $rows['area_district'];
        $area_district_url = str_replace(' ', '_', $area_district);
        echo '<div class="area_name"><h3><a href=Driveway_Cleaning_'.$area_district_url.'>'.$area_district.'</a></h3></div><br>';

    }  



    if ($rows['area_name'] != $area_name &&  isset ($rows['area_name'])){
        $area_name = $rows['area_name'];
        $area_name_url = str_replace(' ', '_', $area_name);
        echo '<div class="area_name"><a href=Driveway_Cleaning_'.$area_name_url.'>'.$area_name.'</a></div><br>';

However i don't want it to be one long verticle list. 但是我不希望它成为一个长长的Verticle列表。 I want each district eg Birmingham, Sandwell etc to start a new "column" to the right using divs and float:left However i can't work out where to start and close the div. 我希望每个区(例如伯明翰,桑德韦尔等)都使用div和float:left在右侧开始一个新的“列”,但是我无法确定从哪里开始和关闭div。 Can anyone help? 有人可以帮忙吗?

Try something like this: 尝试这样的事情:

$area_district = null; // set this to null initially, so you can identify the first iteration
if ($rows['area_shire'] != $area_shire && isset ($rows['area_shire'])){   
    $area_shire = $rows['area_shire'];
    $area_shire_url = str_replace(' ', '_', $area_shire);
    echo '<div class="area_name"><h2><a href=Driveway_Cleaning_'.$area_shire_url.'>'.$area_shire.'</a></h2></div><br>';

}  

if ($rows['area_district'] != $area_district && isset ($rows['area_district'])){
    if($area_district!==null) echo '</div>'; // only close the previous div if this isn't the first
    echo '<div class "areas_column">'; // open the div only when the district changes
    $area_district = $rows['area_district'];
    $area_district_url = str_replace(' ', '_', $area_district);
    echo '<div class="area_name"><h3><a href=Driveway_Cleaning_'.$area_district_url.'>'.$area_district.'</a></h3></div><br>';

}  

if ($rows['area_name'] != $area_name &&  isset ($rows['area_name'])){
    $area_name = $rows['area_name'];
    $area_name_url = str_replace(' ', '_', $area_name);
    echo '<div class="area_name"><a href=Driveway_Cleaning_'.$area_name_url.'>'.$area_name.'</a></div><br>';
}
echo '</div>'; // close the last div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM