Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Following code will delete a product from table
- * A product is identified by product id (pid)
- */
- // array for JSON response
- $response = array();
- // check for required fields
- if (isset($_POST['id'])) {
- $id = $_POST['id'];
- // include db connect class
- require_once __DIR__ . '/config.inc.php';
- // connecting to db
- $db = new DB_CONNECT();
- // mysql update row with matched pid
- $result = mysql_query("DELETE FROM foodordered WHERE id = $id");
- // check if row deleted or not
- if (mysql_affected_rows() > 0) {
- // successfully updated
- $response["success"] = 1;
- $response["message"] = "Product successfully deleted";
- // echoing JSON response
- echo json_encode($response);
- } else {
- // no product found
- $response["success"] = 0;
- $response["message"] = "No product found";
- // echo no users JSON
- echo json_encode($response);
- }
- } else {
- // required field is missing
- $response["success"] = 0;
- $response["message"] = "Required field(s) is missing";
- // echoing JSON response
- echo json_encode($response);
- }
- ?>
- package com.yiqiexample.cc;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ListActivity;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- //import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.AdapterView.OnItemClickListener;
- public class Food extends ListActivity
- implements OnClickListener {
- // Progress Dialog
- private ProgressDialog pDialog;
- private ListAdapter adapter;
- //testing on Emulator:
- private static final String READ_COMMENTS_URL = "http://10.0.2.2/pbda2/foodordered.php";
- private static final String url_delete_product = "http://10.0.2.2/pbda2/delete_product.php";
- // my ip add 192.168.43.176
- //private CheckBox chkFood, chkDrinks, chkServices;
- //private Button btnDisplay, chkClear, deliever, chkClearFood, fooddeliever, drinksdeliever, servicesdeliever, chkClearDrinks, chkClearServices;
- //private TextView clearThis,orderdisplay, clearThisFood, foodorderdisplay, drinksorderdisplay, servicesorderdisplay, clearThisDrinks, clearThisServices;
- private static final String TAG_SUCCESS = "success";
- private static final String TAG_POSTS = "posts";
- private static final String TAG_SEATNUMBER = "seatnumber";
- private static final String TAG_FOODORDERED = "foodordered";
- //it's important to note that the message is both in the parent branch of
- //our JSON tree that displays a "Post Available" or a "No Post Available" message,
- //and there is also a message for each individual post, listed under the "posts"
- //category, that displays what the user typed as their message.
- //An array of all of our comments
- private JSONArray mComments = null;
- //manages all of our comments in a list.
- private ArrayList<HashMap<String, String>> mCommentList;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.food);
- View v = findViewById(R.id.backmain);
- //set event listener
- v.setOnClickListener(this);
- View z = findViewById(R.id.drinksbtn);
- //set event listener
- z.setOnClickListener(this);
- View x = findViewById(R.id.servicebtn);
- //set event listener
- x.setOnClickListener(this);
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- //loading the comments via AsyncTask
- new LoadComments().execute();
- }
- /**
- * Retrieves recent post data from the server.
- */
- public void updateJSONdata() {
- // Instantiate the arraylist to contain all the JSON data.
- // we are going to use a bunch of key-value pairs, referring
- // to the json element name, and the content, for example,
- // message it the tag, and "I'm awesome" as the content..
- mCommentList = new ArrayList<HashMap<String, String>>();
- // Bro, it's time to power up the J parser
- JSONParser jParser = new JSONParser();
- // Feed the beast our comments url, and it spits us
- //back a JSON object. Boo-yeah Jerome.
- JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
- //when parsing JSON stuff, we should probably
- //try to catch any exceptions:
- try {
- //I know I said we would check if "Posts were Avail." (success==1)
- //before we tried to read the individual posts, but I lied...
- //mComments will tell us how many "posts" or comments are
- //available
- mComments = json.getJSONArray(TAG_POSTS);
- // looping through all posts according to the json object returned
- for (int i = 0; i < mComments.length(); i++) {
- JSONObject c = mComments.getJSONObject(i);
- //gets the content of each tag
- String seatnumber = c.getString(TAG_SEATNUMBER);
- String foodordered = c.getString(TAG_FOODORDERED);
- // creating new HashMap
- HashMap<String, String> map = new HashMap<String, String>();
- map.put(TAG_SEATNUMBER, seatnumber);
- map.put(TAG_FOODORDERED, foodordered);
- // adding HashList to ArrayList
- mCommentList.add(map);
- //annndddd, our JSON data is up to date same with our array list
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onClick(View arg0) {
- if(arg0.getId() == R.id.backmain){
- //define a new Intent for the second Activity
- Intent intent = new Intent(this,MainActivity.class);
- //start the second Activity
- this.startActivity(intent);
- }
- if(arg0.getId() == R.id.drinksbtn){
- //define a new Intent for the second Activity
- Intent intent = new Intent(this,Drinks.class);
- //start the second Activity
- this.startActivity(intent);
- }
- if(arg0.getId() == R.id.servicebtn){
- //define a new Intent for the second Activity
- Intent intent = new Intent(this,Services.class);
- //start the second Activity
- this.startActivity(intent);
- }
- }
- /**
- * Inserts the parsed data into the listview.
- */
- private void updateList() {
- // For a ListActivity we need to set the List Adapter, and in order to do
- //that, we need to create a ListAdapter. This SimpleAdapter,
- //will utilize our updated Hashmapped ArrayList,
- //use our single_post xml template for each item in our list,
- //and place the appropriate info from the list to the
- //correct GUI id. Order is important here.
- //ListAdapter
- adapter = new SimpleAdapter(this, mCommentList,
- R.layout.single_comment,
- new String[] { TAG_SEATNUMBER, TAG_FOODORDERED
- //TAG_DRINKSORDERED, TAG_SERVICES
- }, new int[] { R.id.seatnumber, R.id.orders
- //R.id.drinkstv, R.id.servicestv,
- //adapter = new SimpleAdapter(this, mCommentList,
- //R.layout.single_comment, new String[] { TAG_SEATNUMBER, TAG_FOODORDERED
- //TAG_DRINKSORDERED, TAG_SERVICES
- // }, new int[] { R.id.seatnumber, R.id.orders
- //R.id.drinkstv, R.id.servicestv,
- });
- setListAdapter(adapter);
- ListView lv = getListView();
- lv.setOnItemClickListener(new OnItemClickListener() {
- @SuppressLint("NewApi")
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1,
- final int arg2, long arg3) {
- // TODO Auto-generated method stub
- AlertDialog.Builder alt = new AlertDialog.Builder(
- Food.this,
- android.R.style.Theme_DeviceDefault_Dialog);
- alt.setMessage("Order delivered??");
- alt.setPositiveButton("Yes",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- // TODO Auto-generated method stub
- mCommentList.remove(arg2);
- ((BaseAdapter) adapter).notifyDataSetChanged();
- //new
- new DeleteProduct().execute();
- //end new
- }
- });
- alt.setNegativeButton("No",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- // TODO Auto-generated method stub
- dialog.dismiss();
- }
- });
- AlertDialog dialog = alt.create();
- dialog.show();
- return;
- }
- });
- }
- // I shouldn't have to comment on this one:
- //etListAdapter(adapter);
- // Optional: when the user clicks a list item we
- //could do something. However, we will choose
- //to do nothing...
- //ListView lv = getListView();
- //lv.setOnItemClickListener(new OnItemClickListener() {
- //@Override
- //public void onItemClick(AdapterView<?> parent, View view,
- // int position, long id) {
- // This method is triggered if an item is click within our
- // list. For our example we won't be using this, but
- // it is useful to know in real life applications.
- //}
- //});
- // }
- public class LoadComments extends AsyncTask<Void, Void, Boolean> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(Food.this);
- pDialog.setMessage("Loading orders...");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(true);
- pDialog.show();
- }
- @Override
- protected Boolean doInBackground(Void... arg0) {
- //we will develop this method in version 2
- updateJSONdata();
- return null;
- }
- @Override
- protected void onPostExecute(Boolean result) {
- super.onPostExecute(result);
- pDialog.dismiss();
- //we will develop this method in version 2
- updateList();
- }
- }
- //new
- /*****************************************************************
- * Background Async Task to Delete Product
- * */
- class DeleteProduct extends AsyncTask<String, String, String> {
- /**
- * Before starting background thread Show Progress Dialog
- * */
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(Food.this);
- pDialog.setMessage("Deleting Product...");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(true);
- pDialog.show();
- }
- /**
- * Deleting product
- * */
- protected String doInBackground(String... args) {
- // Check for success tag
- int success;
- try {
- // Building Parameters
- //List<NameValuePair> params = new ArrayList<NameValuePair>();
- //params.add(new BasicNameValuePair("pid", pid));
- // getting product details by making HTTP request
- //JSONObject json = jsonParser.makeHttpRequest(
- // url_delete_product, "POST", params);
- JSONParser jParser = new JSONParser();
- // Feed the beast our comments url, and it spits us
- //back a JSON object. Boo-yeah Jerome.
- JSONObject json = jParser.getJSONFromUrl(url_delete_product);
- // check your log for json response
- Log.d("Delete Product", json.toString());
- // json success tag
- success = json.getInt(TAG_SUCCESS);
- if (success == 1) {
- // product successfully deleted
- // notify previous activity by sending code 100
- Intent i = getIntent();
- // send result code 100 to notify about product deletion
- setResult(100, i);
- finish();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * After completing background task Dismiss the progress dialog
- * **/
- protected void onPostExecute(String file_url) {
- // dismiss the dialog once product deleted
- pDialog.dismiss();
- }
- }
- }
- <?php
- /*
- Our "config.inc.php" file connects to database every time we include or require
- it within a php script. Since we want this script to add a new user to our db,
- we will be talking with our database, and therefore,
- let's require the connection to happen:
- */
- require("config.inc.php");
- //initial query
- $query = "Select * FROM foodordered";
- //execute query
- try {
- $stmt = $db->prepare($query);
- $result = $stmt->execute();
- }
- catch (PDOException $ex) {
- $response["success"] = 0;
- $response["message"] = "Database Error!";
- die(json_encode($response));
- }
- // Finally, we can retrieve all of the found rows into an array using fetchAll
- $rows = $stmt->fetchAll();
- if ($rows) {
- $response["success"] = 1;
- $response["message"] = "Post Available!";
- $response["posts"] = array();
- foreach ($rows as $row) {
- $post = array();
- $post["seatnumber"] = $row["seatnumber"];
- $post["foodordered"] = $row["foodordered"];
- //$post["drinksordered"] = $row["drinksordered"];
- //$post["services"] = $row["services"];
- //update our repsonse JSON data
- array_push($response["posts"], $post);
- }
- // echoing JSON response
- echo json_encode($response);
- } else {
- $response["success"] = 0;
- $response["message"] = "No Post Available!";
- die(json_encode($response));
- }
- ?>
- JSONObject json = jParser.getJSONFromUrl(url_delete_product);
- JSONObject json = jParser.getJSONFromUrl(
- url_delete_product+"?id="+idOfYourFoodItem);
- $id = $_GET['id'];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement