2013-02-10 22 views
0

Hi我的網站上有一個基本的喜歡系統。基本上一旦用戶點擊,這將user_id-has_liked設置爲0.如果mysql的結果不存在回顯?

如果他們的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> 

回答

0

嘗試,爭取在您的迴音/打印功能的使用的一致性和終止的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. 
?> 
+0

這不會工作,因爲如果沒有找到記錄,mysql_fetch_array將返回false,因此它不會進入內部循環。 – CuriousMind 2013-02-10 14:33:08

+0

非常真實,謝謝。如果他只是按照他的代碼建議尋找單個條目,那麼他可以用if語句取代while,並添加另一個else子句。 – Montycarlo 2013-02-10 15:08:30

+0

所以有人可以告訴我我需要做什麼,我很困惑。謝謝 – 2013-02-10 15:11:26

0

您應該試試mysql_num_rows。它會給你記錄集中的行數。 除此之外,請謹慎使用,您正在使用的API已被棄用。在mysql網站上尋找建議的替代方案。

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>