Jump to content


  • Posts

  • Joined

  • Last visited

Recent Profile Visitors

371 profile views

TechnoDiver's Achievements

Advanced Member

Advanced Member (4/5)




Community Answers

  1. Hi Phreaks, I hope you've all made it through the weekend with sound body and mind. The following 2 methods have been working together harmoniously for quite a while: <?php public function action($action, $table, $where = [], $rule = "") { if(count($where) === 3) { $operators = array('=', '<', '>', '<=', '<='); $field = $where[0]; $operator = $where[1]; $value = $where[2]; if(in_array($operator, $operators)) { if($rule == "") { $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?"; } else { $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ? {$rule}"; } if(!$this->query($sql, array($value))->error()) { return $this; } } return false; } public function get($table, $where = [], $rule = "", $column = "*") { return $this->action("SELECT {$column}", $table, $where, $rule); } My original belief was that it was easily extensible. I'm now to the point I'm trying to do that and receiving behaviour that I don't understand. If the action() method becomes thus -> <?php public function action($action, $table, $where = [], $rule = "") { if(count($where) === 3) { $operators = array('=', '<', '>', '<=', '<='); $field = $where[0]; $operator = $where[1]; $value = $where[2]; if(in_array($operator, $operators)) { if($rule == "") { $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?"; } else { $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ? {$rule}"; } if(!$this->query($sql, array($value))->error()) { return $this; } } } else { $sql = "{$action} FROM {$table}"; echo "this <br>"; return $this; } echo "false <br>"; return false; } with the added else{} statement to handle queries without WHERE conditions, and call it with just the $table parameter -> <?php public function get() { // $table = "posts"; $table = "categories"; $field = "*"; $query = $this->_db->get($table); // print_r($query); var_dump($query); } //normally, using conditions, it would look like this: $query = $this->_db->get($table, array($field, "=", $value); It returns: which we can see is picking up my else{} statement as it should but is returning this empty object. Could someone explain this to me? thank you
  2. It seems you'd get the price via the id in the form and not actually type the price in. Which I feel is what I'm trying to do - I've already got both pieces of information that I need, I'm trying to avoid another DB query method.
  3. I'm not sure what you mean here. In my example the user can't change any information. There's a table in my DB of only the categories. Since a <select> dropdown menu with the categories appears more than once in the app I made the selectCat method to create it. This method already collects the cat. id along with it's corresponding category. I'm trying to keep each id:category connection so when an option is chosen I can $_POST both to another method that parses and displays it with other information. It's not that big of a deal to write a quick method that matches items (categories, posts, users etc) with their id's but it seems redundant to do so when each is already paired with it's respective id in the selectCat() method. I'm trying to keep the id:category connection with the <select> element to pass both to $_POST. I've been going through some mental fatigue lately so I realize I could be being a bit slow but I don't quite understand your analogy
  4. Yea, sure, it just takes another simple method. But the method I posted here already has both pieces of information - $category and $id. Is there not a way to pass them both on when they're called? For example, the method that I post in my OP is obviously part of a form to be processed. Processing it requires both the category name and its id. It seems like I would have to make another quick method to determine the category name from the id if the <select> element only holds the id; but my posted method already has both pieces of information, it seems redundant to me to make another. Or am I being a complete muggle and missing something?
  5. There's no difference it's the $category and $cat_id I need to pass together
  6. Maybe not 2 values in one element but something similar. I have the following method -> <?php public function selectCat($type) { $table = "categories"; $field = "type"; $rule = "ORDER BY id ASC"; $query = $this->_db->get($table, array($field, "=", $type), $rule ); $this->_data = $query->all(); $str = ""; foreach($this->_data as $obj) { $cat_id = $obj->id; $category = $obj->category; $cat_title = ucwords($category); $str .= " <option value='$category'>$cat_title</option> "; } return $str; } It's obviously to construct a dynamic select menu. It's called in the html of a page and then $category is passed through $_POST. But I need to pass both $category and $cat_id to $_POST. How would I go about passing both $category and $cat_id through post when the corresponding $cat_title is selected? I feel like an answer is right on the tip of my brain but I just haven't arrived there yet. Any suggestions are really welcome. Thanks EDIT: I've been thinking about having the method return an array of the data and then looping through it in the html to construct the select dropdown but looking ahead, I'm still not sure how a $cat_id would be assigned when the corresponding $cat_title is selected
  7. Interesting, I find that a bit counterintuitive as I've made a habit of keeping singles inside doubles. Thanks for your help
  8. Ok, so I misread that as "a string in JSON format". Still saying that, then why does <?php $permissions = json_decode(json_encode($group->first()->permissions), true); return a string with the exact same value and type as $group->first()->permissions) shouldn't it be returning an array from the json?
  9. I'm having a bit of trouble understanding why json_decode() is returning a null value, hoping someone has the time, will and energy to break it down for me. I've looked it up from various sources and don't get why it's not working. There's the following -> <?php public function hasPermission($key) { $group = $this->_db->get("groups", array("id", "=", $this->data()->group)); if($group->count()) { $permissions = $group->first()->permissions; echo gettype($permissions); echo $permissions; } } This returns $permissions as a string -> But when I do this -> <?php public function hasPermission($key) { $group = $this->_db->get("groups", array("id", "=", $this->data()->group)); if($group->count()) { $permissions = json_decode($group->first()->permissions, true); echo gettype($permissions); } } permissions comes back as a null type. I've been reading about json_decode() and it requires a json string in UTF-8 encoded. Which my data is (or seems to me to be). Could someone point out my error, please?! Thank you
  10. I thought for sure this would work too and didn't it was in fact the 'Preserve Log' option. Thank you. But question: Why does the second way I tried not need the 'Preserve Log' enabled and the function way does?
  11. for me I get a quick flash of something in the console and it disappears before I have a chance to see what it is and then nothing. After doing that multiple times I've can see that the flash is indeed the form data, but it doesn't stay there. It's literally so quick that I've had to reload about a dozen times before my eyes could pick up what it was. I've got no other code attached to this as it's just a simple practice exercise so I'm not really sure what's going on
  12. Hello Phreaks and Geeks, I hope the week has seen you all well and you're all gearing up for a great weekend. I've had to take up relearning of js and new learning of ajax recently and have a simple question I can't seem to resolve. Take this sample form here -> <form action="", method="post", onsubmit="return submitData(this)"> First Name: <br> <input type="text" name="firstname"> <br> Last Name: <br> <input type="text" name="lastname"> <br> Age: <br> <input type="text" name="age"> <br> <input type="submit" id="buttonOne" value="Submit"> </form> I've been passing the morning familiarizing myself with XMLHttpRequest() and FormData() classes and have the following super simple snippets function submitData(fdata) { var formData = new FormData(fdata); for (var pair of formData.entries()) { console.log( pair[0] + ' - ' + pair[1] ); } } AND var formData = new FormData(); formData.append('key_1', 'First value'); formData.append('key_2', 'Second value'); formData.append('key_3', 'Third value'); for (var pair of formData.entries()) { console.log( pair[0] + ' - ' + pair[1] ); } The bottom one, where I create the form data programatically displays the data in console properly. The top one where I try to pull the data from the form does not work. Could someone break down what is happening here and point out the errors in my thinking please. Thank you
  13. That's interesting. I've used that method in a few, very sparse situations. I originally wrote and still do write my HTML and CSS beforehand in a static way (where all repeated elements are typed in) and after I get the layout right I delete all but one of the repetitive elements and then cut->paste the last one into the HTML loop. At the time it was the best way I could fathom doing it with so many repeating elements being looped through.
  14. Yea, that becomes more clear the more experience I get. I've already made a separate HTML class, I'll work on repositioning the logic. I'm catching what you're throwing out there now. Thanks
  • 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.