使用PHP和CodeIgniter在数据库中对价目表进行折扣

时间:2015-07-16 01:53:28

标签: php mysql codeigniter

我有一个表单来输入折扣/上升的百分比,并将其应用于存储在数据库表中的产品列表,这是我在下面解释的控制器代码:

public class DiscardEmergencyPers extends DialogFragment implements View.OnClickListener, AdapterView.OnItemSelectedListener,DialogInterface.OnDismissListener, DialogInterface {
    private final String fullname;
    private final List<EmBaseInfo> mModels;
    private final String status;
    private final int position;
    private final RecyclerView rc;

//private final String updated_at;
EditText pin_code;
Button cancel, done;
Communicator com;
private EmergenciaDBAdapter emergencia_helper;
private Context context;
private RecyclerView recycler_view;

public DiscardEmergencyPers(String fullname, String status,List<EmBaseInfo> mModels, int position, RecyclerView rc){
    this.fullname=fullname;
    this.status = status;
    this.mModels = mModels;
    this.position = position;
    this.rc =rc;

}
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    com = (Communicator) activity;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_discardemperson, null);
    context = getActivity().getApplicationContext();
    getDialog().setTitle("Remove emergency contact");
    //Spinner that contains relationships with the current contact
    pin_code = (EditText) view.findViewById(R.id.pin);

    /*ArrayAdapter adapter = ArrayAdapter.createFromResource(context, R.array.relationship, android.R.layout.simple_spinner_dropdown_item);
    relationship_spinner.setAdapter(adapter);
    relationship_spinner.setOnItemSelectedListener(this);*/
    //End of Spinner
    //

    emergencia_helper = new EmergenciaDBAdapter(context);
    //spinner = (Spinner)view.findViewById(R.id.spinner);
    cancel = (Button) view.findViewById(R.id.cancel);
    done = (Button) view.findViewById(R.id.done);
    cancel.setOnClickListener(this);
    done.setOnClickListener(this);

    setCancelable(false);
    return view;
}

@Override
public void onClick(View view) {
    if(view.getId()==R.id.cancel){
        dismiss();
        com.onDialogMessage("Cancel clicked");
    }
    else{
        com.onDialogMessage("Done clicked");
        if(pin_code.getEditableText().toString().equals("12345")){
            dismiss();

            emergencia_helper.update(status, fullname/*,updated_at*/);

            //EBAdapter n_adapter = new EBAdapter(getActivity(),removeItem(this.position),getFragmentManager(),rc);
            //rc.setAdapter(n_adapter);
            //rc.getAdapter().(this.position);

            rc.getAdapter().notifyDataSetChanged();
            rc.getAdapter().notifyItemRemoved(0);
            //Bundle savedInstanceState=null;

            //getActivity().onCreate(savedInstanceState);

            CharSequence text = "Good Pin "+rc.getAdapter();
            Log.d("Contacts", rc.getClass()+"");
            Log.d("Contacts", rc.getAdapter()+" "+getActivity());
            int duration = Toast.LENGTH_SHORT;
            Toast.makeText(context, text, duration).show();
        }
        else{
            CharSequence text = "Wrong Pin";
            //Log.d("Contacts", mModels.toString());
            int duration = Toast.LENGTH_SHORT;
            Toast.makeText(context, text, duration).show();
        }



        //long id=emergencia_helper.insert(photo,fullname,phone,relationship, status,created_at,updated_at);
        /*if(id<0){
            CharSequence text = "Row insertion unsuccessful ";
            //Log.d("Contacts", mModels.toString());
            int duration = Toast.LENGTH_SHORT;
            Toast.makeText(context, text, duration).show();
        }
        else{
            CharSequence text = "Row insertion successful ";
            //Log.d("Contacts", mModels.toString());
            int duration = Toast.LENGTH_SHORT;
            Toast.makeText(context, text, duration).show();
        }*/
    }
}    

首先设置上升,然后从products表中获取与我们正在处理的客户端相对应的所有内容(存储为会话变量),每个设置数组以更新和更新产品有相应的ID。 (该表包含列id,客户端,名称和价格)。之后,它应该重定向到另一个控制器。

它没有显示错误,但没有对数据库进行任何更改。

1 个答案:

答案 0 :(得分:0)

I modified your code, please use this and check. it will work correctly.
$rise = $this->input->post('rise')/100;
$this->db->where('client',$this->session->userdata('id_client'));
$query = $this->db->get('products');

foreach ($query->result() as $row){

      $price=$row->price + ($row->price * $rise);

    $price = array(
        'price' => $price
    );
    $this->db->where('id',$row->id);
    $this->db->update('products',$price);

}