如果mysql结果不存在echo?

时间:2013-02-10 14:20:11

标签: php mysql

您好我的网站上有一个基本的喜欢系统。基本上,一旦用户点击了,就会将user_id-has_liked从0设置为1。

如果他们的user_id_has_liked设置为0,则显示类似链接,如果设置为1则显示不同。但是我想添加另一个条件,如果结果不在mysql中,那么回显出类似的链接。

有人可以告诉我在哪里以及我想添加什么来实现这一目标。

<div class="profile_likes">
<?php
$user_like_set = user_like_status();
while ($like = mysql_fetch_array($user_like_set)) 
if ($like['user_id_has_liked'] == '0')  { ?>

<a href="like_profile.php?to=<?php echo "$profile_id"; ?>">Like</a>&nbsp;&nbsp;|&nbsp;&nbsp;<?

$count_likes_set = count_likes();
while ($likes = mysql_fetch_array($count_likes_set)) {

    echo "". $likes['likes'] ." People Like ".$profile[2]."";

    //$check_new_duos_set = check_new_escort_duos(); while ($newd = mysql_fetch_array($check_new_duos_set)) { 

    ?>

    <? } }?>



    <?php
$user_like_set = user_like_status();
while ($like = mysql_fetch_array($user_like_set)) 
if ($like['user_id_has_liked'] == '1')  { ?>

<a href="unlike_profile.php?to=<?php echo "$profile_id"; ?>">Unlike</a>&nbsp;&nbsp;|&nbsp;&nbsp;<?

$count_likes_set = count_likes();
while ($likes = mysql_fetch_array($count_likes_set)) {

    echo "". $likes['likes'] ." People Like ".$profile[2]."";

    //$check_new_duos_set = check_new_escort_duos(); while ($newd = mysql_fetch_array($check_new_duos_set)) { 
    ?>

    <? } }?>
    </div>

3 个答案:

答案 0 :(得分:0)

尝试在使用echo / print函数时保持一致性并通过终止php标记来模拟它们,您的代码将变得更加清晰。

<?php
    function printLikeLink($likeLink){
          echo "<a href='{$likeLink}'>Like this profile</a>";
    }
    $user_like_set = user_like_status();
    if ($like = mysql_fetch_array($user_like_set)){
        if ($like['user_id_has_liked'] == '1')  {
            echo "<a href=\"unlike_profile.php?to={$profile_id}\">Unlike</a>|";
            $count_likes_set = count_likes();
            while ($likes = mysql_fetch_array($count_likes_set)) {
                echo "". $likes['likes'] ." People Like ".$profile[2]."";
                //$check_new_duos_set = check_new_escort_duos(); while ($newd = mysql_fetch_array($check_new_duos_set)) { 
            }
        }else printLikeLink("like_profile.php?to={$profile_id}");
    }else printLikeLink("like_profile.php?to={$profile_id}"); // Edit this link.
?>

答案 1 :(得分:0)

你应该尝试mysql_num_rows。它将为您提供记录集中存在的行数。 除此之外,请注意,您使用的API已被弃用。在mysql网站上寻找建议的替代方案。

答案 2 :(得分:0)

尝试这个我只是做了一个改变你的user_like_status();应该返回true或false

<div class="profile_likes">
<?php
    $user_like_set = user_like_status();//should return true or false
    if ($user_like_set == false){
        $what = "Like"; 
    }else{
        $what = "Unlike";
    }?>    
    <a href="like_profile.php?to=<?php echo "$profile_id"; ?>"><?php echo $what ;?></a>&nbsp;&nbsp;|&nbsp;&nbsp;
    <?php 
    $count_likes_set = count_likes();
    while ($likes = mysql_fetch_array($count_likes_set)) {
    echo "". $likes['likes'] ." People Like ".$profile[2]."";
    }
 ?>
</div>