在Post PHP之后返回结果

时间:2017-05-31 10:24:39

标签: php

我有一个循环3个文件的脚本。 每个文件都将数据发布到api并返回结果,在变量$ result中接受或拒绝。

在Accepted响应中,脚本停止运行并回显结果。 在拒绝时,回显出被拒绝的响应并继续下一个文件。

问题是:让我们说所有3个文件都给出了拒绝的响应,它回应了3个被拒绝的回复。

如果没有获得接受的响应,我怎能只回显一个通用响应。

 @IBAction func addBasket(_ sender: UIButton) {

    let prefs         = UserDefaults.standard
    let status        = prefs.string(forKey: "status")

    if(status != nil){

        if( status == "pending"){

            if(BasketItemList.count < 5 ) {

                self.addpost()

            } else {


                let alert = SCLAlertView()
                alert.showWarning("Basket", subTitle: "Upgrade to add more")

            }



        } else {

            if(BasketItemList.count < 50 ) {

                self.addpost()

            } else {

                let alert = SCLAlertView()
                alert.showWarning("Basket", subTitle: "Upgrade to add more")

            }



        }


    }



}


func addpost() {

    let appearance = SCLAlertView.SCLAppearance(
        showCloseButton: false
    )

    let alert = SCLAlertView(appearance : appearance)
    let txt = alert.addTextField("Enter name")
    alert.addButton("Save") {

        if txt.text?.characters.count != 0 {

            let basketname : String = txt.text!

            let userID = FIRAuth.auth()?.currentUser?.uid

            let postitem : [String :AnyObject] = ["userid" : userID! as AnyObject , "basketname" : basketname as AnyObject ]

            let dbref = FIRDatabase.database().reference()
            dbref.child("BasketList").childByAutoId().setValue(postitem)

            self.Basketdata2()



            let appearance = SCLAlertView.SCLAppearance(
                kDefaultShadowOpacity: 0,
                showCloseButton: false
            )

            let alertView = SCLAlertView(appearance: appearance)
            alertView.showTitle(
                "Saved", // Title of view
                subTitle: "", // String of view
                duration: 2.0, // Duration to show before closing automatically, default: 0.0
                completeText: "Done", // Optional button value, default: ""
                style: .success, // Styles - see below.
                colorStyle: 0xA429FF,
                colorTextButton: 0xFFFFFF
            )




        } else {


            let alert = SCLAlertView()
            alert.showError("Oops!", subTitle: "Basket name should not be empty")

            self.tableviewbasket.reloadData()

        }

    }
    alert.addButton("Cancel"){


    }
    alert.showEdit("Add basket", subTitle: "Please enter your basket name")




}

func Basketdata2() {
    HUD.show(.labeledProgress(title: "Loading...", subtitle: ""))

    let databaseref = FIRDatabase.database().reference()
    var userID = FIRAuth.auth()?.currentUser?.uid

    if userID == nil {

        userID = userfbid
    }

    databaseref.child("BasketList").queryOrdered(byChild: "userid").queryEqual(toValue: userID!).observeSingleEvent(of: .value, with: { (snapshot) in

        if snapshot.exists() {

            self.tableviewbasket.backgroundView = nil;
            HUD.hide()

        }  else {

            HUD.hide()


            self.tableviewbasket.setContentOffset(CGPoint(x : 0, y: -98), animated: true)


            if (self.BasketItemList.count == 0) {

                // tableView is empty. You can set a backgroundView for it.
                let label = UILabel(frame: CGRect(x: 5, y: 0, width: self.tableviewbasket.bounds.size.width, height:self.tableviewbasket.bounds.size.height))
                label.text = "The best preparation for tomorrow \n is doing your best today.\n Please create your first basket."
                label.textColor = UIColor.black;
                label.textAlignment = .center
                label.numberOfLines = 4
                label.sizeToFit()
                label.font = UIFont(name: "AvenirNext-Regular", size: 16.0)
                self.tableviewbasket.backgroundView = label;
                self.tableviewbasket.separatorStyle = .none;

            }

        }

    })
}


  func Basketdata() {

    HUD.show(.labeledProgress(title: "Please wait...", subtitle: ""))

    self.BasketItemList.removeAll()
    self.Basketid.removeAll()

    let databaseref = FIRDatabase.database().reference()
    let userID = FIRAuth.auth()?.currentUser?.uid

    databaseref.child("BasketList").queryOrdered(byChild: "userid").queryEqual(toValue: userID!).observeSingleEvent(of: .value, with: { (snapshot) in

        if snapshot.exists() {

            databaseref.child("BasketList").queryOrdered(byChild: "userid").queryEqual(toValue: userID!).observe(.childAdded, with: {
                (snapshot) in



                if let dictionary = snapshot.value as? [String : AnyObject] {


                    let basketitem = BasketList(text : "")
                    basketitem.setValuesForKeys(dictionary)

                    self.BasketItemList.append(basketitem)
                    self.Basketid.append(snapshot.key)
                    DispatchQueue.main.async {

                        if !self.BasketItemList.isEmpty {

                            HUD.hide()
                            self.tableviewbasket.reloadData()

                        }

                    }


                } else {

                    self.tableviewbasket.reloadData()
                    HUD.hide()
                }


            })




        }  else {

            if (self.BasketItemList.count == 0) {

                // tableView is empty. You can set a backgroundView for it.
                let label = UILabel(frame: CGRect(x: 5, y: 0, width: self.tableviewbasket.bounds.size.width, height:self.tableviewbasket.bounds.size.height))
                label.text = "The best preparation for tomorrow \nis doing your best today"
                label.textColor = UIColor.black;
                label.textAlignment = .center
                label.numberOfLines = 2
                label.sizeToFit()
                label.font = UIFont(name: "AvenirNext-Regular", size: 16.0)
                self.tableviewbasket.backgroundView = label;
                self.tableviewbasket.separatorStyle = .none;

            }


            HUD.hide()


        }

    })




}

2 个答案:

答案 0 :(得分:1)

试试这个,这是专门针对3拒绝回复

$failed = 0;
foreach($getSeq as $key){
    $fileName = $key->file;
    include_once 'Lenders/' . $fileName;

    if($result == 'Accepted'){

        echo 'Accepted';
        break;

    }
    if($result == 'Rejected'){
        $failed++;
    }


}

if($failed == 3) {
    echo "Rejected";
}

你没有说过,如果其他比率 (1A:2R,2A:1R) 会发生什么,所以我只是为3次拒绝创建了< / p>

答案 1 :(得分:1)

你可以使用array()完成它,如下所示:

<?php
    $resultArr = array(); // suppose we insert below Accepted = 1 and Rejected = 0
    foreach($getSeq as $key){

        $fileName = $key->file;
        include_once 'Lenders/' . $fileName;

        if($result == 'Accepted'){
            echo "Accepted";
           $resultArr[] = 1;
           break;

        }
        if($result == 'Rejected'){
            $resultArr[] = 0;
        }
    }

    if(!in_array(1, $resultArr)){
        echo 'Rejected';
    }