Jump to content

ke-jo

New Members
  • Posts

    7
  • Joined

  • Last visited

Everything posted by ke-jo

  1. @Barand Thank you for your reply. I think I can see what is going on. I will work with your code. Would you be offended by a $100usd donation? Are you available for continued support?
  2. Hello, I am trying phpfreaks out without much luck here. If anybody here truly knows how to validate checkboxes as a group please give some assistance. I had to remove the array from the 'name' attribute in my checkbox inputs in order to get the user's checked selections to be properly distributed into separate database columns (not all in one column as a string of text). I am now able to successfully get the entries into the database as "1" for selected and "0" for un-selected. Now however, since there is no array that is holding these input names my validation class method does not work. My validation is suppose to show an error if none of these checkboxes are selected. I had to change each input name to its own identity ( fruit_selection_apple, etc... ), then set my validation like (see code below). However, this is NOT OOP PHP. I had to hard code these values into the validation class. I am also now getting a notice, " Notice: Undefined index: fruit_selection ". Does anyone legitimately know their PHP enough to help with this issue? public function check($source, $items = array()) { foreach($items as $item => $rules) { foreach($rules as $rule => $rule_value) { $value = trim($source[$item]); $item = escape($item); $checkboxvalue = (isset($_POST['fruit_selection_apple'])) || (isset($_POST['fruit_selection_orange'])) || (isset($_POST['fruit_selection_banana'])) || (isset($_POST['fruit_selection_pear'])) || (isset($_POST['fruit_selection_kiwi']) ) if($rule === 'atleastone' && empty($checkboxvalue)) { $this->addError("{$item} You must select at least one checkbox."); }
  3. @requinixThanks.... I will check to see if I can get help on Stack Overflow.
  4. @requinix Then how would you suggest I get the values to be entered into the individual database columns? As stated earlier the array did not place the entries into the individual columns. Can you actually show me some working code? Thanks Example: column apple = 0, column orange = 1, column banana = 1, column pear = 0, column kiwi = 1.
  5. @requinix Thanks for the reply. I am still having issues with this code. I removed the array brackets from the name, ( name="fruit_selection" ) . Without the array I am now able to get the entries into the database properly as "1" for selected and "0" for un-selected. However, since there is no array that is holding these input names my validation class method does not work. My validation is suppose to show an error if none of these checkboxes are selected. I had to change each input name to its own identity ( fruit_selection_apple ), then set my validation like this: public function check($source, $items = array()) { foreach($items as $item => $rules) { foreach($rules as $rule => $rule_value) { $value = trim($source[$item]); $item = escape($item); $checkboxvalue = (isset($_POST['fruit_selection_apple'])) || (isset($_POST['fruit_selection_orange'])) || (isset($_POST['fruit_selection_banana'])) || (isset($_POST['fruit_selection_pear'])) || (isset($_POST['fruit_selection_kiwi']) ) if($rule === 'atleastone' && empty($checkboxvalue)) { $this->addError("{$item} You must select at least one checkbox."); } However, this is NOT OOP PHP. I had to hard code these values into the validation class. I am also now getting a notice, "Notice: Undefined index: fruit_selection " on: $validate = new Validate(); $validation = $validate->check($_POST, array( 'fruit_selection' => array( 'atleastone' => true ), Can anybody help me to re-code this to work without the notice and to keep it as OOP? I know this is a bit complex but I would appreciate the help. Thanks
  6. @Psycho Thanks for your feedback. I have removed the PDO on " $sql = "INSERT INTO preffruit (`user_id`, `" . implode('`, `', $keys) . "`) VALUES (LAST_INSERT_ID(), {$values})"; " Because my code is failing. I need to keep it simple so I can try to figure out how to get it to work. I am trying to crawl before I walk. You are way ahead of my abilities right now. I promise you I will replace the PDO when my code works. Thanks.
  7. Hello all, I am trying to learn OOP in PHP so please forgive my ignorance. I seem to be having difficulty inserting data into my database from checkbox items. My database has a column for each each fruit. I would like for each column to be populated with either a one(1) for checked or a zero(0) for not checked. Currently, my attempts have failed. I can either get all checkboxes to insert all zeros or I can get the first column to insert a one(1) while the others as zeros. Is there anybody here that can help me figure out what I am doing wrong and help me to re-work my code so I can get this to work? If anybody can help me using OOP methods that would be fantastic. Thank you all in advance. $preffruit->create_preffruit(array( 'fruit_apple' => escape(Input::get(['fruit_selection'][0]) ? 1 : 0), 'fruit_orange' => escape(Input::get(['fruit_selection'][1]) ? 1 : 0), 'fruit_banana' => escape(Input::get(['fruit_selection'][2]) ? 1 : 0), 'fruit_pear' => escape(Input::get(['fruit_selection'][3]) ? 1 : 0), 'fruit_kiwi' => escape(Input::get(['fruit_selection'][4]) ? 1 : 0) )); <input type="checkbox" name="fruit_selection[]" value="fruit_apple"> <input type="checkbox" name="fruit_selection[]" value="fruit_orange"> <input type="checkbox" name="fruit_selection[]" value="fruit_banana"> <input type="checkbox" name="fruit_selection[]" value="fruit_pear"> <input type="checkbox" name="fruit_selection[]" value="fruit_kiwi"> public function insert_preffruit($table, $fields = array()) { $keys = array_keys($fields); $values = ''; $x = 1; foreach($fields as $field) { $values .= '?'; if($x < count($fields)) { $values .= ', '; } $x++; } $sql = "INSERT INTO preffruit (`user_id`, `" . implode('`, `', $keys) . "`) VALUES (LAST_INSERT_ID(), {$values})"; if(!$this->query($sql, $fields)->error()) { return true; } return false; }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.