I want my UISlider value to be sent to mysql database when button is clicked

81 views Asked by At

I want to be able to connect my swift project to my database (hosted on blue host using phpmyadmin) and when the button is clicked, I want the value on the slider or label (since they are connected) to be inputed into a table in my database. I know this is a two part question but I am having a hard time finding a helpful resource for:

  1. connecting swift 3 to my database using php and phpmyadmin (I can't find a solid tutorial that explains where to put files and how to access them)

and

  1. how to send that value to the database.

I will be adding more to this but to break some ground on my project i really need a solid start with the connection and figuring out how to send data and retrieve it with swift and the database.

I really appreciate any help on this subject or even links to any resources that may help me. I am new to programming so all this is foreign to me.

// So far this is my code in [view controller.swift]

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var lbl: UILabel!

    @IBAction func slider(_ sender: UISlider)
    {
        lbl.text = String(Int(sender.value))
    }

    func postToServerFunction() {
        print(lbl.text!)
    }

    @IBAction func postToServerButton(_ sender: UIButton) {
        postToServerFunction()
    }


    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
}

This is what my storyboard looks like

1

There are 1 answers

0
Justin Lennox On

This is kind of a big subject so it would be hard to answer it in one fell swoop but seeing as nobody else has tried, let me try to help you out a little here.

I see you already have a function 'postToServerButton', which I'm assuming is connected to the 'Button' button in your storyboard. This is calling the postToServerFunction(), which I don't see written. I'll start out by writing that. You'll also need to connect the slider as an IBOutlet to your code to get its value. I'm writing this code from memory so some of it is probably wrong but hopefully you'll get the idea

@IBOutlet weak var slider: UISlider! // Connect your slider to this
let constantValue = 8 // Don't change this

func postToServerFunction() {
    var sliderValue = Int(self.slider.value) // The slider value as an Int
    var sliderString = String(sliderValue) // The slider value as a String
    // The line below is to help you check that you're getting the right values from the slider
    print("\(self.constantValue)==D, Slider value: \(sliderValue), Slider String: \(sliderString)")

    postValueToServer(sliderValue) // We'll write this below, this is where the post to server part is happening
}

So this next function is where things get complicated. This is connecting your code to a database. I've written some basic code below but this is a complex topic that depends on a variety of variables.

func postValueToServer(sliderValue : Int) {
    print("\(self.constantValue)==D, Posting slider value \(sliderValue) to database") // Again just another check to make sure we're posting the right value
    var url: NSURL = NSURL(string: "http://INSERTYOURPHPURL.php")!
    var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
    request.HTTPMethod = "POST"
    request.HTTPBody = sliderValue.dataUsingEncoding(NSUTF8StringEncoding);
    NSURLConnection.sendAsynchronousRequest(request, queue:       NSOperationQueue.mainQueue())
        {
            (response, data, error) in
            print(response)

        }   
}

Check out this site for a good guide: http://www.ios-blog.co.uk/tutorials/swift/swift-how-to-send-a-post-request-to-a-php-script/