SamC

Members
  • Content count

    672
  • Joined

  • Last visited

  • Days Won

    4

SamC last won the day on January 27

SamC had the most liked content!

Community Reputation

570 Excellent

3 Followers

About SamC

  • Rank
    Hero Member

Profile Information

  • Location
    Surrey, UK

Recent Profile Visitors

1,402 profile views
  1. Only just noticed the release_date key is either not there at all, or present, but empty for some movies e.g.: [97] => Array ( [character] => Fred Rogers [credit_id] => 5a709346c3a36847e4012aaa [poster_path] => [id] => 501907 [video] => [vote_count] => 0 [adult] => [backdrop_path] => [genre_ids] => Array ( [0] => 18 ) [original_language] => en [original_title] => You Are My Friend [popularity] => 1 [title] => You Are My Friend [vote_average] => 0 [overview] => The story of Fred Rogers, the honored host and creator of the popular children's television program, Mister Rogers' Neighborhood (1968). [release_date] => ) Had to modify the functions as checking for key existence was not enough: // Movies with release date function hasReleaseDate($movie) { // Has key with value if (array_key_exists('release_date', $movie) && !empty($movie['release_date'])) { return true; } return false; } // Movies without release date function noReleaseDate($movie) { // No key if (!array_key_exists('release_date', $movie)) { return true; } // Has key but no value if (array_key_exists('release_date', $movie) && empty($movie['release_date'])) { return true; } return false; } Seems to work now.
  2. Did this now, code could most likely be improved, but it works: if ($movies) { // Movies with release date function hasReleaseDate($movies) { return (array_key_exists('release_date', $movies)) ? true : false; } // Movies without release date function noReleaseDate($movies) { return (!array_key_exists('release_date', $movies)) ? true : false; } // Generate arrays of movies with/without release date $movies_with_release_date = array_filter($movies, 'App\Controllers\hasReleaseDate'); $movies_without_release_date = array_filter($movies, 'App\Controllers\noReleaseDate'); // Sort by date - newest first function sortNewestFirst( $a, $b ) { return strtotime($b["release_date"]) - strtotime($a["release_date"]); } usort($movies_with_release_date, "App\Controllers\sortNewestFirst"); // Append movies without release date $movies = array_merge($movies_with_release_date, $movies_without_release_date); View::renderTemplate('Actor/list-all.html', [ 'name' => $this->getFormattedName($name), 'movies' => $movies ] ); } else { View::renderTemplate('Actor/no-results.html'); } The more I move on with this, the more I see the importance of finding some kind of structure. It's starting to confuse myself with where to put code, whether it goes in a model, a controller etc. Def need to work on this. However, all good for now thanks @arjen
  3. I'll give this a try, thanks. Thanks, had a problem submitting, 2mins+ with nothing haopening so I refreshed, waited, waited, redreshed, tried again. Third time submitted normally i.e. took seconds. Not sure what went wrong, maybe dodgy internet.
  4. Hi, The array I'm working with (shortened example, the first three items) is: Array ( [0] => Array ( [character] => [credit_id] => 5a26eac792514103360f3e03 [release_date] => 2017-06-13 [vote_count] => 5 [video] => [adult] => [vote_average] => 7.6 [title] => Celtics/Lakers: Best of Enemies [genre_ids] => Array ( [0] => 99 ) [original_language] => en [original_title] => Celtics/Lakers: Best of Enemies [popularity] => 3.191622 [id] => 456788 [backdrop_path] => [overview] => A two-part series exploring the Celtics–Lakers rivalry, focusing mainly on the 1980s era of Larry Bird and Magic Johnson but also examining the entire history of the NBA through the rivalry. [poster_path] => /mF2ccs6AuGUvEIY9lfj8chFu6pW.jpg ) [1] => Array ( [character] => Performer [credit_id] => 5939ea8ec3a36823440029c4 [release_date] => 2017-06-04 [vote_count] => 1 [video] => [adult] => [vote_average] => 1 [title] => Comedy Central's Colossal Clusterfest [genre_ids] => Array ( [0] => 35 ) [original_language] => en [original_title] => Comedy Central's Colossal Clusterfest [popularity] => 1.651011 [id] => 460340 [backdrop_path] => /gsZT6wW4Oo7ApYD4TG5aZWby1Jj.jpg [overview] => Highlights from the inaugural Colossal Clusterfest that took place in San Francisco from June 2-4. [poster_path] => /ui1nZudYxcqJjFIZsiuTyqcV6PL.jpg ) [2] => Array ( [character] => [credit_id] => 589119a092514103f000c00f [release_date] => 2017-03-10 [vote_count] => 4 [video] => [adult] => [vote_average] => 2.6 [title] => G-Funk [genre_ids] => Array ( [0] => 99 ) [original_language] => en [original_title] => G-Funk [popularity] => 5.432522 [id] => 438517 [backdrop_path] => [overview] => G-Funk is the untold story of three childhood friends from East Long Beach who helped commercialize hip hop by developing a sophisticated and melodic new approach – merging Gangsta Rap with elements of Motown, Funk, and R&B. [poster_path] => /lmByKQnHcMch8FkWzNquKtbFueN.jpg ) ) ...and I wanted to sort them by date, newest first. It's small class like this. The problem is outlined in the comments: <?php namespace App\Controllers; // Bring View class into current namespace use \Core\View; // Bring Actor model class into current namespace use App\Models\Actor; /** * About controller */ class Actors extends \Core\Controller { public function list() { // Model 'Actor' just queries API and returns array $movies = Actor::getAll(); // Sort by date - newest first function sortNewestFirst( $a, $b ) { return strtotime($b["release_date"]) - strtotime($a["release_date"]); } /** * ********************************************** * usort() ERRORS IF 'release_date' does not exist * ********************************************** */ usort($movies, "App\Controllers\sortNewestFirst"); View::renderTemplate('Actor/listAll.html', [ 'name' => $this->getFormattedName(), 'movies' => $movies ] ); } /** * Get the current name from URL */ private function getFormattedName() { return ucwords(htmlspecialchars($_GET['name'], ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8")); } } The error: Uncaught exception: 'ErrorException' Message: 'Undefined index: release_date' Unfortunately, the API has no release date for some movies, so my search works perfectly for some actors, but errors for others. I was trying to figure out how to get array_key_exists() into this somehow but I'm totally stuck. Being an array of arrays makes this more tricky. I was thinking of looping each one, if the key exists, add to a new array, but then the movie with the missing key would be completely absent. I was thinking of just chucking it at the bottom of the list. Hopefully this makes sense. Any help, hints or improvements would be awesome, thanks.
  5. I'd be interested in reading this Good luck with the project, sounds exciting!
  6. Since starting my journey to actually learn programming, css is off the menu (but struggling to ignore the PW forum!). So with that in mind, a framework will be very handy indeed. I haven't got time to learn grid right now, nor would I use it anyway, too early days for me.
  7. Ah I see. I was thinking of: 1) Submitting the form 2) Post to form-process.php 3) Sanitize values, see which ones validate (with valitron) 4) Stick the valid ones in session as an array of form data along with which ones have an error, like: [ 'name' => 'Sam', 'email' => 'fail', 'message' => 'hi this is a message', 'phone_number' => 'fail' ] 5) Redirect back to original form 6) Repopulate the valid fields from session, error class on the rest If all fields are valid, after (3), just email form, redirect back to contact page (with now blank contact form) with success message. Is this a goofy approach? I don't see any other way if POST data has been cleared, either by session->redirect() (post to same page) or by a PRG pattern.
  8. Thanks everyone for the input here, it's very useful Regarding this, how does this stop duplicate submissions? I mean, the code doesn't run when the form hasn't been submitted ($input->post_submit), but when the form has been submitted, what happens on refresh? The last action was POST so doesn't the form attempt to POST again? This is one reason why I started using the PRG pattern, although tbh, I'd rather post to the same page, makes things simpler, especially if you're keeping form fields populated i.e. the ones that did validate. Regarding the nette docs, I didn't really understand them! I'm a mere beginner @bernhard
  9. Hi everyone, just writing a new tutorial as a 'break' from learning PHP. It involves making a contact form. In brief, the part of the form with the radios and a checkbox: contact.php <fieldset class="form-group"> <div class="row"> <legend class="col-form-label col-sm-2 pt-0">Choose a colour</legend> <div class="col-sm-10"> <div class="form-check"> <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="red" checked> <label class="form-check-label" for="gridRadios1">Red</label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="blue"> <label class="form-check-label" for="gridRadios2">Blue</label> </div> <div class="form-check"> <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="green"> <label class="form-check-label" for="gridRadios3">Green</label> </div> </div> </div> </fieldset> <div class="form-group row"> <div class="col-sm-2"><a href="#">Terms & Conditions</a> (required)</div> <div class="col-sm-10"> <div class="form-check"> <input class="form-check-input" type="checkbox" name="checkboxAgree" id="checkboxAgree"> <label class="form-check-label" for="checkboxAgree">Tick to agree</label> </div> </div> </div> ...and the template where the form posts to (very early days work in progress): contact-process.php <?php namespace ProcessWire; // Sanitize user inputs $name = $sanitizer->text($input->post->name); $email = $sanitizer->email($input->post->email); $message = $sanitizer->textarea($input->post->message); // Radio button values $allowed_values = ['red', 'green', 'blue']; /** * Only allow allowed values above, returns NULL if anything other * than the values in the above array */ $radio_value = $sanitizer->option($input->post->gridRadios, $allowed_values); // DOES THIS NEED TO BE SANITIZED?? $checkbox_agree = $input->post->checkboxAgree; $submitted = $input->post->submitted; // Concantenate all the values $str = 'Name: ' . $name; $str .= '<br>'; $str .= 'Email: ' . $email; $str .= '<br>'; $str .= 'Message: ' . $message; $str .= '<br>'; $str .= 'Radio value: ' . $radio_value; $str .= '<br>'; $str .= 'Checkbox agree: ' . $checkbox_agree; $str .= '<br>'; $str .= 'Form is submitted: ' . $submitted; echo $str; Form outputs: Name: Sam Email: sam@test.com Message: Hi, this is a test message... Radio value: blue Checkbox agree: on Form is submitted: 1 So would it be necessary to sanitize the radio buttons so only red, blue or green are submitted, and also, what about the checkbox? Could a user manipulate the POST array to send values other than the ones I want? Maybe just being paranoid but don't want to give out information that I haven't checked first. Thanks
  10. Hi @Artomultiplo Sorry but this site never materialised. Maybe @Robin S would be able to help you more with this.
  11. Nice site, I like it! I think you have a couple of typos on the contact page though: 'Insginnia / Happiness Inside' and: 'Reach Insginnia' Might want to check the other pages just in case
  12. I think Ryan may have bought one by now To join in the fun though, my favourite mouse after trying many is the Roccat Savu. Fits like a glove, no RSI for my PC worn hands https://www.roccat.org/en-GB/Products/Gaming-Mice/Savu/
  13. This might help: https://www.pwtuts.com/processwire-tutorials/in-depth-look-at-the-images-field-type/uploading-and-rendering-images/ Not sure why there's two loops in there, I didn't fully understand your first post. Maybe like: <ul> <?php $child_pages = $page->children; foreach($child_pages as $child_page): ?> <li> <?= $child_page->title; ?> <!-- IF IMAGE FIELD SET TO ALLOW ONLY 1 --> <img src="<?= $child_page->image->url; ?>" /> <!-- OR IF IMAGE FIELD SET TO ALLOW MULTIPLE, GET THE FIRST ONE, not sure if 'first' or 'first()' --> <img src="<?= $child_page->image->first()->url; ?>" /> </li> <?php endforeach; ?> </ul> Presuming the image field itself is named 'image'. p.s. welcome to the forum @disall2000
  14. Sorry but I didn't actually make anything, the whole thing was hypothetical. TBH I still don't know how to approach different 'types' of users. Saving everything on a user template doesn't make sense to me when users will need different fields. I like the sound of @LostKobrakais approach best, using a user template simply for authentication and having a 'profile' or 'employee' template linked to the user. If you do work it out, feel free to share how you did it!
  15. I guess I haven't been doing anything complex enough to need a GUI. Just stuff like this: https://confluence.atlassian.com/bitbucketserver/importing-code-from-an-existing-project-776640909.html Forced myself away from dropbox and now have to use git on my two computers in order to sync my work. Otherwise I would have just kept putting it back. It's been interesting to say the least. Cool progress though!