开发者

Could this code be cleaner?? It looks a right state?

I have this code to pull data from the database and insert the data into one of 4 columns,

I have spent a whole day searching and just cant seem to find out how to do it..

Ideally, I want to select all from database and then where the fetch array has a column id of 1 - echo that then the sama for the next column etc..

<?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>';              

    ?>

The code looks a right state.. Could I do it better?


Use for and change the code, some code :

<?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 


maybe

    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);
//....
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜