Jump to content

upload the text file & fetch data


shumonira

Recommended Posts

Hi I have a student form. 

<?php 
   $fonts = "verdana";
   $fontcolor = "#3A9BE8";
   $bgcolor = "#faaa3c";
?>

<!doctype html>
<html>
<head>
   <title>Php Syntax</title>
   <style>
      body{
         font-family:<?php echo $fonts;?>
         
      }
      .phpcoding{
         width: 900px;
         margin: 0 auto;
         background: #ddd;
         
         min-height:400px;
      }
      
      .headeroption, .footeroption{
         background:#444;
         color:<?php echo $fontcolor; ?>;
         text-align: center;
         padding: 20px;
      }
      .headeroption h2, footeroption h2{
         margin:0;
      }
      .maincontent{
         min-height:400px;
         padding: 20px;
         background:<?php echo $bgcolor;?>
      }
   </style>
</head>
<body>

<div class="phpcoding">
   <section class="headeroption">
      <h2>Student Data</h2>
   </section>
   
   <section class="maincontent">      
      <div class = "student_data">
         <form action = "" method = "post">
            <table>
               <tr>
                  <td>Name</td>
                  <td><input type = "text" name = "name" /></td>
               </tr>
               
               <tr>
                  <td>City</td>
                  <td><input type = "text" name = "city" /></td>
               </tr>
               
               <tr>
                  <td>Exam Date</td>
                  <td><input type = "date" name = "examDate" /></td>
               </tr>
               
               <tr>
                  <td>Browse to upload file... </br></td>
                  <td><input type = "file" name = "browseFile" /></td>
               </tr>                           
               
               <tr>
                  <td></br></td>
                  <td><input type = "submit" name = "uploadFile" value = "Upload"/></td>
               </tr>   
               <tr>
                  <td></br></td>
                  <td><input type = "submit" name = "save" value = "Save"/></td>
               </tr>                  
            </table>
         </form>
      </div>
   </section>
   
   <section class="footeroption">
      <h4> <?php echo "&copy All rights reserved" ?></h4>
   </section>
      
</div>

</body>
</html>

I have a text file "marks.txt". the text file looks like this. 3 terms marks are there for Math, Economics & English. 

 

Math

80
55
90
Economics
59
22
60
English
83
68
76
I want to upload the data from this text file to MySQL table. I just need the numbers, so how can I avoid the String (Math, Economics and English).
please help me. I don't know how to get data from text file.
Link to comment
Share on other sites

If you are storing the data in a database table then you do not want to ignore the subject, you want to store it with the marks.

 

Something like this will do it

//
// CREATE THE TABLE
//
$pdo->exec("DROP TABLE IF EXISTS mark");
$sql = "CREATE TABLE mark (
            mark_id int not null auto_increment primary key,
            subject varchar(20),
            term tinyint,
            marks int 
        )";
$pdo->exec($sql);
//
// PROCESS THE DATA
//
$insertSql = "INSERT INTO mark (subject, term, marks) VALUES (?,?,?)";
$stmt = $pdo->prepare($insertSql);

$data = file('marks.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$subject='';
foreach ($data as $val) {
    if (!ctype_digit($val)) {
        $subject = $val;          // if not a number, store subject
        $term = 1;                // reset term number
    }
    else {
        $stmt->execute([$subject, $term++, $val ]);   // insert record
    }
}

results:

mysql> SELECT * FROM mark;
+---------+-----------+------+-------+
| mark_id | subject   | term | value |
+---------+-----------+------+-------+
|       1 | Math      |    1 |    80 |
|       2 | Math      |    2 |    55 |
|       3 | Math      |    3 |    90 |
|       4 | Economics |    1 |    59 |
|       5 | Economics |    2 |    22 |
|       6 | Economics |    3 |    60 |
|       7 | English   |    1 |    83 |
|       8 | English   |    2 |    68 |
|       9 | English   |    3 |    76 |
+---------+-----------+------+-------+
9 rows in set (0.00 sec)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.