bionicsamir Posted December 21, 2019 Share Posted December 21, 2019 Hi All, I am a newbie to php and I have this issue. I would appreciate if anyone can help out. I tried a few things but was not able to come up with a solution. From reading few codes and learning from it, I created this function in query.php and am calling this function in uk.php file, but the problem is that it loads and shows the data in table, but then it stops loading page from there on and the footer which is in html code after the place where I am calling this function does not load. I tried removing return false; command, but with that it loads the footer, but it shows the table after the footer. I want the table with data to show in between header and footer. Can you guys let me know what I need to do to achieve that? File Name: query.php function uk() { $output = ''; $result = db_query("SELECT * FROM lecture where groups='uk'"); if(mysqli_num_rows($result) > 0) { $output .= '<div id="style2" style="overflow-x:auto;"> <table> <tr> <th class="text-center">Date</th> <th class="text-center">Title</th> <th class="text-center">Venue</th> <th class="text-center">Duration</th> <th class="text-center">Size (MB)</th> <th class="text-center">Link</th> </tr>'; while($row = mysqli_fetch_array($result)) { $output .= ' <tr> <td>'.$row["mydate"].'</td> <td>'.$row["title"].'</td> <td>'.$row["venue"].'</td> <td>'.$row["duration"].'</td> <td>'.$row["size"].'</td> <td><a href="../'.$row["path"].$row["file_name"].'">Save</a></td> </tr> '; } echo $output; } else { echo 'Data Not Found'; } } File Name: uk.php Header html code here Some body text <?php include '../includes/query.php'; uk(); return false; ?> Footer html code here Quote Link to comment https://forums.phpfreaks.com/topic/309725-php-function-call-in-html-stops-loading-footer-or-loads-below-footer/ Share on other sites More sharing options...
bionicsamir Posted December 21, 2019 Author Share Posted December 21, 2019 I fixed it.. was missing </table> Quote Link to comment https://forums.phpfreaks.com/topic/309725-php-function-call-in-html-stops-loading-footer-or-loads-below-footer/#findComment-1572738 Share on other sites More sharing options...
benanamen Posted December 21, 2019 Share Posted December 21, 2019 (edited) A function also known as a method when in a Class should do one thing and do it well. It should also be easily re-usable and not have to be edited to use it elsewhere. What you have is none of that. What you will probably discover as the first problem is that you have hard coded the groups which means if you want a group other than uk you now have a problem on your hands. Duplicating the function for every group is not the answer. What you want to do is pass the groups value to the function as a parameter. Now you can use the same function for any group. Same thing goes for the hard-coded table name. There is also no option to specify specific columns. Again you would run into problems with the hard coded column headers and every other hard coded item. Here is something you get you going towards a dynamic solution. (It's not a cut n paste solution) <?php declare(strict_types=1); $sql = 'YOUR-QUERY-HERE'; $stmt = $db->pdoQuery($sql); $row = $stmt->fetch(PDO::FETCH_ASSOC); if (!$row) { echo '<H1>No Records Available</H1>'; } else { $data = []; foreach ($row as $key => $val) { $data[ucwords(str_replace('_', ' ', $key))] = $val; } ?> <div class="table-responsive"> <table id="myDataTable" class="table table-bordered table-striped table-hover table-sm"> <thead class="thead-dark"> <tr> <th><?= join('</th><th>', array_keys($data)) . "</th>\n" ?> </tr> </thead> <tbody class="searchable"> <?php do { echo "<tr>\n<td>" . join("</td>\n<td>", $row) . "</td>\n"; echo " </tr> \n"; } while ($row = $stmt->fetch(PDO::FETCH_ASSOC)); ?> </tbody> </table> </div> <?php } Edited December 21, 2019 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/309725-php-function-call-in-html-stops-loading-footer-or-loads-below-footer/#findComment-1572739 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.