推荐用户视频和youtube一样

时间:2014-12-02 18:50:25

标签: php mysql

我正在尝试制作像主页一样的YouTube。使用下面的代码,我想制作推荐给我的用户的视频。

以下代码仅显示用户的视频。

<?php $query = "SELECT
                 user.uid,
                 user.user_name,
                 user.user_avatar,
                 user_posts.uid_dk,
                 user_posts.post_id,
                 user_posts.post_name,
                 user_posts.post_info,
                 user_posts.post_time,
                 user_posts.post_ext,
                 user_posts.post_num,
                 user_posts.post_views
                 FROM user 
                 JOIN user_posts 
                 ON user_posts.uid_dk = user.uid 
                 WHERE user_name='$user_name' LIMIT 5";
                 $run_query = mysql_query($query);
                 while($data=mysql_fetch_assoc($run_query)){
                     $post_name=$data['post_name'];
                     $post_time = $data['post_time'];
                     $post_views = $data['post_views'];
                     $post_numid = $data['post_num'];
                     $post_id = $data['post_id'];
                     $user_name = $data['user_name'];
                     $user_avatar = $data['user_avatar'];
                 ?>

<div class="onerilent"><img src="<?php echo $user_avatar;?>"><?php echo $user_name ;?> Recommended for you</div>
<div class="onmnwrp">
   <div class="onmn">
     <div class="onmn_img"><img src="<?php echo $base_url.'user_uploads/'.$post_num;?>.png"></div>
     <div class="onmg_tit"><?php echo $post_name;?></div>
     <div class="onm_snm">gönderen: <?php echo $user_name;?></div>
     <div class="onm_tim"><?php echo $post_views;?> views</div>
   </div>
</div>
<?php  } ?>

我想只展示一次此部分。

<div class="onerilent"><img src="<?php echo $user_avatar;?>"><?php echo $user_name ;?> Recommended for you</div>

任何人都可以在这方面帮助我吗?

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用计数器,如下所示:

<?php 
$query = "SELECT
        user.uid,
        user.user_name,
        user.user_avatar,
        user_posts.uid_dk,
        user_posts.post_id,
        user_posts.post_name,
        user_posts.post_info,
        user_posts.post_time,
        user_posts.post_ext,
        user_posts.post_num,
        user_posts.post_views
    FROM user 
    JOIN user_posts 
    ON user_posts.uid_dk = user.uid 
    WHERE user_name='$user_name' LIMIT 5";

    $run_query = mysql_query($query);

    $counter = 1;


    while($data=mysql_fetch_assoc($run_query)){
        $post_name=$data['post_name'];
        $post_time = $data['post_time'];
        $post_views = $data['post_views'];
        $post_numid = $data['post_num'];
        $post_id = $data['post_id'];
        $user_name = $data['user_name'];
        $user_avatar = $data['user_avatar'];


        if($counter == 1){
            $counter++;
            echo '<div class="onerilent"><img src="'.$user_avatar.'">'.$user_name.' Recommended for you</div>';
        }
        ?>    

        <div class="onmnwrp">
            <div class="onmn">
                <div class="onmn_img"><img src="<?php echo $base_url.'user_uploads/'.$post_num;?>.png"></div>
                <div class="onmg_tit"><?php echo $post_name;?></div>
                <div class="onm_snm">gönderen: <?php echo $user_name;?></div>
                <div class="onm_tim"><?php echo $post_views;?> views</div>
            </div>
        </div>
        <?php 
    }
    ?>

注意$ counter变量在循环之前设置为1,并且在循环内部有一个条件来检查它是否设置为值1,如果是,则它回显你的html和递增$ counter,使其不再设置为1