这段代码可以更干净吗?它看起来是正确的状态?

时间:2011-10-09 08:57:58

标签: php mysql

我有这个代码从数据库中提取数据并将数据插入4列中的一列

我花了一整天的时间搜索,似乎无法找到如何做到这一点..

理想情况下,我想从数据库中选择all,然后在fetch数组的列id为1的地方选择 - echo,然后是下一列的sama等。

<?php


            echo '<div class="column grid_3 clearfix" id="column0" >';
            echo '&nbsp;';
            $user_sites_0=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='0' ORDER BY sort_no");

            if(!$user_sites_0) {
                echo 'No sites added, please <a class="addsite" href="#">add one now</a>';
            }
            else {

                while($user_site_0=mysqli_fetch_array($user_sites_0))
                {
                    $id = stripslashes($user_site_0['id']);
                    $site_name = stripslashes($user_site_0['site_name']);
                    $site_address = stripslashes($user_site_0['site_address']);
                    $site_desc = stripslashes($user_site_0['site_desc']);
                    $site_category = stripslashes($user_site_0['site_category']);
                    $getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
                    echo '<div class="dragbox" id="item'.$id.'">';
                    echo '<h2 class="h2handle">'.$site_name.' <span class="close"><a href="#" id="'.$id.'" class="delete_button"><img src="assets/img/closepanel.png"></a></span></h2>';
                    echo '<div class="dragbox-content" ';
                    if($user_site_0['collapsed']==1)
                        echo 'style="display:none;" ';
                    echo '>';
                    echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
                    echo '<p>' . $site_category . '</p>';
                    echo '<p>' . $site_address . '</p>';
                    echo '<p>' . $site_desc . '</p>';
                    echo'   </div>
                    </div>';

                }

            }
            echo '</div>';



    ?>
    <?php


            echo '<div class="column grid_3 clearfix" id="column1" >';
            echo '&nbsp;';
            $user_sites_1=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='1' ORDER BY sort_no");

            if(!$user_sites_1) {
                echo '';
            }
            else {

                while($user_site_1=mysqli_fetch_array($user_sites_1))
                {
                    $id = stripslashes($user_site_1['id']);
                    $site_name = stripslashes($user_site_1['site_name']);
                    $site_address = stripslashes($user_site_1['site_address']);
                    $site_desc = stripslashes($user_site_1['site_desc']);
                    $site_category = stripslashes($user_site_1['site_category']);
                    $getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;

                    echo '<div class="dragbox" id="item'.$id.'">';
                    echo '<h2 class="h2handle">'.$site_name.' <span class="close"><a href="#" id="'.$id.'" class="delete_button"><img src="assets/img/closepanel.png"></a></span></h2>';
                    echo '<div class="dragbox-content" ';
                    if($user_site_1['collapsed']==1)
                        echo 'style="display:none;" ';
                    echo '>';
                    echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
                    echo '<p>' . $site_category . '</p>';
                    echo '<p>' . $site_address . '</p>';
                    echo '<p>' . $site_desc . '</p>';
                    echo'   </div>
                    </div>';

                }
            }
            echo '</div>';



    ?>
    <?php


            echo '<div class="column grid_3 clearfix" id="column2">';
            echo '&nbsp;';
            $user_sites_2=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='2' ORDER BY sort_no");

            if(!$user_sites_2) {
                echo '';
            }
            else {

                while($user_site_2=mysqli_fetch_array($user_sites_2))
                {
                    $id = stripslashes($user_site_2['id']);
                    $site_name = stripslashes($user_site_2['site_name']);
                    $site_address = stripslashes($user_site_2['site_address']);
                    $site_desc = stripslashes($user_site_2['site_desc']);
                    $site_category = stripslashes($user_site_2['site_category']);
                    $getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;

                    echo '<div class="dragbox" id="item'.$id.'">';
                    echo '<h2 class="h2handle">'.$site_name.' <span class="close"><a href="#" id="'.$id.'" class="delete_button"><img src="assets/img/closepanel.png"></a></span></h2>';
                    echo '<div class="dragbox-content" ';
                    if($user_site_2['collapsed']==1)
                        echo 'style="display:none;" ';
                    echo '>';
                    echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
                    echo '<p>' . $site_category . '</p>';
                    echo '<p>' . $site_address . '</p>';
                    echo '<p>' . $site_desc . '</p>';
                    echo'   </div>
                    </div>';
                }
            }
            echo '</div>';              

    ?>
    <?php


            echo '<div class="column grid_3 clearfix" id="column3">';
            echo '&nbsp;';
            $user_sites_3=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='3' ORDER BY sort_no");

            while($user_site_3=mysqli_fetch_array($user_sites_3))
            {
                $id = stripslashes($user_site_3['id']);
                $site_name = stripslashes($user_site_3['site_name']);
                $site_address = stripslashes($user_site_3['site_address']);
                $site_desc = stripslashes($user_site_3['site_desc']);
                $site_category = stripslashes($user_site_3['site_category']);
                $getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;

                echo '<div class="dragbox" id="item'.$id.'">';
                echo '<h2 class="h2handle">'.$site_name.' <span class="close"><a href="#" id="'.$id.'" class="delete_button"><img src="assets/img/closepanel.png"></a></span></h2>';
                echo '<div class="dragbox-content" ';
                if($user_site_3['collapsed']==1)
                    echo 'style="display:none;" ';
                echo '>';
                echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
                echo '<p>' . $site_category . '</p>';
                echo '<p>' . $site_address . '</p>';
                echo '<p>' . $site_desc . '</p>';
                echo'   </div>
                </div>';
            }
            echo '</div>';              

    ?>

代码看起来是正确的状态..我能做得更好吗?

2 个答案:

答案 0 :(得分:3)

使用for并更改代码,部分代码:

<?php

for ($i=0; $i<4; $i++)
{
  echo '<div class="column grid_3 clearfix" id="column'.$i.'" >';
  echo '&nbsp;';
  $user_sites=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='".$i."' ORDER BY sort_no");
  if(!$user_sites) {
    echo 'No sites added, please <a class="addsite" href="#">add one now</a>';
  }
  else
  {
    while($user_site=mysqli_fetch_array($user_sites))
    {
       ... // Do it yourself 

答案 1 :(得分:0)

也许

    function get_site($column_id){
    $str = "";

     $user_sites = mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='".$column_id."' ORDER BY sort_no");
    if(!$user_sites_0) {
                echo 'No sites added, please <a class="addsite" href="#">add one now</a>';
            }
            else { 
            while($user_site_0=mysqli_fetch_array($user_sites_0))
                {
                    $id = stripslashes($user_site_0['id']);
                    $site_name = stripslashes($user_site_0['site_name']);
                    $site_address = stripslashes($user_site_0['site_address']);
                    $site_desc = stripslashes($user_site_0['site_desc']);
                    $site_category = stripslashes($user_site_0['site_category']);
                    $getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
                    $str.= '<div class="dragbox" id="item'.$id.'">';
                    $str.= '<h2 class="h2handle">'.$site_name.' <span class="close"><a href="#" id="'.$id.'" class="delete_button"><img src="assets/img/closepanel.png"></a></span></h2>';
                    $str.= '<div class="dragbox-content" ';
                    if($user_site_0['collapsed']==1)
                        echo 'style="display:none;" ';
                    $str.= '>';
                    $str.= '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
                    $str.= '<p>' . $site_category . '</p>';
                    $str.= '<p>' . $site_address . '</p>';
                    $str.= '<p>' . $site_desc . '</p>';
                    $str.='   </div>
                    </div>';

                }
            }
            $str.='</div>';
            return $str;}

echo '<div class="column grid_3 clearfix" id="column0">';
echo '&nbsp;';
echo oget_site(0);

echo '<div class="column grid_3 clearfix" id="column1">';
echo '&nbsp;';
echo oget_site(1);
//....