So we have a comment system in our application, and now we want to be able to add replies to specific comments, like a Facebook wall.
Our Java method for posting a comment:
class PostComment extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AddCommentActivity.this);
pDialog.setMessage("Posting Comment...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String post_title = title.getText().toString();
String post_message = message.getText().toString();
//We need to change this:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(AddCommentActivity.this);
String post_username = sp.getString("username", "anon");
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", post_username));
params.add(new BasicNameValuePair("title", post_title));
params.add(new BasicNameValuePair("message", post_message));
Log.d("request!", "starting");
//Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(
POST_COMMENT_URL, "POST", params);
// full json response
Log.d("Post Comment attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Comment Added!", json.toString());
finish();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Comment Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(AddCommentActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
and our PHP to send to MySQL:
<?php
//load and connect to MySQL database stuff
require("config.inc.php");
$query_params=null;
if (!empty($_POST)) {
//initial query
$query = "INSERT INTO comments ( username, title, message ) VALUES ( :user, :title, :message ) ";
//Update query
$query_params = array(
':user' => $_POST['username'],
':title' => $_POST['title'],
':message' => $_POST['message']
);
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error. Couldn't add post!";
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "Post Successfully Added!";
//echo json_encode($response);
} else {
?>
I've made a new table in MySQL called replies which has:
reply_id (INT AI)
reply_content (text)
reply_postid (INT)
reply_byusername (varchar)
I'm guessing somehow we make an arraylist
that picks the commentid
from the original comment and we can chain that to replies, but I don't know how. I generally have no idea how to proceed from here.