如果查询存在,如果不存在,则回显这个吗?

时间:2013-02-08 14:25:09

标签: php mysql

目前我已经查询回复了我网站上的所有成员,它会显示他们的个人资料图片并链接到他们的个人资料页面。说我有50个成员,查询限制为60然后我想用默认图像填充空白空间并链接到默认页面,所以即时通讯查找if语句,如果用户不存在然后回显这张照片而链接呢?

有人可以告诉我哪里出错了。感谢

<?php
        $user_set = get_users();
        while ($users = mysql_fetch_array($user_set)) {



            if (!file_exists($users)) {
    $users = "data/photos/0/_default.jpg";
}



        $age = age_from_dob($users['dob']);
             echo "
            <div class=\"sugarushcase\">
            <a href=\"profile.php?id={$users['id']}\"><img width=80px height= 80px src=\"data/photos/{$users['id']}/_default.jpg\" class=\"boxgrid\"/></a><h58> {$users['first_name']} {$users['last_name']}</h58><br/><br/><h52>{$users['contact_number']}<br/><br/> ".$age.", From {$users['location']}</h52>

            </div>";
        }
    ?> 

2 个答案:

答案 0 :(得分:0)

下面的内容是什么?

<?php


    $user_set = get_users();
    $user_count = mysql_num_rows($user_set);
    while ($users = mysql_fetch_array($user_set)) {

        if (!file_exists($users)) {
            $users = "data/photos/0/_default.jpg";
        }

        $age = age_from_dob($users['dob']);

        echo "<div class=\"sugarushcase\">
        <a href=\"profile.php?id={$users['id']}\"><img width=80px height= 80px src=\"data/photos/{$users['id']}/_default.jpg\" class=\"boxgrid\"/></a><h58> {$users['first_name']} {$users['last_name']}</h58><br/><br/><h52>{$users['contact_number']}<br/><br/> ".$age.", From {$users['location']}</h52>
        </div>";

    }

    // if there were less than 60 users we need some default profiles to fill the spaces
    if($user_count < 60){
        // how many default spaces do we need?
        $default_profiles_needed = 60 - $user_count;        
        for($i = 1; $i <= $default_profiles_needed; $i++){
            echo "<div class=\"sugarushcase\">
                    <a href=\"default.php\">
                        <img width=80px height= 80px src=\"default.jpg\" class=\"boxgrid\"/>
                    </a>
                </div>";
        }
    }


?> 

答案 1 :(得分:0)

你可以寻找这个

   <?php
    $user_set = get_users();
    if (!$user_set) {

                 $users = "data/photos/0/_default.jpg";
                   }
                   else {
    while ($users = mysql_fetch_array($user_set)) {


    $age = age_from_dob($users['dob']);
         echo "
        <div class=\"sugarushcase\">
        <a href=\"profile.php?id={$users['id']}\"><img width=80px height= 80px src=\"data/photos/{$users['id']}/_default.jpg\" class=\"boxgrid\"/></a><h58> {$users['first_name']} {$users['last_name']}</h58><br/><br/><h52>{$users['contact_number']}<br/><br/> ".$age.", From {$users['location']}</h52>

        </div>";
    } }
?>