Your full service technology partner! 
-Collapse +Expand
Search PHP Group:

-Collapse +Expand PHP Store

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...PHP   Print This    All Groups  
The Workings of a Simple PHP Scrolling List
Posted 7 years ago on 5/16/2014 and updated 12/13/2020
Take Away:

This article shows how PHP can be used to access a MySQL database to retrieve data for a scroll list. Here, I will discuss one that lists 12 rows at a time on a web page.



I am going to discuss the workings of a scroll list of data table records that can be displayed on a web page using PHP scripting code. The programming code will list 12 records at a time from a data table contained within a MySQL database. It can scroll forwards and backwards by the same number of rows by clicking the labeled buttons above the list, Next and Previous. This is a simple to use utility and it works in a clean and seamless manner.


In this patch of code, you can see the how the data table row counter is advanced forward by 12 rows in the code enclosed in the if-then data structure beginning with “if ($HTTP_POST_VARS[‘submit_page_forward‘])”. And it will go backwards by the same number in the programming under the if-then data structure starting with “if ($HTTP_POST_VARS[‘submit_page_backward‘])”. The PHP session variable, $_SESSION[start_row]”, holds the first of the next 12 data records to be displayed in the scroll list. “$_SESSION[totalrecords]” is another session variable that contains the total number of records in the data table that is used to populate the scroll list. It is used to detect the end of the scroll list used with the forward scrolling function mentioned before.

  1. session_start();    
  2. // include the file containing log in constants.
  3. include‘generic_config.php‘;  
  4. // redirect to home page.
  5. if($_POST[‘submit_return_to‘])   
  6. {  
  7. header("Location:");    
  8. }  
  9. // redirect to comment posting page.
  10. if($_POST[‘submit_add_feedback‘])   
  11. {  
  12. header("Location:");    
  13. }  
  14. // scroll forward 12 rows.
  15. if($HTTP_POST_VARS[‘submit_page_forward‘])   
  16. {  
  17. $_SESSION[start_row] = $_SESSION[start_row] + 12;  
  18. if( $_SESSION[start_row] > $_SESSION[totalrecords] ) {  
  19. $_SESSION[start_row] = $_SESSION[start_row] - 12;  
  20. }  
  21. }  
  22. // scroll backward 12 rows.
  23. if($HTTP_POST_VARS[‘submit_page_backward‘])   
  24. {  
  25. $_SESSION[start_row] = $_SESSION[start_row] - 12;  
  26. if( $_SESSION[start_row] < 0 ) {  
  27. $_SESSION[start_row] = 0;  
  28. }  
  29. }  


The display of the scroll list consists of a PHP script enclosed in a HTML form. When the site visitor clicks on the Next or Previous input element buttons, the form will send the selection to the server. It will then be handled by the code in the previous section of this article.

At the beginning of the PHP code within the HTML form (see below), a connection is made to the website’s server and the MySQL database is set. The predefined constants for the server and database connection can be found in the configuration file specified near the beginning of this web page, “include ‘generic_config.php‘;”. All the records in the “customer_ feedback” data table are queried by the PHP directive, “$result=mysql_query("SELECT * FROM customer_feedback") or die(‘Could not select table‘); ”. The total number of records in the data table is retrieved by another PHP directive, “$total_records = mysql_num_rows($result);”. Then that value is assigned to a session variable used by the forward scrolling function, “$_SESSION[totalrecords] = $total_records;”. The contents of the “remarks” field from the data table is assigned to a PHP variable like this, “$remark = mysql_result($result, $num, "remarks");”. Then the variable is displayed on the scroll list like this, “echo nl2br($remark);”. This is done within a for loop data structure that repeats 12 times for each row to be displayed. Also, note how the HTML is encapsulated within the PHP scripting language. The idea is to weave a HTML table into the fabric of PHP during the display of each of the 12 rows. This weaving technique is also applied to the PHP if-endif data structure that is used to enable and disable the navigation buttons located just above the row listing. Lastly, the queried resource is freed like this, “mysql_free_result($result);”. Then the server connection is closed like so, “mysql_close($conn);”. Please click here to view the full code readout.


The scroll list I have discussed here can be easily adapted for most any task that involves displaying rows of information from a data table in a MySQL database. There are many online applications that can utilize this. I offer application development services in other programming languages besides PHP. These include C, C++, Corel Paradox, C# and other platforms. I also offer computer repair services in my area as well.


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P162A1
Enter key:
Article Contributed By NE Ohio Computer Guy:

Please visit my software developer website for more information about my services. I offer application development as well as Android app coding services. My developer skills are best suited to dealing with custom software projects. I can perform programming for Corel Paradox as well as C# and PHP.

In my local area of northeast Ohio, I can cater to computer repair and "fix my computer" issues. And don‘t forget to check out my YouTube Channel. It is full of cool videos and has something for everyone!

Use my contact web page today to reach me about any software design ideas you have.

Visit Profile

 KB Article #102648 Counter
Since 5/16/2014
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

©1995-2021 Prestwood IT Solutions.   [Security & Privacy]