0

With the current code, it's very inefficient, confusing and seems to lack performance.

What could I improve in the MySQL Querying, or PHP to make this code easier and less [ all over the place ].

Are there better ways to replace what I'm currently trying to accomplish, which is to retrieve information from the database [ according to the iD ] and display them. My question is mostly based towards the foreach.

Posts table
+---------------+
|Post_iD        |
|message        |
|uid_fk         |
|tagged_people  |
|_iP            |
|created        |
|uploads        |
+---------------+

->load_messages.php -->

<?php
    $lastid = "";
if ($lastid == '')
    $lastid = 0;

if ( $_iD ) {
    $updatesarray = $Wall->Updates($_iD, $lastid);
    $total        = $Wall->Total_Updates($_iD);
} else {
    $updatesarray = $Wall->Friends_Updates($_iD, $lastid);
    $total        = $Wall->Total_Friends_Updates($_iD);
}

if ($updatesarray) {
    foreach ($updatesarray as $data) {
        $post_iD     = $data['post_iD'];
        $orimessage = $data['message'];
        $message    = tolink(htmlcode($data['message']));
        $time       = $data['created'];
        $mtime      = date("g:i", $time);
        $_iUsername   = $data['_iUsername'];
        $uploads    = $data['uploads'];
        $msg_uid    = $data['uid_fk'];
?>  
    <ul class="_Om" id="stbody<?php echo $post_iD;?>">
        <li>
            <div class="_VC">
                <div class="stimg">
                    <img src="<?php echo $Profile_Pic;?>" class='picture' alt='<?php echo $_iUsername;?>'/>
                </div>      
                <div class="">
                    <b class="timeago">
                        <?php echo $_iUsername;?>&nbsp;&nbsp;&middot;&nbsp;
                            <a href='<?php echo $base_url ?>status/<?php echo $post_iD; ?>'title='<?php echo $time;?>' class="timeposted">
                                    <?php echo $mtime;?>
                            </a>
                    </b> 
                </div> 
                <div class="body_text">
                    <?php echo clear($message);?>
                </div>
                <?php
                    if ($uploads) {
                        echo "<div style='margin-top:10px'>";
                        $s = explode(",", $uploads);
                        foreach ($s as $a) {
                            $newdata = $Wall->Get_Upload_Image_Id($a);
                            if ($newdata)
                                echo "<a href='uploads/" . $newdata['image_path'] . "' rel='facebox'><img src='uploads/" . $newdata['image_path'] . "' class='imgpreview' /></a>";
                        }
                        echo "</div>";
                    }
                ?>

                <div class="stexpandbox">
                    <div class="stexpand<?php echo $post_iD;?>">
                        <?php
                            if (textlink($orimessage)) {
                            $link = textlink($orimessage);
                            echo Expand_URL($link); 
                            }
                        ?>     
                    </div>
                </div>
                <?php if ($_iD == $msg_uid) { ?>
                    <a class="stdelete" href="#" id="<?php echo $post_iD;?>" title="Delete Update"></a>
                <?php } ?>
            </div>      
        </li>
    </ul>
<?php } if ($total > $perpage) { ?>
        <div id="more<?php echo $post_iD;?>" class="morebox">
            <a href="#" class="more" id="<?php echo $post_iD;?>">More</a>
        </div>
<?php } } else echo '<h3 id="noupdates">No Updates!</h3>';?> 

Database query (Updates,Total_Updates,Friends_Updates,Total_Friends_Updates)

http://pastie.org/private/j66vwnsfyk1fai7zlnrrg
4

1 回答 1

2

让我给你我的想法。

这是非常低效的

从头开始重新编码,希望你会发现你所做的一些设计缺陷。也许你可以停下来:

 $post_iD     = $data['post_iD'];

据我所知,这些东西在同一个循环中,所以只需使用关联数组即可。

令人困惑

尝试使用 MVC 架构模式并为您的代码添加一些注释。MVC 应该有很大帮助,而且功能可以走很长的路。

并且:

        <?php if ($_iD == $msg_uid) { ?>
                <a class="stdelete" href="#" id="<?php echo $post_iD;?>" title="Delete Update"></a>
        <?php } ?>

那个锚是要删除还是不删除?因为它似乎缺少一些文本。除非样式它的 CSS 位于同一页面的顶部?从我的角度来看,您所做的与嵌入 css 没有什么不同。我认为

<?php

   if ($_iD == $msg_uid) { 

      echo '<a class="stdelete" href="#" id="'.$post_iD.'" title="Delete Update">Delete</a>';

   }

?>

对大多数程序员来说更具可读性。

缺乏表现

提前规划您的系统,编写更好的查询,只选择它需要的内容,如果失败,确定帖子的重要性。如果这不是世界末日,如果你失去了一对。查看 NoSQL 数据库。但是从您提交的代码来看,这将是缺乏性能而不是 MySQL 的罪魁祸首。

于 2013-06-19T02:46:57.353 回答