kevinkev 0 Newbie Poster

I have the following php code that queries my Database called get_cost.php.The query is working as it displays all content of my TABLE si, kindly assist!

<?php

/*
 * Following code will list all the products
 */

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '../config.php';

 $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        // Selecing database
 mysql_select_db(DB_DATABASE);

// get all products from products table
$result = mysql_query("SELECT * FROM si ", $con);

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["si"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $product = array();
        $product["code"] = $row["code"];
        $product["descr"] = $row["descr"];
        $product["categ"] = $row["categ"];
        $product["punit"] = $row["punit"];
        $product["price"] = $row["price"];

        // push single product into final response array
        array_push($response["si"], $product);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);
}
?>

And below is my Activity class, i want to be able to display cost on text view after onclicklistener on my button, below is my activity.

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;

import com.electra.theoffice.LoginActivity;

import com.electra.theoffice.UserFunctions;
import com.electra.theoffice.JSONParser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Categories extends Activity implements OnClickListener{
    UserFunctions userFunctions;
    Button beerButton,spiritsButton,foodButton,
           wineButton,rumButton,sodaButton,
           addButton,subtractButton,logoutButton,
           orderButton,backButton;
    TextView tableName,displayQuantity,displayProduct,displayCost,calculateCost;
    int cost,counter;
    String getTableName;
    ArrayList<HashMap<String, String>> productsList;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();
    // url to get all products list
    private static String url_all_products = "http://10.0.2.2/theOffice_login/get_cost.php";


    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "products";
    private static final String TAG_CODE = "code";
    private static final String TAG_PRICE = "price";

    // products JSONArray
    JSONArray products = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.categories);
        initialise();
        Bundle getBasket= getIntent().getExtras();
        getTableName= getBasket.getString("table");
        tableName.setText(getTableName);
        // Hashmap for ListView
        productsList = new ArrayList<HashMap<String, String>>();



    }

    private void initialise() {
        // TODO Auto-generated method stub
        cost=0;
        counter=0;
        beerButton=(Button)findViewById(R.id.buttonBeer);
        spiritsButton=(Button)findViewById(R.id.buttonSpirit);
        foodButton=(Button)findViewById(R.id.buttonFood);
        wineButton=(Button)findViewById(R.id.buttonWine);
        rumButton=(Button)findViewById(R.id.buttonRum);
        sodaButton=(Button)findViewById(R.id.buttonSoda);
        logoutButton=(Button)findViewById(R.id.buttonLogout);
        orderButton=(Button)findViewById(R.id.buttonSendOrder);
        tableName=(TextView)findViewById(R.id.textViewTableName);
        displayProduct=(TextView)findViewById(R.id.textViewProductSelected);
        displayQuantity=(TextView)findViewById(R.id.textViewQuantityAmount);
        displayCost=(TextView)findViewById(R.id.textViewDisplayCost);
        calculateCost=(TextView)findViewById(R.id.textViewCalculateCost);
        addButton=(Button)findViewById(R.id.buttonAddQuantity);
        subtractButton=(Button)findViewById(R.id.buttonSubtractQuantity);
        calculateCost.setOnClickListener(this);
        addButton.setOnClickListener(this);
        subtractButton.setOnClickListener(this);
        beerButton.setOnClickListener(this);
        spiritsButton.setOnClickListener(this);
        foodButton.setOnClickListener(this);
        wineButton.setOnClickListener(this);
        rumButton.setOnClickListener(this);
        sodaButton.setOnClickListener(this);
        logoutButton.setOnClickListener(this);
        orderButton.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.buttonBeer:
            displayProduct.setText("Tusker");
            break;
        case R.id.buttonFood:
            displayProduct.setText("Whitecap");
            break;
       case R.id.buttonSoda:
           displayProduct.setText("Guiness");
           break;
       case R.id.buttonWine:
           displayProduct.setText("Tusker Lager");
           break;
      case R.id.buttonSpirit:
          displayProduct.setText("Pilsner");
           break;
      case R.id.buttonRum:
          displayProduct.setText("Snapp");
          break;
      case R.id.buttonAddQuantity:
          counter++;
          displayQuantity.setText(""+counter);
          cost= counter*120;
          break;
      case R.id.buttonSubtractQuantity:
          counter--;
          displayQuantity.setText(""+counter);
          break;
      case R.id.textViewCalculateCost:
          cost= counter*120;
          displayCost.setText("" + cost);
          break;
      case R.id.buttonLogout:
         userFunctions = new UserFunctions();
         if(userFunctions.logoutUser(getApplicationContext())){
         Intent login = new Intent(getApplicationContext(), LoginActivity.class);
         login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         startActivity(login);
            finish();
         }else{
             // user is not logged in show login screen
            Intent login = new Intent(getApplicationContext(), LoginActivity.class);
            login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(login);
            // Closing dashboard screen
            finish();
         }
          break;
        }

    }

    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.