While and If Statements

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
bldb
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Nov 09, 2017 11:46 am

Sat Nov 11, 2017 5:31 pm

My working code looked like this:

Code: Select all

<?php
 
  $students = "student.txt"; // text file for students and student number
 
  //converting a string into a variable
 
  $name = $_POST["name"];
  $number = $_POST["snumber"];
 
  $coursedata = "course.txt"; // text file coursedata
  $cfile = fopen ($coursedata, 'r');
 
  while ($line = fgets ($cfile)) {
    $drop = explode ('||', trim ($line));
 
  } // end of course while
 
    fclose ($cfile);
 
  //open student file and explode into an array
 
  $sfile = fopen($students, 'r') or die ("Student file does not exist");
 
  $found = 0;
 
  while ($sline = fgets ($sfile)) {
    $list = explode(",",trim ($sline));
 
    //test array against text input
 
    if ($name == $list[0] && $number == $list[1]) {
 
      $found = 1;
 
      //load number and course selected into load.txt;
 
      $handle = fopen ('load.txt', 'a');
      fwrite($handle, "$number, $drop[1] \n");
      fclose($handle);
 
      include 'load.txt';
 
      break;
 
  } 
 
  } // end of while
 
  if (!$found) {
 
    include 'index.php';
 
  } // end of found i
 
  fclose($sfile);
 
  ?>
  
  
When the user entered in their name, student name and selected a course it would take them to the code listed about. Then I was able to get the code to write the student number and the course code into a text file called load.php and load that to enrolled.php. So my next step was to get it so that way the same student couldn't register for a course they are already enrolled in. However when I entered in the code below it wouldn't display anything. Anyone have any suggestions?

Code: Select all

<?php
 
  $students = "student.txt"; // text file for students and student number
 
  //converting a string into a variable
 
  $name = $_POST["name"];
  $number = $_POST["snumber"];
 
  $coursedata = "course.txt"; // text file coursedata
  $cfile = fopen ($coursedata, 'r');
 
  while ($line = fgets ($cfile)) {
    $drop = explode ('||', trim ($line));
 
  } // end of course while
 
    fclose ($cfile);
 
  //open student file and explode into an array
 
  $sfile = fopen($students, 'r') or die ("Student file does not exist");
 
  $found = 0;
 
  while ($sline = fgets ($sfile)) {
    $list = explode(",",trim ($sline));
 
    //test array against text input
 
    if ($name == $list[0] && $number == $list[1]) {
 
      $found = 1;
 
      //load number and course selected into load.txt;
 
      $handle = fopen ('load.txt', 'a');
 
      while ($loadf = fgets ($handle)) {
        $data = explode (',', $loadf);
 
          if ($number == $data[0] && $drop[1] != $data[1]) {
 
              fwrite($handle, "$number, $drop[1] \n");
 
        } elseif ($number == $data[0] && $drop[1] == $data[1]) {
 
          echo  "Student is already registered for this course";
 
        } //end of else if
 
      include 'load.txt';
 
      break;
 
    } // end of while
 
    fclose($handle);
 
  } // end of if
 
  } // end of while
 
  if (!$found) {
 
    include 'index.php';
 
  } // end of found i
 
  fclose($sfile);
 
  ?>

Quote
MultiQuote
Edit

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 525
Joined: Mon Feb 22, 2016 5:52 pm

Sun Nov 12, 2017 3:02 am

What do you expect this to do?

Code: Select all

while ($line = fgets ($cfile)) {
    $drop = explode ('||', trim ($line));
 
  } 
And now having re-read it, what does it actually do?

bldb
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Nov 09, 2017 11:46 am

Sun Nov 12, 2017 3:00 pm

In my index.php script where I created a form, that line reads course.txt and turns it into an array so it can be read as course name, course code and the number of seats available. Like this:

English||COMP-1004||4
Web Development||COMP-6002||5
Object-Oriented Java||COMP-1006||3
Networking Essentials||COMP-1035||4
Dynamic Websites AMP||COMP-3006||2

In enrolled.php I wasn't able to access that array from index.php so I included it in enrolled.php

bldb
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Nov 09, 2017 11:46 am

Sun Nov 12, 2017 3:31 pm

I have changed my code on enrolled.php a little bit so now the explode of the courses are gone.

Based on the code below what should I use in order to check if the course has already be written to load.txt?

Code: Select all

<?php

$students = "student.txt"; // text file for students and student number

//converting a string into a variable

$name = $_POST["name"];
$number = $_POST["snumber"];
$course = $_POST["pcourse"];

//open student file and explode into an array

$sfile = fopen($students, 'r') or die ("Student file does not exist");

$found = 0;

// turning students into an array to read

while ($sline = fgets ($sfile)) {
  $list = explode(",",trim($sline));

  //test array against text input

  if ($name == $list[0] && $number == $list[1]) {

    $found = 1;

    //load number and course selected into load.txt;

    $handle = fopen ('load.txt', 'r');
    while ($loadf = fgets ($handle)) {
      $data = explode (',', trim($loadf));

  } // end of while 32

  fclose ($handle);

    $handle = fopen('load.txt', 'a');
    fwrite ($handle, "$name, $course \n");

  fclose ($handle);

    include 'load.txt';

    break;

  } // end of if 34

} // end of while

if (!$found) {

  include 'index.php';
}

fclose($sfile);

?>

bldb
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Nov 09, 2017 11:46 am

Sun Nov 12, 2017 3:32 pm

Heres my form on index.php

Code: Select all

<!DOCTYPE html>
<html>
  <body>
    <h1>Course Selection</h1><br>


    <form action="enrolled.php" method="post">


              Name: <input type="text" name="name" placeholder="Name" required="required" maxlength="50">
              <br><br>

              Student Number: <input type="text" name= "snumber" required="required" maxlength="9">
              <br><br>

        <?php
        //form

      $coursedata = "course.txt"; // text file coursedata
      echo "Select a course: <select name = \"pcourse\">\n";

      $cfile = fopen ($coursedata, 'r') or die ("File does not exist or you do not have permission");

      while ($line = fgets ($cfile)) {
        $drop = explode ('||', $line);

      echo " <option value =\"$drop[1]\">$drop[0] $drop[1] $drop[2]</option>\n";


      }

      fclose ($cfile);

      echo " </select>\n";


      ?>

      <br><br>
      <input type = "submit" value="submit" name= "submit">

    </form>

    </body>
    </html>

Post Reply