slotegraafd Posted November 13, 2020 Share Posted November 13, 2020 Hi, WHAT: So I cannot for the life of me figure out why I'm getting this error. I'm trying to create a function that will display the user information from the database in a table but I have like a ton of these errors on the page and I don't know whats wrong. STEPS TO RESOLVE: So I've gone over my code a bunch of types to make sure that all the variables and what not were spelled correctly and as far as I can tell they are. I've also googled this issue to see if I can find a solution but none of them are very helpful. I honestly don't know whats wrong so kinda hard to find ways to resolve an issue, I don't even really know what this error means. THE CODE: This is where I put the function into action <?php display_table( array( "id" => "Id", "emailaddress" => "Email", "firstname" => "First Name", "lastname" => "Last Name", "salesperson" => "Salesperson", "phonenumber" => "Phone Number", "extension" => "Extension", "type" => "Type" ) ); ?> //This is the function <?php function display_table($fields, $data, $rows, $page){ if(isset($_GET['page'])){ $page = $_GET['page']; } else { $page = 1; } $firstRecord = ($page - 1) * ROWS_PER_PAGE; $pageNumbers = ceil($rows / ROWS_PER_PAGE); echo '<div class="table-responsive w-75 mx-auto py-3"> <table class="table table-dark table-bordered table-sm"> <thead> <tr>'; foreach($fields as $key){ echo '<th class="py-2">' . $key . '</th>'; } echo '</tr> </thead> </tbody>'; $keys = array_keys($fields); for($record = $firstRecord; $record < $firstRecord + ROWS_PER_PAGE; $record++){ $row = $data[$record]; echo '<tr>'; for($recordCount = 0; $recordCount < count($keys); $recordCount++){ $column = $keys[$recordCount]; echo '<td class="py-2">' . $row[$column] . '</td>'; } echo '</tr>'; } echo '</tbody> </table'; for($pages = 1; $pages <= $pageNumbers; $pages++){ echo '<a class="btn btn-dark mx-1" href=?page=' . $pages . '</a>'; } } ?> Any help/advice would be really appreciated Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/ Share on other sites More sharing options...
Barand Posted November 13, 2020 Share Posted November 13, 2020 When you call your "display_table()" function you are passing a single array parameter ($fields). Your function also expects $data, $rows and $page parameters. What happened to those? Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582318 Share on other sites More sharing options...
Strider64 Posted November 13, 2020 Share Posted November 13, 2020 I know it would mean basically starting over, but something like this is screaming pagination. Here's a small cms example using pagination for my website: /* * Pagination Code */ $current_page = htmlspecialchars($_GET['page'] ?? 1); // Current Page Location: $per_page = 3; // Total articles per page $total_count = $journal::countAll(); // Totoal articles in database table: $pagination = new Pagination($current_page, $per_page, $total_count);  Here's the Pagination Class: <?php namespace Miniature; use PDO; use Miniature\Database as DB; class Pagination extends Journal { public $currentPage; public $perPage; public $totalCount; public $result = \NULL; protected $query = \NULL; protected $stmt = \NULL; public function __construct($currentPage = 1, $perPage = 20, $totalCount = 0) { $this->currentPage = (int) $currentPage; $this->perPage = (int) $perPage; $this->totalCount = (int) $totalCount; } public function offset() { return $this->perPage * ($this->currentPage - 1); } public function totalPages() { return ceil($this->totalCount / $this->perPage); } public function previousPage() { $prev = $this->currentPage - 1; return ($prev > 0) ? $prev : false; } public function nextPage() { $next = $this->currentPage + 1; return ($next <= $this->totalPages()) ? $next : false; } public function previousLink($url = "") { $link = ""; if ($this->previousPage() != false) { $link .= "<a class=\"menuExit\" href=\"{$url}?page={$this->previousPage()}\">"; $link .= "« Previous</a>"; } return $link; } public function nextLink($url = "") { $link = ""; if ($this->nextPage() != false) { $link .= "<a class=\"menuExit\" href=\"{$url}?page={$this->nextPage()}\">"; $link .= "Next »</a>"; } return $link; } public function numberLinks($url = "") { $output = ""; for ($i = 1; $i <= $this->totalPages(); $i++) { if ($i == $this->currentPage) { $output .= "<span class=\"selected\">{$i}</span>"; } else { $output .= "<a class=\"menuExit\" href=\"{$url}?page={$i}\">{$i}</a>"; } } return $output; } public function pageLinks($url) { $output = ""; if ($this->totalPages() > 1) { $output .= "<div class=\"pagination\">"; $output .= $this->previousLink($url); $output .= $this->numberLinks($url); $output .= $this->nextLink($url); $output .= "</div>"; } return $output; } public function readPage() { $this->query = 'SELECT id, user_id, author, page, thumb_path, path, post, page, Model, ExposureTime, Aperture, ISO, FocalLength, heading, content, DATE_FORMAT(date_added, "%M %e, %Y") as date_added, date_added as myDate FROM cms ORDER BY myDate DESC LIMIT :perPage OFFSET :blogOffset'; $this->stmt = static::pdo()->prepare($this->query); // Prepare the query: $this->stmt->execute([':perPage' => $this->perPage, ':blogOffset' => $this->offset()]); // Execute the query with the supplied data: $this->result = $this->stmt->fetchAll(PDO::FETCH_OBJ); return $this->result; } } Just some food for thought or maybe some ideas on how to fix the code? Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582319 Share on other sites More sharing options...
slotegraafd Posted November 13, 2020 Author Share Posted November 13, 2020 10 hours ago, Barand said: When you call your "display_table()" function you are passing a single array parameter ($fields). Your function also expects $data, $rows and $page parameters. What happened to those? What do you mean? Cuz it does actually show the data in the table still  Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582326 Share on other sites More sharing options...
Barand Posted November 13, 2020 Share Posted November 13, 2020 52 minutes ago, slotegraafd said: What do you mean? Not sure how to say it with any more clarity. Your definition of the function has 4 parameters, namely $fields, $data, $rows and $page When you call the function you provide only the "$fields" parameter. There are no values provided for the other three. Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582328 Share on other sites More sharing options...
slotegraafd Posted November 13, 2020 Author Share Posted November 13, 2020 17 minutes ago, Barand said: Not sure how to say it with any more clarity. Your definition of the function has 4 parameters, namely $fields, $data, $rows and $page When you call the function you provide only the "$fields" parameter. There are no values provided for the other three. display_table( array( "id" => "Id", "emailaddress" => "Email", "firstname" => "First Name", "lastname" => "Last Name", "salesperson" => "Salesperson", "phonenumber" => "Phone Number", "extension" => "Extension", "type" => "Type" ), client_select_all(), client_count(), 1 ); I forgot to add the last part of this, I have those two as other functions to for the data and rows and such  Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582329 Share on other sites More sharing options...
kicken Posted November 13, 2020 Share Posted November 13, 2020 What do client_select_all() and client_count() return? Move them out of the function call and assign them to a variable, then var_dump that variable. $data = client_select_all(); var_dump($data); $rows = client_count(); var_dump($rows); display_table( array( "id" => "Id", "emailaddress" => "Email", "firstname" => "First Name", "lastname" => "Last Name", "salesperson" => "Salesperson", "phonenumber" => "Phone Number", "extension" => "Extension", "type" => "Type" ), $data, $rows, 1 ); They probably are not returning what you're expecting them too. Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582342 Share on other sites More sharing options...
kicken Posted November 14, 2020 Share Posted November 14, 2020 (edited) Your data array only contains 2 rows, but your function is written to expect a minimum of ROWS_PER_PAGE rows.  Once the loop passes the end of your data array, $row = $data[$record]; Will result in $row being NULL because the index it's trying to access doesn't exist. What you need to do is change your function to only loop until the end of the array if there are not enough rows available. So you want the end of your loop to be either $firstRecord + ROWS_PER_PAGE or $rows which ever is smallest. This can be easily determined by using the min() function. So change your for loop to $lastRecord = min($firstRecord + ROWS_PER_PAGE, $rows); for($record = $firstRecord; $record < $lastRecord; $record++){  Additionally, the function currently generates invalid HTML due to various errors in the HTML strings. You'll want to address those as well. Edited November 14, 2020 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582345 Share on other sites More sharing options...
slotegraafd Posted November 14, 2020 Author Share Posted November 14, 2020 48 minutes ago, kicken said: Your data array only contains 2 rows, but your function is written to expect a minimum of ROWS_PER_PAGE rows.  Once the loop passes the end of your data array, $row = $data[$record]; Will result in $row being NULL because the index it's trying to access doesn't exist. What you need to do is change your function to only loop until the end of the array if there are not enough rows available. So you want the end of your loop to be either $firstRecord + ROWS_PER_PAGE or $rows which ever is smallest. This can be easily determined by using the min() function. So change your for loop to $lastRecord = min($firstRecord + ROWS_PER_PAGE, $rows); for($record = $firstRecord; $record < $lastRecord; $record++){  Additionally, the function currently generates invalid HTML due to various errors in the HTML strings. You'll want to address those as well. Okay so that didnt in fact get rid of the errors I was getting but now it doesnt show any of the data Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582347 Share on other sites More sharing options...
kicken Posted November 14, 2020 Share Posted November 14, 2020 @slotegraafd then perhaps you have an additional problem or did not make the correct changes. Provide the updated code after you've made the changes. I copied your function from the original post and tested it with the sample data and changes and it works just fine for me.  Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582348 Share on other sites More sharing options...
slotegraafd Posted November 14, 2020 Author Share Posted November 14, 2020 37 minutes ago, kicken said: @slotegraafd then perhaps you have an additional problem or did not make the correct changes. Provide the updated code after you've made the changes. I copied your function from the original post and tested it with the sample data and changes and it works just fine for me.  Is this how you did it. Idk what im missing $lastRecord = min($firstRecord + ROWS_PER_PAGE, $rows); for($record = $firstRecord; $record < $lastRecord; $record++){ $row = $data[$firstRecord + ROWS_PER_PAGE]; echo '<tr>'; for($recordCount = 0; $recordCount < count($keys); $recordCount++){ $column = $keys[$recordCount]; echo '<td class="py-2">' . $row[$column] . '</td>'; } echo '</tr>'; }  Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582349 Share on other sites More sharing options...
kicken Posted November 14, 2020 Share Posted November 14, 2020 21 minutes ago, slotegraafd said: $row = $data[$firstRecord + ROWS_PER_PAGE]; That should not have been changed. Change it back to the original $row = $data[$record];  Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582351 Share on other sites More sharing options...
mac_gyver Posted November 14, 2020 Share Posted November 14, 2020 if you are trying to paginate an array of data, just use array_slice(). it takes the same offset and length values as the LIMIT clause in an sql query would. this would eliminate all this 'glue' logic. you would just foreach(){} loop over the array of data returned. this will also work correctly for a partial last slice or an initial slice with less than ROWS_PER_PAGE elements in it. if this data is coming from an sql query, why aren't you just getting the requested page of data directly in the query? Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582353 Share on other sites More sharing options...
slotegraafd Posted November 14, 2020 Author Share Posted November 14, 2020 12 hours ago, kicken said: That should not have been changed. Change it back to the original $row = $data[$record];  Everything else is right though? Cuz it still doesnt work Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582389 Share on other sites More sharing options...
kicken Posted November 14, 2020 Share Posted November 14, 2020 10 minutes ago, slotegraafd said: Cuz it still doesnt work Works for me. Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582392 Share on other sites More sharing options...
slotegraafd Posted November 14, 2020 Author Share Posted November 14, 2020 Just now, kicken said: Works for me. Okay well can you show me where exactly you put that stuff in my code so I can see if maybe i put it in the wrong spot Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582393 Share on other sites More sharing options...
kicken Posted November 14, 2020 Share Posted November 14, 2020 Click the link there and compare. Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582395 Share on other sites More sharing options...
slotegraafd Posted November 14, 2020 Author Share Posted November 14, 2020 5 minutes ago, kicken said: Click the link there and compare. Oh I see i got it thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/311699-how-to-resolve-trying-to-access-array-offset-on-value-of-type-null-error/#findComment-1582396 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.