Jump to content

Strider64

Members
  • Posts

    473
  • Joined

  • Last visited

  • Days Won

    12

Everything posted by Strider64

  1. Yes, It is after monkeying around with it for almost a day I simplified things and came up with this - I still think it can be better. $data = []; /* * The below must be used in order for the json to be decoded properly. */ try { $data = json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR); } catch (JsonException $e) { } //echo "<pre>" . print_r($data['category'], 1) . "</pre>"; //die(); if (isset($_SESSION['category']) && $data['category'] === $_SESSION['category']) { $_SESSION['current_page'] += 1; } else { unset($_SESSION['category']); // This probably isn't even needed? $_SESSION['category'] = $data['category']; $_SESSION['current_page'] = 1; } I will find out when I have more images or have less per page and have my links working in JavaScript (Which I probably should break this down even further?). Thanks John
  2. I am definitely going crazy as I know can't get it the database to load whereas before I could. Before this latest fiasco of mine I did what they said to do. /* create FETCH request */ const createRequest = (url, succeed, fail) => { let cat = {category: category.value} cat.category = category.value; console.log(category.value); console.log('cat',cat); fetch(url, { method: 'POST', // or 'PUT' body: JSON.stringify(cat) }) //.then((response) => handleErrors(response)) .then(res => res.text()) // convert to plain text .then(text => console.log(text)) .then((data) => succeed(data)) .catch((error) => fail(error)); }; Now I get this gobbly gook on all browsers <br /> <b>Warning</b>: Undefined array key "category" in <b>/homepages/11/d329291176/htdocs/phototech/galleryImagesGet.php</b> on line <b>19</b><br /> [{"id":171,"user_id":2,"author":"John Pepp","page":"blog","category":"general","image_path":"assets\/uploads\/img-gallery- It's obvious that it's the first two lines of the mess. But how would I fix it? It used to work with it on Firefox, but I restarted my computer and now it say is doesn't load. Even though I did do a re-hard set time when I made modifications in Firefox, but at least Chrome is still giving me that error. So at least I am just semi-crazy. BTW - Thanks for helping
  3. I normally don't ask for help but this problem is driving me crazy. I'm using plain Vanilla JS and use FETCH to retrieve a bunch of information for my photo gallery . https://phototechguru.com/gallery.php It works fine in Firefox, but not Chrome or Safari. In the later two browsers I am getting this error message. Database Table did not load SyntaxError: Unexpected token < in JSON at position 0 I will try to give the code I think that is important - here's some of the vanilla javascript /* create FETCH request */ const createRequest = (url, succeed, fail) => { let cat = {} cat.category = category.value; console.log(category.value); console.log('cat',cat); fetch(url, { method: 'POST', // or 'PUT' body: JSON.stringify(cat) }) .then((response) => handleErrors(response)) .then((data) => succeed(data)) .catch((error) => fail(error)); }; function selection(category) { /* Remove Image For Screen (cleanup) */ while (container.firstChild) { container.removeChild(container.firstChild) } let url = 'galleryImagesGet.php'; //let url = urlRequest + "category=" + category; createRequest(url, categoryUISuccess, categoryUIError); } category.addEventListener('change', () => { selection(category.value) } , false); category.value = 'general'; createRequest('galleryImagesGet.php' , categoryUISuccess, categoryUIError); })(); the galleryImagesGet.php <?php require_once 'assets/config/config.php'; require_once "vendor/autoload.php"; use PhotoTech\CMS; use PhotoTech\Pagination_New as Pagination; $cat = []; /* Makes it so we don't have to decode the json coming from javascript */ header('Content-type: application/json'); /* * The below must be used in order for the json to be decoded properly. */ try { $cat = json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR); } catch (JsonException $e) { } if ($cat['category'] === $_SESSION['category'] && isset($_SESSION['category'])) { $_SESSION['current_page'] + 1; } elseif (!isset($cat['category'])) { $_SESSION['category'] = $cat['category']; $_SESSION['current_page'] = 1; } else { $_SESSION['category'] = $cat['category']; $_SESSION['current_page'] = 1; } $per_page = 12; // Total number of records to be displayed: $total_count = CMS::countAllPage('blog', $_SESSION['category']); // Total Records in the db table: /* Send the 3 variables to the Pagination class to be processed */ $pagination = new Pagination($_SESSION['current_page'], $per_page, $total_count); /* Grab the offset (page) location from using the offset method */ $offset = $pagination->offset(); /* * Grab the data from the CMS class method *static* * and put the data into an array variable. */ $cms = CMS::page($per_page, $offset, 'blog', $_SESSION['category']); output($cms); function output($output): void { http_response_code(200); try { echo json_encode($output, JSON_THROW_ON_ERROR); } catch (JsonException) { } } I have tried Googling to find the solution and tried some of them, but I'm not to familiar with some of the terminology and I haven't gone too in-depth with the console for debugging. I can show more code if needed. What's really puzzling is why will work in Firefox and not other browsers? 🤔 I don't know if this what put in the right forum category?
  4. I just want to add it seems like you want to validate the data after it has been sent that to me doesn't makes sense to me. 🤔 To me this invalidates the reason you are using Ajax in the first place. I could be looking at the code wrong?
  5. /* * As long as you have the correct field names as the key and * the correct values in the corresponding keys the following * procedural function should work with no problem. * */ function insertData(array $data, $pdo, $table) { try { /* Initialize an array */ $attribute_pairs = []; /* * Set up the query using prepared states with the values of the array matching * the corresponding keys in the array * and the array keys being the prepared named placeholders. */ $sql = 'INSERT INTO ' . $table . ' (' . implode(", ", array_keys($data)) . ')'; $sql .= ' VALUES ( :' . implode(', :', array_keys($data)) . ')'; /* * Prepare the Database Table: */ $stmt = $pdo->prepare($sql); /* * Grab the corresponding values in order to * insert them into the table when the script * is executed. */ foreach ($data as $key => $value) { if($key === 'id') { continue; } // Don't include the id: $attribute_pairs[] = $value; // Assign it to an array: } return $stmt->execute($attribute_pairs); // Execute and send boolean true: } catch (PDOException $e) { /* * * getCode() is the sqlstate * * echo "unique index" . $e->errorInfo[1] . "<br>"; * * An error has occurred if the error number is for something that * this code is designed to handle, i.e. a duplicate index, handle it * by telling the user what was wrong with the data they submitted * failure due to a specific error number that can be recovered * from by the visitor submitting a different value * * return false; * * else the error is for something else, either due to a * programming mistake or not validating input data properly, * that the visitor cannot do anything about or needs to know about * * throw $e; * * re-throw the exception and let the next higher exception * handler, php in this case, catch and handle it */ if ($e->errorInfo[1] === 1062) { return false; } throw $e; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; // Not for a production server: } return true; } Here's a detailed explanation of catching a duplicate username. Someone explained to me a long time ago that this was the proper way to catch duplicates especially if your website has a lot of traffic on it. You could do the following to check if a username already exists, BUT it doesn't prevent duplicates as two people could be entering the exact username at the same time. Sorry it's part of a Class, but this is just use as a courtesy to whoever is registering as the odds entering at the same username at the same time is not that great. public static function usernameCheck($username): array { if (isset(static::$table)) { $query = "SELECT username FROM " . static::$table ." WHERE username = :username"; } $stmt = Database::pdo()->prepare($query); $stmt->bindParam(':username', $username); $stmt->execute(); if ($stmt->fetch(PDO::FETCH_ASSOC)) { $data['check'] = true; return $data; } $data['check'] = false; return $data; }
  6. but if you handle errors first /* Handle General Errors in Fetch */ const handleErrors = function (response) { if (!response.ok) { throw (response.status + ' : ' + response.statusText); } return response.json(); }; and do this /* create FETCH request */ const createRequest = (url, succeed, fail) => { fetch(url) .then((response) => handleErrors(response)) // Check for errors .then((data) => succeed(data)) .catch((error) => fail(error)); // Catch the errors }; aren't you do the same thing with promises ? Fetch to me can be confusing.
  7. Why not catch the "error"? /* Handle General Errors in Fetch */ const handleErrors = function (response) { if (!response.ok) { throw (response.status + ' : ' + response.statusText); } return response.json(); }; /* Success function utilizing FETCH */ const UISuccess = function (parsedData) { /* Successfully Load & No Errors */ }; /* If Database Table fails to load then hard code */ const UIError = function (error) { console.log("Database Table did not load", error); /* Do Something like even run more code? */ }; /* create FETCH request */ const createRequest = (url, succeed, fail) => { fetch(url) .then((response) => handleErrors(response)) .then((data) => succeed(data)) .catch((error) => fail(error)); }; createRequest(url, UISuccess, UIError);
  8. /* Handle General Errors in Fetch */ const handleErrors = function (response) { if (!response.ok) { throw (response.status + ' : ' + response.statusText); } return response.json(); }; /* Success function utilizing FETCH */ const UISuccess = (data) => { console.log(data); // Parsed Data coming back from Ajax }; const UIError = (error) => { console.log("Database Table did not load", error); }; /* create FETCH request */ const createRequest = (url, succeed, fail) => { fetch(url) .then((response) => handleErrors(response)) .then((data) => succeed(data)) .catch((error) => fail(error)); }; /* Call the createRequest function const NameOfFunction () => {} */ createRequest(requestUrl, UISuccess, UIError); I found breaking down FETCH in separate function calls to be easier to understand. Maybe this will help?
  9. I find this an easy way to handle updating using an array /* Note ALL index keys must match the names of the Database Table columns */ function updateData(array $data, $pdo, $table): bool { /* Initialize an array */ $attribute_pairs = []; /* Create the prepared statement string */ foreach ($customers as $key => $value) { /* I personally would had just used id instead of customer_id as that is more inline with universal naming */ if($key === 'customer_id') { continue; } // Don't include the id: $attribute_pairs[] = "$key=:$key"; // Assign it to an array: } /* * The sql implodes the prepared statement array in the proper format * and updates the correct record by id. */ $sql = 'UPDATE ' . $table . ' SET '; $sql .= implode(", ", $attribute_pairs) . ' WHERE customer_id =:customer_id'; /* Normally in two lines, but you can daisy-chain pdo method calls */ $pdo->prepare($sql)->execute($customers); return true; }
  10. /* Check for Validity */ function checkDate($input) { return strtotime($input); } $format='Y/m/d' // Format to Display $valid = checkDate($input); // Check to see if user input is valid if ($valid && isset($input) && strlen($input) === 10) { $date = DateTime::createFromFormat($format, $input); } A different way in doing this
  11. Here's a procedural function that I did a long time ago demonstrating this: function insertData(array $data, $pdo, $table) { try { /* Initialize an array */ $attribute_pairs = []; /* * Set up the query using prepared states with the values of the array matching * the corresponding keys in the array * and the array keys being the prepared named placeholders. */ $sql = 'INSERT INTO ' . $table . ' (' . implode(", ", array_keys($data)) . ')'; $sql .= ' VALUES ( :' . implode(', :', array_keys($data)) . ')'; /* * Prepare the Database Table: */ $stmt = $pdo->prepare($sql); /* * Grab the corresponding values in order to * insert them into the table when the script * is executed. */ foreach ($data as $key => $value) { if($key === 'id') { continue; } // Don't include the id: $attribute_pairs[] = $value; // Assign it to an array: } return $stmt->execute($attribute_pairs); // Execute and send boolean true: } catch (PDOException $e) { /* * echo "unique index" . $e->errorInfo[1] . "<br>"; * * An error has occurred if the error number is for something that * this code is designed to handle, i.e. a duplicate index, handle it * by telling the user what was wrong with the data they submitted * failure due to a specific error number that can be recovered * from by the visitor submitting a different value * * return false; * * else the error is for something else, either due to a * programming mistake or not validating input data properly, * that the visitor cannot do anything about or needs to know about * * throw $e; * * re-throw the exception and let the next higher exception * handler, php in this case, catch and handle it */ if ($e->errorInfo[1] === 1062) { return false; } throw $e; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; // Not for a production server: } return true; } I do have to give credit to someone on a different forum help in helping me with the code, but I did rewrite the code to spruce it up and make it a little more streamline. HTH - John
  12. I will throw a monkey wrench into this discussion as I find using grids along with media queries so much easier when it comes to designing whole websites. A short example : /* Approximately the size of an iPad 768px */ @supports (grid-area: auto) { @media screen and (min-width: 48em) { .site { display: grid; grid-template-columns: 1fr; grid-template-areas: "nav" "main" "sidebar" "footer"; justify-content: center; } .nav { grid-area: nav; display: flex; justify-content: space-between; } .nav-title { display: inline-block; } .sidebar { grid-area: sidebar; } If you design for small screens (smartphones) without media queries then work you way up to larger screen devices the CSS is so much more manageable as you are using less CSS and it's easier to change to the design around to by just changing a few things around. Incorporating some Flexbox where Grids doesn't handle it so easy into the mix makes it even better.
  13. Here's another way of doing pagination : /* * Pagination Format * Read all the data from the database table in an array format */ function readData($pdo, $table, $page, $perPage, $offset) { $sql = 'SELECT * FROM ' . $table . ' WHERE page=:page ORDER BY date_added DESC LIMIT :perPage OFFSET :blogOffset'; $stmt = $pdo->prepare($sql); // Prepare the query: $stmt->execute(['perPage' => $perPage, 'blogOffset' => $offset, 'page' => $page]); // Execute the query with the supplied data: return $stmt->fetchAll(PDO::FETCH_ASSOC); } if (isset($_GET['page']) && !empty($_GET['page'])) { $current_page = urldecode($_GET['page']); } else { $current_page = 1; } $per_page = 1; // Total number of records to be displayed: /* Total number of records that NEEDS to be displayed */ $total_count = totalRecords($pdo, 'cms'); /* calculate the offset */ $offset = $per_page * ($current_page - 1); /* calculate total pages to be displayed */ $total_pages = ceil($total_count / $per_page); /* Figure out the Pagination Links */ $links = links_function('index.php', $current_page, $total_pages); /* Finally, call for the data from the database table to display */ $cms = readData($pdo, 'cms', 'blog', $per_page, $offset); The below code is just one of many ways in setting up on how to display the links : There might be a few logic bugs in it as well as this is old code: function previous_page ($current_page): bool|int { $prev = $current_page - 1; return ($prev > 0) ? $prev : false; } #[Pure] function previous_link($url="index.php", $current_page) { if(previous_page($current_page) !== false) { $links .= '<a href="' . $url . '?page=' . previous_page($current_page) . '">'; $links .= "&laquo; Previous</a>"; } return $links; } function next_page($current_page, $total_pages): bool|int { $next = $current_page + 1; return ($next <= $total_pages) ? $next : false; } #[Pure] function next_link($url="index.php", $current_page, $total_pages) { if(next_page($current_page, $total_pages) !== false) { $links .= '<a href="' . $url . '?page=' . next_page($current_page, $total_pages) . '">'; $links .= "Next &raquo;</a>"; } return $links; } #[Pure] function links_function($url="index.php", $current_page, $total_pages): string { $links .= "<div class=\"pagination\">"; $links .=previous_link($url, $current_page); // Display previous link if there are any if ($current_page <= $total_pages) { if ($current_page == 1) { $links .= "<a class='selected' href=\"$url?page=1\">1</a>"; } else { $links .= "<a href=\"$url?page=1\">1</a>"; } $i = max(2, $current_page - 5); if ($i > 2) { $links .= '<span class="three-dots">' . " ... " . '</span>'; } for (; $i < min($current_page + 6, $total_pages); $i++) { if ($current_page == $i) { $links .= "<a class='selected' href=\"$url?page=$i\">$i</a>"; } else { $links .= "<a href=\"$url?page=$i\">$i</a>"; } } if ($i !== $total_pages) { $links .= '<span class="three-dots">' . " ... " . '</span>'; } if ($i === $total_pages) { $links .= "<a href=\"$url?page=$total_pages\">$total_pages</a>"; } elseif ($i == $current_page) { $links .= "<a class='selected' href=\"$url?page=$total_pages\">$total_pages</a>"; } else { $links .= "<a href=\"$url?page=$total_pages\">$total_pages</a>"; } } $links .= next_link('index.php', $current_page, $total_pages); // Display next link if there are any $links .= "</div>"; return $links; }
  14. $sql = "SELECT * FROM agencies_data WHERE agency_user =:username"; $stmt = $db->prepare($sql); // $db is the PDO Connection $stmt->execute([':username' => $_POST['user']); // I am 99 percenct certain : in :username is not needed / just for clarity. while ($row = $stmt->fetch(PDO::FETCH_ASSOC) { // Process results though I do not understand having a form inside a // form that has been submitted } I would concentrate in getting PDO working then onward to the logic of what you are trying to accomplish. I would pull the record one at a time to edit if that is what you are trying to accomplish? $sql = "SELECT * FROM agencies_data WHERE agency_user =:username"; $stmt = $db->prepare($sql); // $db is the PDO Connection $stmt->execute([':username' => $_POST['user']); // I am 99 percenct certain : in :username is not needed / just for clarity. $result = $stmt->fetch(PDO::FETCH_ASSOC) <input class="form-control" type="text" name="oid" value="<?= $result['agency_user'] ?>" > though that would mean you have to modify the code somewhere else.
  15. A good website to understand PDO that I found is this one -> https://phpdelusions.net/pdo and I suggest creating a PHP sandbox to play around with PDO. After reading this thread I think you're concentrating too much on the mysql and other databases (which PDO can handle) than PHP PDO? I have been using PDO for sometime now, but I don't think it's easier to learn than mysqli, though once learned it is more versatile.
  16. I have gotten inspiration out of this person that I found on CodePen - https://codepen.io/annastasshia especially this pen https://codepen.io/annastasshia/pen/YzpEajJ in the past. I no longer used my modification of that code, but for something that you are doing I think would fit very well as I easily modified to include pagination. https://github.com/Strider64/phototechguru/blob/master/photogallery.php
  17. I have been coding in PHP for about 7 or 8 years, but have been programming since the early 1980s. I still remember COBOL with punch cards and I believe Apple Basic was my first programming language.
  18. Here's my version though I believe you need PHP 8.0 to use it? (Though I could be wrong) https://github.com/Strider64/phototechguru/blob/master/src/Database.php The to use it then simply do the following: public static function fetch_by_column_name($sql) { $stmt = Database::pdo()->prepare($sql); // Database::pdo() is the PDO Connection $stmt->execute([ static::$searchItem => static::$searchValue ]); return $stmt->fetch(PDO::FETCH_ASSOC); }
  19. Should be if ($_SERVER['REQUEST_METHOD'] === 'POST')
  20. It's looks like you are trying to do too many things at the same time. What I can guess is your are trying to do a contact page that sends email, pagination and accessing a database table? Though that is only a guess. My suggestion would to take you one thing at a time and start of basic. Learn the fundamentals of PHP and I know it can be boring, but it gives you a solid foundation on coding in PHP. I have been using PHP for a long time now and I still learn new things from online tutorials and from people who know more than me about PHP. There are plenty of tutorials just make sure that it's a current tutorial. Like already stated if you know the problem then show us that section of the code or HTML markup with the code. Though just looking at what you shown us, a configuration file at the top of the page would help you out for a lot of things and prevent you from typing in repetitive code.
  21. Maybe the following will help: <?php session_start(); //unset($_SESSION['vehicle']); //die(); if (!isset($_SESSION)) { $_SESSION['vehicle'] = null; // Pretend coming from a database table: } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_SESSION['vehicle'] = $_POST['vehicle']; //echo "<pre>" . print_r($_POST['vehicle'], 1) . "</pre>"; } ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Checkboxes</title> </head> <body> <form method="post"> <label for="yacht" class="vehicle"><?= $_SESSION['vehicle']['yacht'] ? 'I own a Yacht' : 'Yacht'?></label> <input id="yacht" type="checkbox" name="vehicle[yacht]" value="yacht" <?php echo ($_SESSION['vehicle']['yacht']) ? 'checked' : NULL; ?>> <br> <label for="sportsCar" class="vehicle"><?= $_SESSION['vehicle']['sportsCar'] ? 'I own a Porsche 911' : 'Sports Car'?></label> <input id="sportsCar" type="checkbox" name="vehicle[sportsCar]" value="sportsCar" <?php echo ($_SESSION['vehicle']['sportsCar']) ? 'checked' : NULL; ?>> <br> <label for="plane" class="vehicle" ><?= $_SESSION['vehicle']['plane'] ? 'I own a Cessna Jet' : 'Plane'?></label> <input id="plane" type="checkbox" name="vehicle[plane]" value="plane" <?php echo ($_SESSION['vehicle']['plane']) ? 'checked' : NULL; ?>> <br> <button class="submitButton" type="submit" name="submit" value="enter">submit</button> </form> </body> </html>
  22. I don't like the yellow text and text directly on an image. I like the image of the foxes, but the I don't care for centered text (should be left-aligned in my opinion), plus heading should be higher with it not directly on the image. I'm assuming this website is just being constructed? If that is the case changes shouldn't be too hard to implement once you get enough feed back. I would also design for smartphones first and work you way out to larger screens with it comes to responsive design. Though I didn't see any grids and flex was limit in scope? I would look at other websites to get better ideas for this website. That's what I try do for my own.
  23. My advice take about an hour or two to learn PDO and here's a good link that I still use sometime - https://phpdelusions.net/pdo It will save you a lot of time in the long run. I would also separate the logic as it will also help. Here's an example of my photo gallery and I start off with the display: <div class="container"> <?php $count = 0; foreach ($cms as $record) { echo '<p class="hideContent">' . $record['content'] . '</p>'; echo '<div class="' . $displayFormat[$count] . '">'; echo '<div class="gallery-item">'; echo '<div class="images"><img src="' . $record['image_path'] . '" alt="Photo1" data-exif="' . $record['Model'] . ' ' . $record['ExposureTime'] . ' ' . $record['Aperture'] . ' ' . $record['ISO'] . ' ' . $record['FocalLength'] . '" width="800" height="534">'; echo '</div>'; $count++; echo '<div class="title">' . '<h1 class="pictureHeading">' . $record['heading'] . '</h1>' . '<span class="exifInfo">' . $record['Model'] . '</span>' . '</div>'; echo '</div>'; echo '</div>'; } ?> </div> Here's retrieving the data (The PHP is at the top): if (isset($_GET['page']) && !empty($_GET['page'])) { $current_page = urldecode($_GET['page']); } else { $current_page = 1; } $per_page = 12; // Total number of records to be displayed: $total_count = CMS::countAllPage('blog'); // Total Records in the db table: /* Send the 3 variables to the Pagination class to be processed */ $pagination = new Pagination($current_page, $per_page, $total_count); /* Grab the offset (page) location from using the offset method */ $offset = $pagination->offset(); //echo "<pre>" . print_r($offset, 1) . "</pre>"; //die(); /* * Grab the data from the CMS class method *static* * and put the data into an array variable. */ $cms = CMS::page($per_page, $offset, 'blog'); My GitHub repository for this https://github.com/Strider64/phototechguru Proof that it works - https://phototechguru.com/photogallery.php This is just an example, but in my opinion keep your logic separated and using PHP PDO instead of mysqli will save you a lot of headaches in the long run, but do what you want with what I posted. I'm more into photography than developing lately and with warmer weather arriving it will only get worse. 🤣
  24. and make sure you are inserting all the data if that is the whole script....
  25. The only time I use include is if I have multiple pages, here's an example -> <?php include_once "assets/includes/inc.nav.php"; ?> Here's the HTML though it has a little php in it: <div class="nav"> <input type="checkbox" id="nav-check"> <h3 class="nav-title">&nbsp;</h3> <div class="nav-btn"> <label for="nav-check"> <span></span> <span></span> <span></span> </label> </div> <div class="nav-links"> <a href="index.php">Home</a> <a href="photogallery.php">Gallery</a> <a href="/admin/index.php">Admin</a> <a href="game.php">Quiz</a> <a href="contact.php">Contact</a> <?php if (isset($_SESSION['id'])) { echo '<a href="/admin/logout.php">Logout</a>'; } ?> </div> </div> for configuration files I do this: require_once 'assets/config/config.php'; require_once "vendor/autoload.php"; and put them at the top.
×
×
  • 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.