Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.68 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Following code will delete a product from table
  5. * A product is identified by product id (pid)
  6. */
  7.  
  8. // array for JSON response
  9. $response = array();
  10.  
  11. // check for required fields
  12. if (isset($_POST['id'])) {
  13. $id = $_POST['id'];
  14.  
  15. // include db connect class
  16. require_once __DIR__ . '/config.inc.php';
  17.  
  18. // connecting to db
  19. $db = new DB_CONNECT();
  20.  
  21. // mysql update row with matched pid
  22. $result = mysql_query("DELETE FROM foodordered WHERE id = $id");
  23.  
  24. // check if row deleted or not
  25. if (mysql_affected_rows() > 0) {
  26. // successfully updated
  27. $response["success"] = 1;
  28. $response["message"] = "Product successfully deleted";
  29.  
  30. // echoing JSON response
  31. echo json_encode($response);
  32. } else {
  33. // no product found
  34. $response["success"] = 0;
  35. $response["message"] = "No product found";
  36.  
  37. // echo no users JSON
  38. echo json_encode($response);
  39. }
  40. } else {
  41. // required field is missing
  42. $response["success"] = 0;
  43. $response["message"] = "Required field(s) is missing";
  44.  
  45. // echoing JSON response
  46. echo json_encode($response);
  47. }
  48. ?>
  49.  
  50. package com.yiqiexample.cc;
  51.  
  52. import java.util.ArrayList;
  53. import java.util.HashMap;
  54. import java.util.List;
  55.  
  56. import org.apache.http.NameValuePair;
  57. import org.apache.http.message.BasicNameValuePair;
  58. import org.json.JSONArray;
  59. import org.json.JSONException;
  60. import org.json.JSONObject;
  61. import android.annotation.SuppressLint;
  62. import android.app.Activity;
  63. import android.app.AlertDialog;
  64. import android.app.ListActivity;
  65. import android.app.ProgressDialog;
  66. import android.content.DialogInterface;
  67. import android.content.Intent;
  68. import android.os.AsyncTask;
  69. import android.os.Bundle;
  70. import android.util.Log;
  71. import android.view.View;
  72. import android.view.View.OnClickListener;
  73. import android.widget.AdapterView;
  74. import android.widget.BaseAdapter;
  75. import android.widget.Button;
  76. import android.widget.CheckBox;
  77. import android.widget.EditText;
  78. import android.widget.ListAdapter;
  79. import android.widget.ListView;
  80. import android.widget.SimpleAdapter;
  81. //import android.widget.ImageView;
  82. import android.widget.TextView;
  83. import android.widget.Toast;
  84. import android.widget.AdapterView.OnItemClickListener;
  85.  
  86.  
  87.  
  88. public class Food extends ListActivity
  89. implements OnClickListener {
  90. // Progress Dialog
  91. private ProgressDialog pDialog;
  92. private ListAdapter adapter;
  93.  
  94. //testing on Emulator:
  95. private static final String READ_COMMENTS_URL = "http://10.0.2.2/pbda2/foodordered.php";
  96. private static final String url_delete_product = "http://10.0.2.2/pbda2/delete_product.php";
  97.  
  98. // my ip add 192.168.43.176
  99. //private CheckBox chkFood, chkDrinks, chkServices;
  100. //private Button btnDisplay, chkClear, deliever, chkClearFood, fooddeliever, drinksdeliever, servicesdeliever, chkClearDrinks, chkClearServices;
  101. //private TextView clearThis,orderdisplay, clearThisFood, foodorderdisplay, drinksorderdisplay, servicesorderdisplay, clearThisDrinks, clearThisServices;
  102.  
  103. private static final String TAG_SUCCESS = "success";
  104. private static final String TAG_POSTS = "posts";
  105. private static final String TAG_SEATNUMBER = "seatnumber";
  106. private static final String TAG_FOODORDERED = "foodordered";
  107. //it's important to note that the message is both in the parent branch of
  108. //our JSON tree that displays a "Post Available" or a "No Post Available" message,
  109. //and there is also a message for each individual post, listed under the "posts"
  110. //category, that displays what the user typed as their message.
  111.  
  112. //An array of all of our comments
  113. private JSONArray mComments = null;
  114. //manages all of our comments in a list.
  115. private ArrayList<HashMap<String, String>> mCommentList;
  116.  
  117.  
  118.  
  119. @Override
  120. public void onCreate(Bundle savedInstanceState) {
  121. super.onCreate(savedInstanceState);
  122. this.setContentView(R.layout.food);
  123.  
  124.  
  125.  
  126. View v = findViewById(R.id.backmain);
  127. //set event listener
  128. v.setOnClickListener(this);
  129.  
  130. View z = findViewById(R.id.drinksbtn);
  131. //set event listener
  132. z.setOnClickListener(this);
  133.  
  134. View x = findViewById(R.id.servicebtn);
  135. //set event listener
  136. x.setOnClickListener(this);
  137.  
  138.  
  139.  
  140.  
  141. }
  142.  
  143. @Override
  144. protected void onResume() {
  145. // TODO Auto-generated method stub
  146. super.onResume();
  147. //loading the comments via AsyncTask
  148. new LoadComments().execute();
  149. }
  150.  
  151.  
  152. /**
  153. * Retrieves recent post data from the server.
  154. */
  155. public void updateJSONdata() {
  156.  
  157. // Instantiate the arraylist to contain all the JSON data.
  158. // we are going to use a bunch of key-value pairs, referring
  159. // to the json element name, and the content, for example,
  160. // message it the tag, and "I'm awesome" as the content..
  161.  
  162. mCommentList = new ArrayList<HashMap<String, String>>();
  163.  
  164. // Bro, it's time to power up the J parser
  165. JSONParser jParser = new JSONParser();
  166. // Feed the beast our comments url, and it spits us
  167. //back a JSON object. Boo-yeah Jerome.
  168. JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
  169.  
  170. //when parsing JSON stuff, we should probably
  171. //try to catch any exceptions:
  172. try {
  173.  
  174. //I know I said we would check if "Posts were Avail." (success==1)
  175. //before we tried to read the individual posts, but I lied...
  176. //mComments will tell us how many "posts" or comments are
  177. //available
  178. mComments = json.getJSONArray(TAG_POSTS);
  179.  
  180. // looping through all posts according to the json object returned
  181. for (int i = 0; i < mComments.length(); i++) {
  182. JSONObject c = mComments.getJSONObject(i);
  183.  
  184. //gets the content of each tag
  185. String seatnumber = c.getString(TAG_SEATNUMBER);
  186. String foodordered = c.getString(TAG_FOODORDERED);
  187.  
  188.  
  189.  
  190. // creating new HashMap
  191. HashMap<String, String> map = new HashMap<String, String>();
  192.  
  193. map.put(TAG_SEATNUMBER, seatnumber);
  194. map.put(TAG_FOODORDERED, foodordered);
  195.  
  196.  
  197. // adding HashList to ArrayList
  198. mCommentList.add(map);
  199.  
  200. //annndddd, our JSON data is up to date same with our array list
  201. }
  202.  
  203. } catch (JSONException e) {
  204. e.printStackTrace();
  205. }
  206. }
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. @Override
  217. public void onClick(View arg0) {
  218. if(arg0.getId() == R.id.backmain){
  219. //define a new Intent for the second Activity
  220. Intent intent = new Intent(this,MainActivity.class);
  221.  
  222. //start the second Activity
  223. this.startActivity(intent);
  224. }
  225. if(arg0.getId() == R.id.drinksbtn){
  226. //define a new Intent for the second Activity
  227. Intent intent = new Intent(this,Drinks.class);
  228.  
  229. //start the second Activity
  230. this.startActivity(intent);
  231. }
  232. if(arg0.getId() == R.id.servicebtn){
  233. //define a new Intent for the second Activity
  234. Intent intent = new Intent(this,Services.class);
  235.  
  236. //start the second Activity
  237. this.startActivity(intent);
  238. }
  239.  
  240.  
  241.  
  242. }
  243.  
  244. /**
  245. * Inserts the parsed data into the listview.
  246. */
  247. private void updateList() {
  248. // For a ListActivity we need to set the List Adapter, and in order to do
  249. //that, we need to create a ListAdapter. This SimpleAdapter,
  250. //will utilize our updated Hashmapped ArrayList,
  251. //use our single_post xml template for each item in our list,
  252. //and place the appropriate info from the list to the
  253. //correct GUI id. Order is important here.
  254.  
  255. //ListAdapter
  256. adapter = new SimpleAdapter(this, mCommentList,
  257. R.layout.single_comment,
  258. new String[] { TAG_SEATNUMBER, TAG_FOODORDERED
  259. //TAG_DRINKSORDERED, TAG_SERVICES
  260. }, new int[] { R.id.seatnumber, R.id.orders
  261. //R.id.drinkstv, R.id.servicestv,
  262. //adapter = new SimpleAdapter(this, mCommentList,
  263. //R.layout.single_comment, new String[] { TAG_SEATNUMBER, TAG_FOODORDERED
  264. //TAG_DRINKSORDERED, TAG_SERVICES
  265. // }, new int[] { R.id.seatnumber, R.id.orders
  266. //R.id.drinkstv, R.id.servicestv,
  267.  
  268.  
  269. });
  270.  
  271. setListAdapter(adapter);
  272. ListView lv = getListView();
  273. lv.setOnItemClickListener(new OnItemClickListener() {
  274.  
  275. @SuppressLint("NewApi")
  276. @Override
  277. public void onItemClick(AdapterView<?> arg0, View arg1,
  278. final int arg2, long arg3) {
  279. // TODO Auto-generated method stub
  280.  
  281. AlertDialog.Builder alt = new AlertDialog.Builder(
  282. Food.this,
  283. android.R.style.Theme_DeviceDefault_Dialog);
  284. alt.setMessage("Order delivered??");
  285. alt.setPositiveButton("Yes",
  286. new DialogInterface.OnClickListener() {
  287.  
  288. @Override
  289. public void onClick(DialogInterface dialog,
  290. int which) {
  291. // TODO Auto-generated method stub
  292. mCommentList.remove(arg2);
  293.  
  294. ((BaseAdapter) adapter).notifyDataSetChanged();
  295.  
  296.  
  297. //new
  298. new DeleteProduct().execute();
  299.  
  300. //end new
  301.  
  302. }
  303. });
  304.  
  305. alt.setNegativeButton("No",
  306. new DialogInterface.OnClickListener() {
  307.  
  308. @Override
  309. public void onClick(DialogInterface dialog,
  310. int which) {
  311. // TODO Auto-generated method stub
  312.  
  313. dialog.dismiss();
  314.  
  315. }
  316. });
  317. AlertDialog dialog = alt.create();
  318. dialog.show();
  319.  
  320. return;
  321. }
  322. });
  323. }
  324.  
  325.  
  326. // I shouldn't have to comment on this one:
  327. //etListAdapter(adapter);
  328.  
  329. // Optional: when the user clicks a list item we
  330. //could do something. However, we will choose
  331. //to do nothing...
  332. //ListView lv = getListView();
  333. //lv.setOnItemClickListener(new OnItemClickListener() {
  334.  
  335. //@Override
  336. //public void onItemClick(AdapterView<?> parent, View view,
  337. // int position, long id) {
  338.  
  339. // This method is triggered if an item is click within our
  340. // list. For our example we won't be using this, but
  341. // it is useful to know in real life applications.
  342.  
  343.  
  344.  
  345.  
  346.  
  347. //}
  348. //});
  349.  
  350.  
  351.  
  352. // }
  353.  
  354.  
  355.  
  356.  
  357. public class LoadComments extends AsyncTask<Void, Void, Boolean> {
  358.  
  359. @Override
  360. protected void onPreExecute() {
  361. super.onPreExecute();
  362. pDialog = new ProgressDialog(Food.this);
  363. pDialog.setMessage("Loading orders...");
  364. pDialog.setIndeterminate(false);
  365. pDialog.setCancelable(true);
  366. pDialog.show();
  367. }
  368. @Override
  369. protected Boolean doInBackground(Void... arg0) {
  370. //we will develop this method in version 2
  371. updateJSONdata();
  372. return null;
  373.  
  374. }
  375.  
  376.  
  377. @Override
  378. protected void onPostExecute(Boolean result) {
  379. super.onPostExecute(result);
  380. pDialog.dismiss();
  381. //we will develop this method in version 2
  382. updateList();
  383. }
  384. }
  385.  
  386.  
  387.  
  388. //new
  389.  
  390. /*****************************************************************
  391. * Background Async Task to Delete Product
  392. * */
  393. class DeleteProduct extends AsyncTask<String, String, String> {
  394.  
  395. /**
  396. * Before starting background thread Show Progress Dialog
  397. * */
  398. @Override
  399. protected void onPreExecute() {
  400. super.onPreExecute();
  401. pDialog = new ProgressDialog(Food.this);
  402. pDialog.setMessage("Deleting Product...");
  403. pDialog.setIndeterminate(false);
  404. pDialog.setCancelable(true);
  405. pDialog.show();
  406. }
  407.  
  408. /**
  409. * Deleting product
  410. * */
  411. protected String doInBackground(String... args) {
  412.  
  413. // Check for success tag
  414. int success;
  415. try {
  416. // Building Parameters
  417. //List<NameValuePair> params = new ArrayList<NameValuePair>();
  418. //params.add(new BasicNameValuePair("pid", pid));
  419.  
  420. // getting product details by making HTTP request
  421. //JSONObject json = jsonParser.makeHttpRequest(
  422. // url_delete_product, "POST", params);
  423.  
  424. JSONParser jParser = new JSONParser();
  425. // Feed the beast our comments url, and it spits us
  426. //back a JSON object. Boo-yeah Jerome.
  427. JSONObject json = jParser.getJSONFromUrl(url_delete_product);
  428.  
  429. // check your log for json response
  430. Log.d("Delete Product", json.toString());
  431.  
  432. // json success tag
  433. success = json.getInt(TAG_SUCCESS);
  434. if (success == 1) {
  435. // product successfully deleted
  436. // notify previous activity by sending code 100
  437. Intent i = getIntent();
  438. // send result code 100 to notify about product deletion
  439. setResult(100, i);
  440. finish();
  441. }
  442. } catch (JSONException e) {
  443. e.printStackTrace();
  444. }
  445.  
  446. return null;
  447. }
  448.  
  449. /**
  450. * After completing background task Dismiss the progress dialog
  451. * **/
  452. protected void onPostExecute(String file_url) {
  453. // dismiss the dialog once product deleted
  454. pDialog.dismiss();
  455.  
  456. }
  457.  
  458. }
  459.  
  460.  
  461.  
  462.  
  463.  
  464. }
  465.  
  466. <?php
  467.  
  468. /*
  469. Our "config.inc.php" file connects to database every time we include or require
  470. it within a php script. Since we want this script to add a new user to our db,
  471. we will be talking with our database, and therefore,
  472. let's require the connection to happen:
  473. */
  474. require("config.inc.php");
  475.  
  476. //initial query
  477. $query = "Select * FROM foodordered";
  478.  
  479. //execute query
  480. try {
  481. $stmt = $db->prepare($query);
  482. $result = $stmt->execute();
  483. }
  484. catch (PDOException $ex) {
  485. $response["success"] = 0;
  486. $response["message"] = "Database Error!";
  487. die(json_encode($response));
  488. }
  489.  
  490. // Finally, we can retrieve all of the found rows into an array using fetchAll
  491. $rows = $stmt->fetchAll();
  492.  
  493.  
  494. if ($rows) {
  495. $response["success"] = 1;
  496. $response["message"] = "Post Available!";
  497. $response["posts"] = array();
  498.  
  499. foreach ($rows as $row) {
  500. $post = array();
  501. $post["seatnumber"] = $row["seatnumber"];
  502. $post["foodordered"] = $row["foodordered"];
  503. //$post["drinksordered"] = $row["drinksordered"];
  504. //$post["services"] = $row["services"];
  505.  
  506.  
  507.  
  508. //update our repsonse JSON data
  509. array_push($response["posts"], $post);
  510. }
  511.  
  512. // echoing JSON response
  513. echo json_encode($response);
  514.  
  515.  
  516. } else {
  517. $response["success"] = 0;
  518. $response["message"] = "No Post Available!";
  519. die(json_encode($response));
  520. }
  521.  
  522. ?>
  523.  
  524. JSONObject json = jParser.getJSONFromUrl(url_delete_product);
  525.  
  526. JSONObject json = jParser.getJSONFromUrl(
  527. url_delete_product+"?id="+idOfYourFoodItem);
  528.  
  529. $id = $_GET['id'];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement