Posts

Showing posts from November, 2023

Read a line from text file in PHP

Image
      Reading a specific line from text file and display it in HTML Page   This blog demonstrates how to read a specific row from a text file and display the details in an HTML page using PHP.  You can find the demo1.php, states.txt files in the previous tutorials.   demo1.php   states.txt  demo2.php <html> <head> <title>Coding-zon tutorials</title> </head>   <body bgcolor=""> <center> <br /><br /><br /><br /> <?php print "<br> <h2>State Information</h2><br />" ;   $file = file ( 'states.txt' ); $row = "" ; if ( isset ( $_POST [ 'submit' ])) { $stateID = $_POST [ 'stateID' ]; //user selects from downdown foreach ( $file as $row ){ $cols = explode ( ';' , $row );    if ( $cols [ 0 ] == $stateID ){ echo "<table border='1'>" ; echo "<tr><td>State Name :</td><

How to read txt file into Html Dropdown

Image
Read content/data from text file and write it into HTML Dropdown           This blog demonstrates how to populate a dropdown list with values from a text file using PHP. This tutorial uses two files.   1. states.txt : The text file contains a set of rows. Each row contains a state information such as  stateID, stateName , stateCapital , statePopulation   etc. and each value in a row, separated with semicolon . Other variations are possible to store states info ex: CSV, but in this tutorial our goal is reading a text file and populating data in an HTML dropdown. The first line in the states.txt file contains the columns names. The next five rows are the data. As shown below, link. states.txt  2. demo1.php : Displays text data in a dropdown.  There are 3 steps implemented in this file step1 -  Reads the sates text file and stores states details in an array. step2 - There is an HTML form with one dropdown. All array data is loaded in a dropdown using foreach loop. step-3 - User selec