Author Topic: The best way of displaying result of a MySQL query into label/button  (Read 3724 times)

TicTac

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
What I'm trying to accomplish here is for a list of aircraft to be downloaded from a database and then for each record to be presented for the user to click. The part I am struggling with is separating each record so it can be used on it's own. All I have currently is two scripts, one php script that queries the database and returns the string of aircraft + attributes:

  1. <?php
  2.     // Configuration
  3.     $hostname = 'myhost';
  4.     $username = 'myusername';
  5.     $password = 'mypassword';
  6.     $database = 'mydatabase';
  7.  
  8.     try {
  9.         $dbh = new PDO('mysql:host='. $hostname .';dbname='. $database, $username, $password);
  10.     } catch(PDOException $e) {
  11.         echo '<h1>An error has occurred.</h1><pre>', $e->getMessage() ,'</pre>';
  12.     }
  13.  
  14.     $sth = $dbh->query('    SELECT * FROM aircraft ORDER BY cost DESC LIMIT 5');
  15.     $sth->setFetchMode(PDO::FETCH_ASSOC);
  16.  
  17.     $result = $sth->fetchAll();
  18.  
  19.     if(count($result) > 0) {
  20.         foreach($result as $r) {
  21.             echo $r['manufac'], " ", $r['model'], " ", $r['cost'], "\n";
  22.         }
  23.     }
  24. ?>
  25.  

And I also have a controller:

  1. public class aircraftDownloader : MonoBehaviour
  2. {
  3.     private string secretKey = "#############";
  4.     public string getAircraftURL = "http://cowcatcher.net/am/display.php";
  5.         public UILabel aircraftlabel;
  6.     void Start()
  7.     {
  8.         StartCoroutine(GetScores());
  9.     }
  10.  
  11.     // remember to use StartCoroutine when calling this function!
  12.     IEnumerator GetScores()
  13.     {
  14.         aircraftlabel.text = "Loading Aircraft Data";
  15.         WWW hs_get = new WWW(getAircraftURL);
  16.         yield return hs_get;
  17.  
  18.         if (hs_get.error != null)
  19.         {
  20.             print("There was an error getting the high score: " + hs_get.error);
  21.         }
  22.         else
  23.         {
  24.             aircraftlabel.text = hs_get.text;
  25.         }
  26.     }
  27.  
  28. }
  29.  

At the moment, all this does is display each record on a new line on a single label. I need some way of seperating each downloaded record so I can use them in separate NGUI buttons/elements. After that I was considering displaying each in a table type format, what's the best way of doing this?

Thanks!

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: The best way of displaying result of a MySQL query into label/button
« Reply #1 on: October 30, 2013, 06:19:30 PM »
It depends much on the design. You can use multiple labels or one label with multiple lines in it - in the end it really depends on how you want it on the screen.

TicTac

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: The best way of displaying result of a MySQL query into label/button
« Reply #2 on: October 30, 2013, 10:24:29 PM »
Right okay, the main problem I have at the moment I suppose then is separating the records from the sql query. I'll stick it on unity answers since that isn't really specific to NGUI!