0

I am currenrly running a PHP script which returns data from my database.

Below is my php script with some information commented out. As you can see i am hardcoding the dietid to = 1. I would like to pass into the script the dietid from my activity when the script runs. All my code works OK and the data is displayed in my listview, how do i pass the dietid?

Thanks in advance

Script to get data from db updated withdietid

$con = mysqli_connect($host, $user, $pwd, $db);


 if(mysqli_connect_errno($con)) {
 die("Failed to connect to MySQL: " . mysqli_connect_error());
  } 


   $dietid = array( $_GET['dietid'] );
   $sql = "SELECT * FROM food WHERE dietid =".$dietid;
   $result = mysqli_query($con, $sql);


   $rows = array();


  while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
   $rows[] = $row; 
   }


   mysqli_close($con);

   echo json_encode($rows);

Snipped Main Activity UPDATED WITH NEW URL

  private void initView() {
    // show progress dialog
    dialog = ProgressDialog.show(this, "", "Loading...");

    int dietid = 1;

    String url = "http://xxxxxxxxxxx/diet_info.php?dietid="+dietid;
    FetchDietInformationTask task = new FetchDietInformationTask(this);
//    List<NameValuePair> params = new ArrayList<NameValuePair>();

 //   params.add(new BasicNameValuePair("dietid","1"));
    task.execute(url);
}

  public void onFetchComplete(List<FoodInfoModel> data) {
    // dismiss the progress dialog
    this.data = data;
    System.out.println("data is " + data);

    if(dialog != null)  dialog.dismiss();

    // create new adapter
     adapter = new DietAdapterNew(this, data);
    // set the adapter to list
    listview.setAdapter(adapter);
    adapter.notifyDataSetChanged();

   // listview.setOnItemClickListener(myOnItemClickListener);
    }


public void onFetchFailure(String msg) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();

    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

FetDietInformationTask

   public class FetchDietInformationTask extends AsyncTask<String, Void,String>{

        private final FetchDietInformationListener listener;
        private String msg;

public FetchDietInformationTask(FetchDietInformationListener listener) {
    this.listener = listener;
}

@Override
protected String doInBackground(String... params) {
    if(params == null) return null;

    // get url from params
    String url = params[0];


    try {
        // create http connection
        HttpClient client = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);

        // connect
        HttpResponse response = client.execute(httpget);

        // get response
        HttpEntity entity = response.getEntity();

        if(entity == null) {
            msg = "No response from server";
            return null;        
        }

        // get response content and convert it to json string
        InputStream is = entity.getContent();
        return streamToString(is);
    }
    catch(IOException e){
        msg = "No Network Connection";
    }

    return null;
    }

    @Override
        protected void onPostExecute(String sJson) {
    if(sJson == null) {
        if(listener != null) listener.onFetchFailure(msg);
        return;
    }        

    try {
        // convert json string to json array
        JSONArray aJson = new JSONArray(sJson);
        // create list list
        List<FoodInfoModel> foodmodelist = new ArrayList<FoodInfoModel>();

        for(int i=0; i<aJson.length(); i++) {
            JSONObject json = aJson.getJSONObject(i);
            FoodInfoModel foodmodel = new FoodInfoModel();


            foodmodel.setDietID(Integer.parseInt(json.getString("dietid")));
            foodmodel.setDay(Integer.parseInt(json.getString("day")));
            foodmodel.setTime(Integer.parseInt(json.getString("time")));
            foodmodel.setQty(Integer.parseInt(json.getString("qty")));
            foodmodel.setItem(json.getString("item"));
            foodmodel.setMeasure(json.getString("measure"));




            // add the name to list
            foodmodelist.add(foodmodel);


       for(int e=0; e<foodmodel.getDay(); e++) {

           List<DayFoodModel> daylist = new ArrayList<DayFoodModel>();

                DayFoodModel dayfoodmodel = new DayFoodModel();
                dayfoodmodel.setDay(foodmodel.getDay());
                dayfoodmodel.setTime(foodmodel.getTime());
                dayfoodmodel.setItem(foodmodel.getItem());
                dayfoodmodel.setQty(foodmodel.getQty());
                dayfoodmodel.setFoodData(foodmodel.getItem() + foodmodel.getMeasure());


                daylist.add(dayfoodmodel);
            }
        }

        //notify the activity that fetch data has been complete
        if(listener != null) listener.onFetchComplete(foodmodelist);
    } catch (JSONException e) {
        msg = "Invalid response";
        if(listener != null) listener.onFetchFailure(msg);
        return;
    }        
    }

/**
 * This function will convert response stream into json string
 * @param is respons string
 * @return json string
 * @throws IOException
 */
    public String streamToString(final InputStream is) throws IOException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder(); 
    String line = null;

    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } 
    catch (IOException e) {
        throw e;
    } 
    finally {           
        try {
            is.close();
        } 
        catch (IOException e) {
            throw e;
        }
    }

    return sb.toString();
    }
    }

Listener

public interface FetchDietInformationListener {
public void onFetchComplete(List<FoodInfoModel> data);
public void onFetchFailure(String msg);
}

1 Answer 1

-1
$dietid = $_GET['dietid'];
$sql = "SELECT * FROM food WHERE dietid =".$dietid;

String url = "http://xxxxxxxxxxx/diet_info.php?dietid="+dietid;
Sign up to request clarification or add additional context in comments.

6 Comments

Im getting a invalid respone msg from the json catch, even if i put the URL to "xxxxxxxxxxx/diet_info.php?dietid=2 it wont load the data into the listview but does if i just hardcode the 2 in the select
it seems that when i load that url in the adress bar it gives me the following: : mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean at: while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { $rows[] = $row;
have you add above 2 lines in your php script
@Sunit Shouldn't $dietid = array( $_GET['dietid'] ); be $dietid = $_GET['dietid'] ;?
iRuth yes that has solved it its all working great now, omg i havnt slept in days been looking at code to long cant believe i couldnt figure this one out...always trying to overcomplicate thanks to you both!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.