2

I hope this isn't a stupid question, I have been trawling the solutions and haven't been able to work this out..

In my main view controller I get data from a MySQL database using PHP and JSON. The user then selected relevant items from a table view by clicking on a custom button in a custom cell. I am at the point where I can add to/remove from the list based on which buttons are clicked/unclicked...

    private var selectedItems = [Int: String]()

    func cellButtonTapped(cell: CustomCell) {

    let indexPath = self.tableView.indexPathForRowAtPoint(cell.center)!
    let selectedItem = postsCollection[indexPath.row]
    let item = selectedItem.product
    let id = selectedItem.Id

    let idItem : [Int:String] = [id:"\(item)"]

        if selectedItems[id] == nil {
            // now val is not nil and the Optional has been unwrapped, so use it
            selectedItems[id] = "\(item)" //item
        } else {
            selectedItems[id] = nil
        }

   println("\(selectedItems)")

    }

If I select items on the list it is displayed like this as I select the items. When I deselect them, the get removed from the list.

[6: Mybulen, 7: Ecotrin XL, 5: Aspen Simvastatin, 8: Lorien]

I would like to create a JSON Array and POST the array to a PHP file that I have the updates the database.. The resultant JSON Array should look something like this...

[{"Id":"6","repeatflag":"1"},{"Id":"7","repeatflag":"1"},{"Id":"5","repeatflag":"1"}]

So basically, any item I select gets added to the array with a "repeatflag" set to "1". My PHP file has a script that updates the "repeatflag" in the MySQL database. When the user is ready, they will click on a send button that will POST their selection in the the JSON Array. Hope that makes sense?

Thanks.

1
  • You declare your selectedItems Object as [Int: String], but the way you describe it should look like is [String: Int] for me? Commented Aug 18, 2015 at 13:27

2 Answers 2

1

Well the code from you looks a little bit weird to me, maybe its because I don't know its context... But you can build an JSONArray you want like this:

class ViewController: UIViewController, UITableViewDelegate {

    var jsonArrayWithItemsToSendToPHP: [[String: AnyObject]] = [[String: AnyObject]]()

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let selectedItem = postsCollection[indexPath.row]

        var jsonObjectToPutToArray: [String: AnyObject] = [String: AnyObject]()
            jsonObjectToPutToArray["id"] = selectedItem.Id
            jsonObjectToPutToArray["name"] = selectedItem.product
            jsonObjectToPutToArray["repeatflag"] = 1

        jsonArrayWithItemsToSendToPHP.append(jsonObjectToPutToArray)
    }

}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Neo. The solution you gave worked to add it to the array. The reason for the if statement is to stay that if the item when selected is not in the array, then add it. When deselecting the item, if it is in the array, it should be removed. So ultimately, I will end up with just the items selected in the array regardless of how many times the items have been selected or unselected... I tried looking for a way to remove an item from an array but couldn't manage.. I am guessing it will have something to do with filter?
Here is a screenshot of the items with checked and unchecked selections... Item Selection
Sorry, one more thing - I know I am pushing it.. :-) This is the current result.. [[id: 7, repeatflag: 1, name: Ecotrin XL], [id: 8, repeatflag: 1, name: Lorien]] is there a way of changing the '[' to '{' for the listed items, so it would look like.. [{id: 7, repeatflag: 1, name: Ecotrin XL}, {id: 8, repeatflag: 1, name: Lorien}]
No it is right... Since Swift the NSLog of an JSONArray holding JSONObjects is that [[key: value], [key: value]]... It is confusing I know... But if you parse it on php side with json_decode($_POST["myPostKey"], true); it will be an index based array, holding key based arrays, in other words, a clean php json representation :-)
Thanks. Haven't tried to send it yet. Based on what you said, I'm sure it will be good... :-) Do you have any suggestions on how I can handles the select/unselect add/remove from the array?
0

class ViewController: UIViewController, UITableViewDelegate {

var jsonArrayWithItemsToSendToPHP: [[String: AnyObject]] = [[String: AnyObject]]()

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedItem = postsCollection[indexPath.row]

    var jsonObjectToPutToArray: [String: AnyObject] = [String: AnyObject]()
        jsonObjectToPutToArray["id"] = selectedItem.Id
        jsonObjectToPutToArray["name"] = selectedItem.product
        jsonObjectToPutToArray["repeatflag"] = 1

    jsonArrayWithItemsToSendToPHP.append(jsonObjectToPutToArray)
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.