Jump to content

TechnoDiver

Members
  • Posts

    203
  • Joined

  • Last visited

Everything posted by TechnoDiver

  1. 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.
  2. 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
  3. 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?
  4. There's no difference it's the $category and $cat_id I need to pass together
  5. 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
  6. Interesting, I find that a bit counterintuitive as I've made a habit of keeping singles inside doubles. Thanks for your help
  7. 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?
  8. 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
  9. 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?
  10. 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
  11. 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
  12. 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.
  13. 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
  14. Do you mean only pulling 8 items from the DB? If that's what you mean could you provide a sample snippet please
  15. I've got a lot of questions today, it seems. I can't see where the problem is in this method, it seemed very simple when I started <?php public function recent() { $rule = "ORDER BY RAND()"; $field = "type"; $type = "recent"; $query = $this->_db->get(self::$_table, array($field, "=", $type), $rule); $this->_data = $query->all(); $counter = 0; while($counter <= 7) { foreach($this->data() as $obj) { if(strlen($obj->title) > 25) { $obj->title = substr($obj->title, 0, 25) . "..."; } $data = array("id" => $obj->id, "title" => $obj->title, "content" => $obj->content, "author" => $obj->author, "add_by" => $obj->add_by, "category" => $obj->post_category, "image" => $obj->post_image, "num_likes" => $obj->num_likes, "num_comments" => $obj->num_comments, "num_views" => $obj->num_views, "post_cat_id" => $obj->post_cat_id, "date_added" => $obj->date_added ); echo $this->html($type, $data); $counter ++; } // $counter ++; } } It's pretty self explanatory. retrieve all items within the criteria and display the first 8. Problem is it displays all of the items that fit the criteria. It works fine other than that and I can't see the problem. What am I overlooking?
  16. I'm experiencing something that I've no idea how to understand it. I have the following methods -> <?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 = [], $column = "*", $rule = "") { // return $this->action("SELECT {$column}", $table, $where, $rule); // } public function get($table, $where = [], $rule = "") { return $this->action("SELECT *", $table, $where, $rule); } They're called like this -> <?php public function recent() { $rule = "ORDER BY RAND()"; $field = "type"; $type = "recent"; $query = $this->_db->get(self::$_table, array($field, "=", $type), $rule); print_r($query); } The code like it is here works. Where the weirdness comes in is if I use the commented out DB->get() method. When I switch it and use the get() method that is commented out in the above code I get a fatal error because it's parsing the query string as this -> The absolute only difference is adding in that $column parameter or not and when it's there this effed up query string is the result. Can one of you fine phreaks break it down for me, I've been staring at my code for an hour and can't figure it out. Thanks
  17. I'm not 100 clear on what you're saying. Do you mean putting the if check at the top of the actual page and call individual methods based off of that check?
  18. Thanks for all your replies, gave me a lot to think about and consider. I've been working on this since I posted this question. This is what I did - since categories.php has a $_GET["cat_id"] and single_item.php has a $_GET["item_id"] I did the following -> <?php public function get($id) { $field = ""; $type = ""; if(Input::get("cat_id")) { //retrieve all posts in the category $field = "post_cat_id"; $query = $this->_db->get(self::$_table, array($field, "=", $id)); $this->_data = $query->all(); $type = "cat"; } elseif(Input::get("post_id")) { //retrieve single post $field = "id"; $query = $this->_db->get(self::$_table, array($field, "=", $id)); $this->_data = $query->all(); $type = "single"; } if($type == "cat") { if(!$this->exists()) { echo $this->html($type); } else { $counter = 0; while($counter < count($this->data())) { foreach($this->data() as $obj) { $data = array("id" => $obj->id, "title" => $obj->title, "content" => $obj->content, "author" => $obj->author, "add_by" => $obj->add_by, "category" => $obj->post_category, "image" => $obj->post_image, "num_likes" => $obj->num_likes, "num_comments" => $obj->num_comments, "num_views" => $obj->num_views ); if(strlen($data["content"]) > 200) { $data["content"] = substr($data["content"], 0, 200) . "..."; } echo $this->html($type, $data); $counter ++; } } } } elseif($type == "single") { foreach($this->_data as $obj) { $data = array("id" => $obj->id, "title" => $obj->title, "content" => $obj->content, "author" => $obj->author, "add_by" => $obj->add_by, "category" => $obj->post_category, "image" => $obj->post_image, "num_likes" => $obj->num_likes, "num_comments" => $obj->num_comments, "num_views" => $obj->num_views, "tags" => $obj->tags, "post_cat_id" => $obj->post_cat_id ); } echo $this->html($type, $data); } } $this->html() is as follows -> <?php private function html($type, $data = []) { $html = ""; if($type == "cat") { if(!empty($data)) { if($data["author"] !== 'contributor') { $credit_str = "<p class='post-author'>Written By <a href='#'>{$data['author']}</a>&nbsp;&nbsp; <span class=''>Added By <a href='#'>{$data['add_by']}</a></span></p>"; } else { $credit_str = "<p class='post-author'>Added By <a href='#'>{$data['add_by']}</a></p>"; } $html .= " <div class='single-blog-post featured-post col-lg-4 mb-30'> <div class='post-thumb'> <a href='single_post.php?post_id={$data['id']}&related={$data['category']}'><img src='img/posts/{$data['image']}' alt=''></a> </div> <div class='post-data'> <a href='single_post.php?post_id={$data['id']}&related={$data['category']}' class=''> <h6>{$data['title']}</h6> </a> <div class='post-meta'> $credit_str <p class='post-excerp'>{$data['content']}</p> <div class='d-flex align-items-center'> <a href='#' class='post-like'><img src='../assets/img/like.png' alt=''> <span>{$data['num_likes']}</span></a> <a href='#' class='post-comment'><img src='../assets/img/chat.png' alt=''> <span>{$data['num_comments']}</span></a> <a href='#' class='post-comment'><i class='fa fa-eye'></i><span>{$data['num_views']}</span></a> </div> </div> </div> </div>"; } else { $html = "<h2 class='text-center text-danger my-4'>There are no posts in this category</h2>"; } } elseif($type == "single") { if($data['author'] !== 'contributor') { $credit = $data['author']; } else { $credit = $data['add_by']; } $tag_str = ""; $tags = explode(",", $data['tags']); foreach($tags as $tag) { $tag_str .= "<li><a href='tags.php?tag=$tag' class='btn bg-color-primary text-color-fourth mx-1'>$tag</a></li>"; } $content = str_replace(["\r", "\n"], '', $data['content']); $html .= " <div class='single-blog-post featured-post single-post'> <div class='post-thumb'> <a href='#'><img src='img/posts/{$data['image']}' alt=''></a> </div> <div class='post-data'> <a href='category_posts.php?cat_id={$data['post_cat_id']}' class='post-catagory'>{$data['category']}</a> <a href='#' class='post-title'> <h6>{$data['title']}</h6> </a> <div class='post-meta'> <p class='post-author'>By <a href='#'>$credit</a></p> <p>{$content}</p> <div class='newspaper-post-like d-flex align-items-center justify-content-between'> <div class='newspaper-tags d-flex'> <span>Tags: </span> <ul class='d-flex'> $tag_str </ul> </div> <div class='d-flex align-items-center post-like--comments'> <a href='#' class='post-like'><i class='fa fa-thumbs-up'></i><span>{$data['num_likes']}</span></a> <a href='#' class='post-comment'><i class='fa fa-comment'></i> <span>{$data['num_comments']}</span></a> <a href='#' class='post-comment'><i class='fa fa-eye'></i><span>{$data['num_views']}</span></a> </div> </div> </div> </div> </div> // THE REST IS IRRELEVANT AND REDACTED TO SAVE SPACE // "; } return $html; } It works well but I'd still certainly be open to any comments regarding the decision and it's implementation. I used elseif{} because I'll probably be expanding on it, maybe, maybe not I forgot to mention that I can see myself breaking up the get() method into 2 methods in the future
  19. Hi Phreaks, hope you all had a productive weekend. I've been contemplating something on my project that I'm not sure about and thought here would be a great place to get some other insights into it. I don't believe I need any code for this question. It's a fairly common setup index.php has category links that lead to a page that lists all items in that category. Click on an item and you go to the page specific to it. So let's say index.php->item_categories.php->single_item.php. I originally had 2 methods in the Item class handling this -> getItemByCategory() and getSingleItem() each item has a bunch of properties to it so I decided I wanted to try to put them into one get() method. Which would require an IF statement. My question is, and pardon me if this sounds naive, what would any of you suggest for the condition? it would very basically be -> if(the page is items_categories.php){ run this code } elseif(the page is single_item.php){ run this code } Is the best option, like in the example, to actually make the condition which page it is? Or sending some sort of indicator via GET? Is there a common convention that is used for this scenario that I'm not aware of? Looking forward to reading your responses
  20. I was just working on that and came here to give it as a solution. I'm really a dumb ass sometimes though because I had been messing around with building that method so long I had a bunch of commented out lines and didn't notice my print_r was also inside the foreach loop. Whereas the echo was meant to be inside it. I have a lot of work to do with getting good at formatting data with loops and other build-ins. It's very time consuming for me at the moment
  21. everything in it's time brother, I appreciate and need the help but I can't help when I get the notifications. Here's the json
  22. Hi, Phreaks, I've made a number in inquiries into roughly the same problem. For that I apologize but I'm really confused and frustrated and can't work this seemingly simple problem out. Like before, I'm trying to make an associative array out of DB data and I can't work out some of the results I'm getting. If I use this code here -> <?php public function all($column = "") { $items = array(); foreach($this->_results as $item) { echo $item->id . " => " . $item->category . " "; } } I get this -> Excellent, so far But if I do this -> <?php public function all($column = "") { $items = array(); foreach($this->_results as $item) { $items [] = [$item->id => $item->category]; print_r($items); } } I get back a whole mound of data -> Why? I don't get it. Why is building this assoc array with category_id as the key and category as the value so difficult. I don't understand why this second code isn't making that arrray
  23. Thanks both for the replies. It was actually pretty simple once I thought it through, it was an early morning question and my mind wasn't totally clear -> <?php public function all() { $items = array(); foreach($this->_results as $item) { $items [] = $item->category; //still need to figure this into a variable } return $items; } but I do have one issue here. Might sound really simplistic to some, but has been giving me problems. This is not meant to be a method specific to categories and I kind quite work out how to abstract $item->category to use a parameter value. for example -> <?php public function all($column) { $items = array(); foreach($this->_results as $item) { $items [] = $item->$column; //still need to figure this into a variable } return $items; } doesn't work if $column = "categories" ( as a string). How do I pass the column name in as a parameter??
×
×
  • 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.