vampke Posted August 13, 2008 Share Posted August 13, 2008 Hello peoples, I appologize if this is somewhat off topic here, since it's more of a lay-out problem, but I'm sure many people here know the answer (if there is one) to this question. I've wrote this script that gets (many) results from a mysql table. The results are put in html tables and a $int is continuously updated by every run of the while (mysql_fetch_array) loop. This works fine. Now I would like to get this $int to the very top of the html page. This is a bit of a pickle... If it were simple html, I would just move the code to the top of the page, but this is not possible since the results are only obtained at the end of the page. One option would be to put the tables in a string until I get the result, then printing first the result and then the tables, but considering the large number of data this may not be the best solution (or is it?). I manage to get it on top using a fixed floating div, but this stays fixed and floating when scrollig down. I was wondering if someone knows a better way to do this... Thanks, v. Quote Link to comment https://forums.phpfreaks.com/topic/119537-how-to-get-results-to-the-top-of-the-html-page/ Share on other sites More sharing options...
ignace Posted August 13, 2008 Share Posted August 13, 2008 use a function that creates your table and returns it, store this in a variable so you get some more flexibility (like being possible to use the $int variable) if you wan't it on a specific place i suggest using a placeholder using str_replace() you can then replace the placeholder with the value of your $int variable Quote Link to comment https://forums.phpfreaks.com/topic/119537-how-to-get-results-to-the-top-of-the-html-page/#findComment-615811 Share on other sites More sharing options...
vampke Posted August 13, 2008 Author Share Posted August 13, 2008 use a function that creates your table and returns it, store this in a variable so you get some more flexibility (like being possible to use the $int variable) if you wan't it on a specific place i suggest using a placeholder using str_replace() you can then replace the placeholder with the value of your $int variable Hi, thanks for your quick response. I already thought of the variable option (it was what I meant with "string") The problem is that it is not just one table, there are actually many. The generated html file actually contains more than 13,000 lines of code :/. I was hoping there would be a better solution Quote Link to comment https://forums.phpfreaks.com/topic/119537-how-to-get-results-to-the-top-of-the-html-page/#findComment-615820 Share on other sites More sharing options...
ignace Posted August 13, 2008 Share Posted August 13, 2008 post your code and we may be able to assist you further Quote Link to comment https://forums.phpfreaks.com/topic/119537-how-to-get-results-to-the-top-of-the-html-page/#findComment-615847 Share on other sites More sharing options...
Psycho Posted August 13, 2008 Share Posted August 13, 2008 The simple solution is to instead of writing the table to the page as you create it you can build the table as a variable. Then when the process ends you can echo out the "totals" and then the content. Example: <?php while ($record = mysql_fetch_assoc($result)) { $tableContent .= "<tr>\n"; $tableContent .= "<td>{$record['name']}</td>\n"; $tableContent .= "<td>{$record['age']}</td>\n"; $tableContent .= "<td>{$record['salary']}</td>\n"; $tableContent .= "<tr>\n"; $count++; $totalAge += $record['age']; $totalSalary += $record['salary']; } //Show the totals echo "Number of staff: " . $count . "<br>\n"; echo "Average Age: " . ($totalAge/$count) . "<br>\n"; echo "Average Salary: " . ($totalSalary/$count) . "<br>\n"; //Show the content echo "<table>\n"; echo "<tr>\n"; echo "<th>Name</th>\n"; echo "<th>Age</th>\n"; echo "<th>Salary</th>\n"; echo "</tr>\n"; echo $tableContent; echo "</table>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/119537-how-to-get-results-to-the-top-of-the-html-page/#findComment-615865 Share on other sites More sharing options...
vampke Posted August 13, 2008 Author Share Posted August 13, 2008 ok, i have oversimplified this, because the code is quite long to post here, but you'll catch the drift i hope: $get_num = "SELECT * FROM numbers"; $get_num_res = mysql_query($get_num,$conn) or die(mysql_error()); $int=0; $display_block ="<html><body>"; while ($numresult = mysql_fetch_array($get_num_res)) { $drNumber = array( $numresult['number1'], $numresult['number2'], $numresult['number3'], $numresult['number4'], $numresult['number5'] ); $display_block .= "<table>\n<tr>\n"; foreach ($drNumber as $value){ $int += $value; $display_block .= "<td class=\"number\">" . $value . "</td>\n"; } } $display_block .= "</tr></table>\n"; //$int would need to appear at the top of the page! $display_block .= "<p>" . $int . "</p>"; $display_block .= "</body>\n</html>"; echo $display_block; The variable option seems valid, but as i said, I generate about 13000 lines of html code before i get to the $int. I'm afraid this might generate problems Quote Link to comment https://forums.phpfreaks.com/topic/119537-how-to-get-results-to-the-top-of-the-html-page/#findComment-615868 Share on other sites More sharing options...
ignace Posted August 13, 2008 Share Posted August 13, 2008 separate your business logic (fetching data, processing..) from your presentation logic (html, css, ..) it is good practice and will avoid problems like you are currently experiencing or simple solution use thead, tbody, tfoot <table> <tbody> <tr> <td>blabla</td> </tr> </tbody> <thead> <tr> <td>on top</td> </tr> </thead> </table> however i highly discourage such usage, separating logic is considered a much better practice Quote Link to comment https://forums.phpfreaks.com/topic/119537-how-to-get-results-to-the-top-of-the-html-page/#findComment-615894 Share on other sites More sharing options...
Psycho Posted August 13, 2008 Share Posted August 13, 2008 <?php $get_num = "SELECT number1, number2, number3, number4, number5 FROM numbers"; $get_num_res = mysql_query($get_num,$conn) or die(mysql_error()); $int=0; while ($numresult = mysql_fetch_array($get_num_res)) { $int += array_sum($numresult); $display_block .= "<tr>\n<td>\n" . implode("</td>\n<td>\n", $numresult) . "</td>\n<\tr>\n"; } ?> <html> <body> <p><?php echo $int; ?></p> <table> <?php echo $display_block; ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/119537-how-to-get-results-to-the-top-of-the-html-page/#findComment-615926 Share on other sites More sharing options...
vampke Posted August 14, 2008 Author Share Posted August 14, 2008 okay, thanks guys, I guess there is no alternative then using a variable to store the output and then returning this at the end of the script. I did not know that table headers could be defined at the end of the table! Thanks for this new info I'm still wondering if this will not generate problems, considering the thousands of lines of html that needs to be stored in the variable. Can anyone give me an idea on this? Quote Link to comment https://forums.phpfreaks.com/topic/119537-how-to-get-results-to-the-top-of-the-html-page/#findComment-616276 Share on other sites More sharing options...
Psycho Posted August 14, 2008 Share Posted August 14, 2008 There are ALWAYS different solutions. For example you could simply run a query to get the results and then another to get the records. Althoug I agree that separating your logic and content makes the most sense, here is an alternative: <?php $sum_query = "SELECT SUM(number1+number2+number3+number4+number5) FROM numbers"; $sum_result = mysql_query($sum_query, $conn) or die(mysql_error()); $sum_record = mysql_fetch_array($sum_result); ?> <html> <body> <p><?php echo $sum_record[0]; ?></p> <table> <?php $get_num = "SELECT number1, number2, number3, number4, number5 FROM numbers"; $get_num_res = mysql_query($get_num,$conn) or die(mysql_error()); while ($numresult = mysql_fetch_array($get_num_res)) { echo "<tr>\n"; foreach ($numresult as $value) { echo "<td>$value</td>\n } echo "<\tr>\n"; } ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/119537-how-to-get-results-to-the-top-of-the-html-page/#findComment-616688 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.