Quantcast
Viewing all articles
Browse latest Browse all 8068

Parse Data From PHP & MYSQL In Appcelerator App

Hi I'm new to Titanium Appcelerator and need some assistance with my app that will run on both Android and iPhone. I am trying to display two fields in a single view without a table. Can someone please help me write the piece of code I'm missing on my Titanium page? I am eternally grateful :) <br/>

This is my php code which outputs: [{&quot;value&quot;:&quot;10%&quot;,&quot;expiration&quot;:&quot;03/31/14&quot;}]

<?php
        class Coupon {
            public $value;
            public $expiration;
 
 
        public function setValue($value) {
            $this->value = $value;
        }
        public function setExpiration($expiration) {
            $this->expiration = $expiration;
        }
        }
 
    header('content-type: application/json; charset=utf-8');
 
    require 'config.php';
 
        $db_server = mysql_connect($db_hostname, $db_username, $db_password);
    if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
    mysql_select_db($db_database) or die("Unable to select database: " . mysql_error());
 
    $query = "SELECT * FROM discount_coupon";
    mysql_query("SET NAMES utf8");
    $result = mysql_query($query);
    if (!$result) die ("Database access failed: " . mysql_error());
 
    $rows = mysql_num_rows($result);
 
    for ($j = 0 ; $j < $rows ; ++$j)
    {
        $row = mysql_fetch_row($result);
        $coupon[$j] = new Coupon;
        $coupon[$j]->setValue($row[3]);
        $coupon[$j]->setExpiration($row[4]);
    }
 
    $json_string = json_encode($coupon); 
    echo $json_string; 
 
    ?>
and here is my Titanium page:
//Application Window Component Constructor
    function Application_Appoinment() {
        //load component dependencies
 
        var employ_listing = Titanium.Network.createHTTPClient();
        var tbl_data = [];
        var array;
 
        //create component instance
        var self = Ti.UI.createWindow({
            navBarHidden : true,
            backgroundImage : "/coupon.png",
 
        });
 
        var win = Ti.UI.createView({
            width : Ti.UI.FILL,
            height : Ti.UI.SIZE,
            top : 0,
            title : L('header_title'),
        });
 
        var topbar = Ti.UI.createView({
            width : Ti.UI.FILL,
            height : 50,
            top : 0,
            title : L('header_title'),
            backgroundImage : Ti.Filesystem.resourcesDirectory + "image/topbar.png",
        });
 
        var back_btn = Ti.UI.createButton({
            width : 60,
            height : 30,
            title : "Back",
            backgroundImage : Ti.Filesystem.resourcesDirectory + "image/button_image.png",
            left : 5,
            top : 10,
            font : {
                fontSize : 12
            },
 
        });
    var home_btn = Ti.UI.createButton({
            width : 60,
            height : 30,
            title : "Home",
            backgroundImage : Ti.Filesystem.resourcesDirectory + "image/button_image.png",
            right : 5,
            top : 10,
            font : {
                fontSize : 12
            },
 
        });
        back_btn.addEventListener('click', function() {
            self.close();
 
        });
 
 
        home_btn.addEventListener('click', function(e) {
 
            var Window;
 
 
            Window = require('ui/handheld/ApplicationWindow');
 
        new Window().open();
 
        });
        var header_title = Ti.UI.createLabel({
            color : 'white',
            font : {
                fontSize : 22
            },
            text : 'Coupon',
            textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
            top : 10,
            width : Ti.UI.SIZE,
            height : Ti.UI.SIZE
        });
 
        //topbar.add(home_btn);
        topbar.add(header_title);
        topbar.add(back_btn);
        win.add(topbar);
 
        var content_view = Ti.UI.createView({
            width : Ti.UI.FILL,
            height : '100%',
            top : 50,
 
        });
 
        self.add(win);
        //self.add(content_view);
        return self;
    }
 
        var content_view = Ti.UI.createView({
            width : Ti.UI.FILL,
            height : '100%',
            top : 50,
 
        });
Here is some code from a template how can I modify this to meet my needs (please keep in mind I don't want to use a table. I just want to display the two fields I need in a regular view:
var content_view = Ti.UI.createView({
            width : Ti.UI.FILL,
            height : '100%',
            top : 50,
 
        });
        var table = Titanium.UI.createTableView({
 
            backgroundImage : "/bg.png",
        });
 
        employ_listing.open("GET", 'http://www.url.com/content.php');
        employ_listing.send();
        console.log("new");
        employ_listing.onload = function() {
 
            var res = JSON.parse(this.responseText);
            Ti.API.info('res@loginReq.onload:\n' + this.responseText)
            var LOGGED = Ti.App.Properties.getString('logged');
            array=res;
            for (var i = 0; i < res.length; i++) {
                var row = Ti.UI.createTableViewRow({
                    width : Ti.UI.FILL,
                    height : 40,
                    backgroundDisabledColor : true,
                    backgroundColor : "#8a8a8a",
                });
                var header_txt = Ti.UI.createLabel({
                    top : 5,
                    left : 10,
                    text : res[i].person,
                    font : {
                        fontSize : 14
                    },
                    color : 'black'
                });
                var detail_text = Ti.UI.createLabel({
                    top : 20,
                    left : 10,
                    text : res[i].headline,
                    font : {
                        fontSize : 12
                    },
                    color : 'black'
                });
                var image = Ti.UI.createImageView({
                    image : Ti.Filesystem.resourcesDirectory + "image/arrow.png",
                    right : 10
 
                });
                row.add(header_txt);
                row.add(detail_text);
                row.add(image);
 
                tbl_data.push(row);
            }
            // alternatively, you could do
            table.setData(tbl_data);
            activityIndicator.hide();
            content_view.add(table);
        }   
 
        employ_listing.onerror = function(e) {
            //Ti.App.fireEvent('server_error_msg');
            var alertDL = Ti.UI.createAlertDialog({
                title : L('error'),
                message : L('server_error_msg'),
                buttonNames : ['OK']
            });
            alertDL.show();
        }

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>