Jump to content

CSV file read


jaArch

Recommended Posts


  1. CSV File Reading
    • Displaying CSV file: Clicking the Show Logfile.txt link at the top of the page should display the CSV records inside logfile.txt
    • Displaying CSV records formatted :
      • Clicking the Show logfile.txt Formatted should display the CSV records formatted in an HTML table is descending order (more recent records are at the top).
      • Use this function to convert CSV records back to an array : fgetcsv()

     

 

 

I have most of this done and the problem I'm having is trying to read what  I put into the file. (I hard coded what should go into the file). Could you tell me what I'm doing wrong and what I should do to fix it?

 

 <?

if ($_POST['_act'] = 'csv'):
$fp = fopen('logfile.txt', 'a');
while (($data = fgetcsv($fp, 1000, ",")) !== FALSE){
	print_r($data);
}
fclose($fp);
endif; 

 if ($_SERVER['REQUEST_METHOD'] == 'POST'):

		if (empty($_POST['fullname'])):
			$errMsg['fullname'] = "Please fill in Your Full Name.";
		endif;
	if (ereg("'Mr\. '", $_POST['fullname']) == false):
			$errMsg['fullname'] = "Please fill in Your Full Name.";
		endif;
		print_r($errMsgs); echo count($errMsgs);

 if (count($disperrMsgs > 0)):
 $dispErrMsgs = true;
 else:
 $fp = fopen('logfile.txt', 'a');
 $fputcsv($fp, array("test", "lol", "roool"));
 fclose($fp);


 $dispSuccessMsg = true;
 endif;


endif;	 
?>

<html>
<head>


<style type="text/css">
h1 {color:red}
div.error {border: 1px solid red; margin: 20px; padding: 20px; width: 400px}
table {border: 1px solid #CCC; margin: 20px; border-collapse: collapse;}
td, th {border: 1px solid #DDD; padding: 2px}
th {background-color:#363; color: white}
td.error {color:red}
div.success  {border: 1px solid green; margin: 20px; padding: 20px; width: 400px; color:green}
</style>
</head>
<body>  
<h1> Form Validation Lab with  Reg Expressions  </h1>
<p><a href="<?= $_SERVER['PHP_SELF'] ?> ">Refresh This Page</a> | <a href="./logfile.txt">Show Logfile.txt</a> |  
<a href="<?= $_SERVER['PHP_SELF'] ?>?_act=csv">Show logfile.txt Formatted</a>  
    <a href="<?= $_SERVER['PHP_SELF'] ?>?_act=clear">Clear logfile.txt </a></p>
    
<? if ($dispErrMsgs): ?>
<div class="error">
<p>There are errors in the code: </p>
    <ol>
    <? foreach($errMsg as $errMsgs ): ?>
        <li><?= $errMsgs ?></li>
            <? endforeach; ?>
            </ol>
    </div>
<? endif; ?>
<? if ($dispSuccessMsg): ?>
<div class="success">
<p>Thank you for your submission. </p>
</div>
<? endif; ?>


<form action="<?= $_SERVER['PHP_SELF'] ?>"  name="getstuff" method="post" >
  <input type="hidden" name="_act" value="post">
  <table cellspacing = "0">
    <tr>
      <th width="78">Full Name:</th>
      <td width="184"><input name="fullname" type="text" class="textbox" 
      value="<? $_POST['fullname']; ?>" size="20" ></td>
      <td width="626" >Salution of Mr. or Mrs. followed by two text strings separated by any number of spaces. </td>
      </tr>

    <tr>
      <th>Street:</th>
      <td><input name="street" type="text" class="textbox" 
      value="<? $_POST['street']; ?>" size="30"></td>
      <td >2 or 3 digit number followed by a text string ending with Street or Road separated by any number of spaces. </td>
    <tr>
      <th>Phone:</th>
      <td><input name="phone" type="text" class="textbox" 
      value="<? $_POST['phone']; ?>" size="20"></td>

      <td >10 digits, first 3 digits have optional parentheses, either side of digits 456 are optional space, dot or hyphen. </td>
    </tr>
    <tr>
      <th>PostalCode:</th>
      <td><input name="postcode" type="text" class="textbox" 
      value="<? $_POST['postcode']; ?>" size="8" maxlength="8"></td>
      <td > Postal Code: 
        Char Char Digit Hyphen/space Char Digit Digit (No XYZ or 0's. Case insensitive. )</td>
    </tr>

    <tr>
      <th>Email:</th>
      <td><input name="email" type="text" class="textbox" 
      		value="<? $_POST['email']; ?>" size="25" maxlength="40"></td>
      <td > Must accept as
        a minimum : a@b.com</td>
     </tr>
  </table>
  <input name="reset" type='button' class="button" value='RESET' onClick="location.href = location.href">

  <input name="_submit" type="submit" class="button" value="Submit me now!!!" >
</form>
<p id="notice">Everything below here is for testing purposes Don't remove this from your lab
  as I will use these buttons to fill your form with good and bad data as I mark
  it.</p>
<script>
az=document.getstuff;
function goodstuff(){
	az.fullname.value=" Mr. Joe Smith ";
	az.street.value="135 Fennell Road ";
	az.phone.value="905-575.1212";
	az.postcode.value="Ln9-T23";
	az.email.value="jsmith@gov.ca";

}
function goodstuff2(){
	az.fullname.value=" Mrs. Josephine Smith ";
	az.street.value=" 13  Fennell Street";
	az.phone.value=" (905)-575.1212 ";
	az.postcode.value=" Ln9 T23";
	az.email.value=" jsmith@gov.ca  ";

}
function badstuff(){
	az.fullname.value="Miss Josephine Smith";
	az.street.value="1 Somewhere Avenue";
	az.phone.value="905575122";
	az.postcode.value="Z8N 3T2";
	az.email.value="jsmith$gov#ca";

}
function badstuff2(){
	az.fullname.value=" Joe Smith ";
	az.street.value="  1392  Fennell Street";
	az.phone.value="[905] 575-1212";
	az.postcode.value=" Ly9 T20";
	az.email.value=" @jsmith$gov#ca";

}
function goodandbad(){
	az.fullname.value="Mrs. Joe";
	az.street.value=" 135 Fennell Avenue West";
	az.phone.value="905:5751212";
	az.postcode.value=" L99-T23";
	az.email.value=" jsmith@.gov.ca  ";

}

</script>
<form>
  <input type=button class="button" onClick="goodstuff();" value="Fill form with good values " length=100 >
  <input type=button class="button" onClick="goodstuff2();" value="Fill form with good values 2 " >

  <input type=button class="button" onClick="badstuff();" value="Fill form with BAD values ">
  <input type=button class="button" onClick="badstuff2();" value="Fill form with BAD values 2 ">
  
  <input type=button class="button" onClick="goodandbad();" value="Fill form with Good and BAD values 2">

  <br>
</form>


</BODY>
</HTML>

Link to comment
Share on other sites

Thanks! That works. But for some reason it only seems to work whenever I put it into teh file without hardcoding it.

 

But when I hard code like this:

 

	 $fp = fopen('logfile.txt', 'r');
 $fputcsv($fp, array("test", "lol", "roool"));
 fclose($fp);

 

It doesn't seem to work. Am I doing something wrong?

Link to comment
Share on other sites

Hmm, another problem I'm having is when I enter in valid data on the form the error message appears when it should only display the success message.

 

Is there a reason for this? I can't seem to figure this part out

 

<?

if ($_POST['_act'] = 'csv'):
$fp = fopen('logfile.txt', 'r');
while (($data = fgetcsv($fp, 1000, ",")) !== FALSE){
	print_r($data);
}
fclose($fp);
endif; 

 if ($_SERVER['REQUEST_METHOD'] == 'POST'):

		if (empty($_POST['fullname'])):
			$errMsg['fullname'] = "Please fill in Your Full Name.";
		endif;
	if (ereg("'Mr\. '", $_POST['fullname']) == false):
			$errMsg['fullname'] = "Please fill in Your Full Name.";
		endif;
		print_r($errMsgs); echo count($errMsgs);

 if (count($disperrMsgs > 0)):
 $dispErrMsgs = true;
 else:
 $fp = fopen('logfile.txt', 'r');
 $fputcsv($fp, array("test", "lol", "roool"));
 fclose($fp);


 $dispSuccessMsg = true;
 endif;


endif;	 
?>

<html>
<head>


<style type="text/css">
h1 {color:red}
div.error {border: 1px solid red; margin: 20px; padding: 20px; width: 400px}
table {border: 1px solid #CCC; margin: 20px; border-collapse: collapse;}
td, th {border: 1px solid #DDD; padding: 2px}
th {background-color:#363; color: white}
td.error {color:red}
div.success  {border: 1px solid green; margin: 20px; padding: 20px; width: 400px; color:green}
</style>
</head>
<body>  
<h1> Form Validation Lab with  Reg Expressions  </h1>
<p><a href="<?= $_SERVER['PHP_SELF'] ?> ">Refresh This Page</a> | <a href="./logfile.txt">Show Logfile.txt</a> |  
<a href="<?= $_SERVER['PHP_SELF'] ?>?_act=csv">Show logfile.txt Formatted</a>  
    <a href="<?= $_SERVER['PHP_SELF'] ?>?_act=clear">Clear logfile.txt </a></p>
    
<? if ($dispErrMsgs): ?>
<div class="error">
<p>There are errors in the code: </p>
    <ol>
    <? foreach($errMsg as $errMsgs ): ?>
        <li><?= $errMsgs ?></li>
            <? endforeach; ?>
            </ol>
    </div>
<? endif; ?>
<? if ($dispSuccessMsg): ?>
<div class="success">
<p>Thank you for your submission. </p>
</div>
<? endif; ?>


<form action="<?= $_SERVER['PHP_SELF'] ?>"  name="getstuff" method="post" >
  <input type="hidden" name="_act" value="post">
  <table cellspacing = "0">
    <tr>
      <th width="78">Full Name:</th>
      <td width="184"><input name="fullname" type="text" class="textbox" 
      value="<? $_POST['fullname']; ?>" size="20" ></td>
      <td width="626" >Salution of Mr. or Mrs. followed by two text strings separated by any number of spaces. </td>
      </tr>

    <tr>
      <th>Street:</th>
      <td><input name="street" type="text" class="textbox" 
      value="<? $_POST['street']; ?>" size="30"></td>
      <td >2 or 3 digit number followed by a text string ending with Street or Road separated by any number of spaces. </td>
    <tr>
      <th>Phone:</th>
      <td><input name="phone" type="text" class="textbox" 
      value="<? $_POST['phone']; ?>" size="20"></td>

      <td >10 digits, first 3 digits have optional parentheses, either side of digits 456 are optional space, dot or hyphen. </td>
    </tr>
    <tr>
      <th>PostalCode:</th>
      <td><input name="postcode" type="text" class="textbox" 
      value="<? $_POST['postcode']; ?>" size="8" maxlength="8"></td>
      <td > Postal Code: 
        Char Char Digit Hyphen/space Char Digit Digit (No XYZ or 0's. Case insensitive. )</td>
    </tr>

    <tr>
      <th>Email:</th>
      <td><input name="email" type="text" class="textbox" 
      		value="<? $_POST['email']; ?>" size="25" maxlength="40"></td>
      <td > Must accept as
        a minimum : a@b.com</td>
     </tr>
  </table>
  <input name="reset" type='button' class="button" value='RESET' onClick="location.href = location.href">

  <input name="_submit" type="submit" class="button" value="Submit me now!!!" >
</form>
<p id="notice">Everything below here is for testing purposes Don't remove this from your lab
  as I will use these buttons to fill your form with good and bad data as I mark
  it.</p>
<script>
az=document.getstuff;
function goodstuff(){
	az.fullname.value=" Mr. Joe Smith ";
	az.street.value="135 Fennell Road ";
	az.phone.value="905-575.1212";
	az.postcode.value="Ln9-T23";
	az.email.value="jsmith@gov.ca";

}
function goodstuff2(){
	az.fullname.value=" Mrs. Josephine Smith ";
	az.street.value=" 13  Fennell Street";
	az.phone.value=" (905)-575.1212 ";
	az.postcode.value=" Ln9 T23";
	az.email.value=" jsmith@gov.ca  ";

}
function badstuff(){
	az.fullname.value="Miss Josephine Smith";
	az.street.value="1 Somewhere Avenue";
	az.phone.value="905575122";
	az.postcode.value="Z8N 3T2";
	az.email.value="jsmith$gov#ca";

}
function badstuff2(){
	az.fullname.value=" Joe Smith ";
	az.street.value="  1392  Fennell Street";
	az.phone.value="[905] 575-1212";
	az.postcode.value=" Ly9 T20";
	az.email.value=" @jsmith$gov#ca";

}
function goodandbad(){
	az.fullname.value="Mrs. Joe";
	az.street.value=" 135 Fennell Avenue West";
	az.phone.value="905:5751212";
	az.postcode.value=" L99-T23";
	az.email.value=" jsmith@.gov.ca  ";

}

</script>
<form>
  <input type=button class="button" onClick="goodstuff();" value="Fill form with good values " length=100 >
  <input type=button class="button" onClick="goodstuff2();" value="Fill form with good values 2 " >

  <input type=button class="button" onClick="badstuff();" value="Fill form with BAD values ">
  <input type=button class="button" onClick="badstuff2();" value="Fill form with BAD values 2 ">
  
  <input type=button class="button" onClick="goodandbad();" value="Fill form with Good and BAD values 2">

  <br>
</form>


</BODY>
</HTML>

Link to comment
Share on other sites

Thanks! That works. But for some reason it only seems to work whenever I put it into teh file without hardcoding it.

 

When you open the file in mode 'r', the only thing you can do is read from it.  When you need to write to it (such as with fputcsv) you need to use a writable mode such as 'a' or 'w'.

 

 

So in your page, when you reading the file for display, fopen in 'r' mode, read the file, and close it.  When your adding data with fputcsv, fopen in 'a' mode, write the data, then close it.

 

Also, you have a $ on your fputcsv function that should not be there.

 

Link to comment
Share on other sites

I think I figuerd it out. Thanks

 

Now I can't seem to figure out why it won't display the error message whenever I leave teh first field empty. Here's my code:

 

 <?

if ($_POST['_act'] = 'csv'):
$fp = fopen('logfile.txt', 'r');
while (($data = fgetcsv($fp, 1000, ",")) !== FALSE){
	print_r($data);
}
fclose($fp);
endif; 

 if ($_SERVER['REQUEST_METHOD'] == 'POST'):

		if (empty($_POST['fullname'])):
			$errMsg['fullname'] = "Please fill in Your Full Name.";
		endif;
	if (ereg("'Mr\. '", $_POST['fullname']) == false):
			$errMsg['fullname'] = "Please fill in Your Full Name.";
		endif;
		print_r($errMsgs); 
		echo count($errMsgs);

 if (count($disperrMsgs) > 0):

 $dispErrMsgs = true;
 else:
 $fp = fopen('logfile.txt', 'r');
 $fputcsv($fp, array("test", "lol", "roool"));
 fclose($fp);
 $dispSuccessMsg = true;
 endif;


endif;	 
?>

<html>
<head>


<style type="text/css">
h1 {color:red}
div.error {border: 1px solid red; margin: 20px; padding: 20px; width: 400px}
table {border: 1px solid #CCC; margin: 20px; border-collapse: collapse;}
td, th {border: 1px solid #DDD; padding: 2px}
th {background-color:#363; color: white}
td.error {color:red}
div.success  {border: 1px solid green; margin: 20px; padding: 20px; width: 400px; color:green}
</style>
</head>
<body>  
<h1> Form Validation Lab with  Reg Expressions  </h1>
<p><a href="<?= $_SERVER['PHP_SELF'] ?> ">Refresh This Page</a> | <a href="./logfile.txt">Show Logfile.txt</a> |  
<a href="<?= $_SERVER['PHP_SELF'] ?>?_act=csv">Show logfile.txt Formatted</a>  
    <a href="<?= $_SERVER['PHP_SELF'] ?>?_act=clear">Clear logfile.txt </a></p>
    
<? if ($dispErrMsgs): ?>
<div class="error">
<p>There are errors in the code: </p>
    <ol>
    <? foreach($errMsg as $errMsgs ): ?>
        <li><?= $errMsgs ?></li>
            <? endforeach; ?>
            </ol>
    </div>
<? endif; ?>
<? if ($dispSuccessMsg): ?>
<div class="success">
<p>Thank you for your submission. </p>
</div>
<? endif; ?>


<form action="<?= $_SERVER['PHP_SELF'] ?>"  name="getstuff" method="post" >
  <input type="hidden" name="_act" value="post">
  <table cellspacing = "0">
    <tr>
      <th width="78">Full Name:</th>
      <td width="184"><input name="fullname" type="text" class="textbox" 
      value="<? $_POST['fullname']; ?>" size="20" ></td>
      <td width="626" >Salution of Mr. or Mrs. followed by two text strings separated by any number of spaces. </td>
      </tr>

    <tr>
      <th>Street:</th>
      <td><input name="street" type="text" class="textbox" 
      value="<? $_POST['street']; ?>" size="30"></td>
      <td >2 or 3 digit number followed by a text string ending with Street or Road separated by any number of spaces. </td>
    <tr>
      <th>Phone:</th>
      <td><input name="phone" type="text" class="textbox" 
      value="<? $_POST['phone']; ?>" size="20"></td>

      <td >10 digits, first 3 digits have optional parentheses, either side of digits 456 are optional space, dot or hyphen. </td>
    </tr>
    <tr>
      <th>PostalCode:</th>
      <td><input name="postcode" type="text" class="textbox" 
      value="<? $_POST['postcode']; ?>" size="8" maxlength="8"></td>
      <td > Postal Code: 
        Char Char Digit Hyphen/space Char Digit Digit (No XYZ or 0's. Case insensitive. )</td>
    </tr>

    <tr>
      <th>Email:</th>
      <td><input name="email" type="text" class="textbox" 
      		value="<? $_POST['email']; ?>" size="25" maxlength="40"></td>
      <td > Must accept as
        a minimum : a@b.com</td>
     </tr>
  </table>
  <input name="reset" type='button' class="button" value='RESET' onClick="location.href = location.href">

  <input name="_submit" type="submit" class="button" value="Submit me now!!!" >
</form>
<p id="notice">Everything below here is for testing purposes Don't remove this from your lab
  as I will use these buttons to fill your form with good and bad data as I mark
  it.</p>
<script>
az=document.getstuff;
function goodstuff(){
	az.fullname.value=" Mr. Joe Smith ";
	az.street.value="135 Fennell Road ";
	az.phone.value="905-575.1212";
	az.postcode.value="Ln9-T23";
	az.email.value="jsmith@gov.ca";

}
function goodstuff2(){
	az.fullname.value=" Mrs. Josephine Smith ";
	az.street.value=" 13  Fennell Street";
	az.phone.value=" (905)-575.1212 ";
	az.postcode.value=" Ln9 T23";
	az.email.value=" jsmith@gov.ca  ";

}
function badstuff(){
	az.fullname.value="Miss Josephine Smith";
	az.street.value="1 Somewhere Avenue";
	az.phone.value="905575122";
	az.postcode.value="Z8N 3T2";
	az.email.value="jsmith$gov#ca";

}
function badstuff2(){
	az.fullname.value=" Joe Smith ";
	az.street.value="  1392  Fennell Street";
	az.phone.value="[905] 575-1212";
	az.postcode.value=" Ly9 T20";
	az.email.value=" @jsmith$gov#ca";

}
function goodandbad(){
	az.fullname.value="Mrs. Joe";
	az.street.value=" 135 Fennell Avenue West";
	az.phone.value="905:5751212";
	az.postcode.value=" L99-T23";
	az.email.value=" jsmith@.gov.ca  ";

}

</script>
<form>
  <input type=button class="button" onClick="goodstuff();" value="Fill form with good values " length=100 >
  <input type=button class="button" onClick="goodstuff2();" value="Fill form with good values 2 " >

  <input type=button class="button" onClick="badstuff();" value="Fill form with BAD values ">
  <input type=button class="button" onClick="badstuff2();" value="Fill form with BAD values 2 ">
  
  <input type=button class="button" onClick="goodandbad();" value="Fill form with Good and BAD values 2">

  <br>
</form>


</BODY>
</HTML>

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.