简体   繁体   English

php / codeigniter-使用while链接两个功能

[英]php/codeigniter - using while to link two functions

I've got some follow/unfollow and block/unblock functions in my app. 我的应用程序中有一些关注/取消关注和阻止/取消阻止功能。 They work fine and the correct data is populating my database. 它们工作正常,正确的数据正在填充我的数据库。 Here's an outline of my logic: 这是我的逻辑概述:

if($this->user_is_followed) {
    //show unfollow button
}else {     
    //show follow button

}

if($this->user_is_blocked) {    
    //show unblock button
}else {
    //show block button
}

One issue: I need to link the follow/unfollow and block/unblock functions for these 2 scenarios: 一个问题:对于这两种情况,我需要链接关注/取消关注和阻止/取消阻止功能:

1 - If a user is following someone, and clicks the block button, that user should be blocked and unfollowed (right now, the user is blocked, but not unfollowed) 1-如果用户正在关注某人,然后单击“阻止”按钮,则该用户应被阻止并取消关注(目前,该用户已被阻止,但尚未取消关注)

2 - If a user is blocking someone, and clicks the follow button, that user should be followed and unblocked (right now, the user is followed, but not unblocked) 2-如果用户正在阻止某人,并单击“关注”按钮,则应跟踪并取消对该用户的关注(目前,已跟踪该用户,但未对其进行阻止)

I tried using -while- to link the 2 functions, but nothing shows up, not even errors, just a blank screen: 我尝试使用-while-链接这两个函数,但是什么都没有显示,甚至没有错误,只是一个空白屏幕:

Here's my attempt at the while logic: 这是我对while逻辑的尝试:

    if($this->user_is_followed) {
        while($this->user_is_blocked) {
            //show unfollow button
        }
    }       
    else if($this->user_is_blocked) {
        while($this->user_is_followed) {        
        //show follow button 
        }       
    }

Simply put, I'm trying to figure out which php control structure I can use for this. 简而言之,我正在尝试找出可以用于此的php控制结构。 Is it -while- or something else? 是-一会儿-还是其他?

The first one would be fine depending on whether or not you could be followed and blocked at the same time. 根据是否可以同时跟踪和阻止您,第一个会很好。

Since it looks like you're doing this for inline output, you might want to just condense it down to a ternary: 由于看起来您正在为内联输出执行此操作,因此您可能希望将其压缩为三进制:

<?php echo ($this->user_is_followed ? 'unfollow button' : 'follow button'); ?>

The ternary would be bad if you had extensive markup since echoing html in php is generally a dumb idea. 如果您具有广泛的标记,则三元数将是不好的,因为在php中回显html通常是一个愚蠢的想法。 But if you were using a library like jquery and listeners on classes you could assign a "followed" or "not-followed" class on an <a> tag, then display according to css... hope that made sense. 但是,如果在类上使用诸如jquery和listeners之类的库,则可以在<a>标记上分配“跟随”或“不跟随”类,然后根据CSS进行显示...希望这是有意义的。

There's nothing wrong with your previous outline of logic. 您先前的逻辑大纲没有错。

if($this->user_is_followed {
    //show unfollow button
}
else {      
    //show follow button
}
if($this->user_is_blocked) {    
    //show unblock button
}
else {
    //show block button
}

the only changes that will happen is the control of your follow,unfollow,block and unblock buttons. 唯一会发生的变化是对跟随,取消关注,阻止和取消阻止按钮的控制。 because there is no action that needs to take place in displaying the buttons right? 因为在正确显示按钮时不需要采取任何操作? so those 2 scenarios should take place after the button is clicked not when the button is displayed. 因此,这两种情况应在单击按钮后发生,而不是在显示按钮时发生。 I suggest create the functions for your follow,unfollo,block, and unblock buttons base on the scenario you want to happen. 我建议根据您想发生的情况为您的关注,取消关注,阻止和取消阻止按钮创建功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM