jaArch Posted November 1, 2011 Share Posted November 1, 2011 CSV File ReadingDisplaying 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> Quote Link to comment https://forums.phpfreaks.com/topic/250218-csv-file-read/ Share on other sites More sharing options...
Nodral Posted November 1, 2011 Share Posted November 1, 2011 Try opening the file with $fp = fopen('logfile.txt', 'r'); This will enable you to read the file rather than append it Quote Link to comment https://forums.phpfreaks.com/topic/250218-csv-file-read/#findComment-1283919 Share on other sites More sharing options...
jaArch Posted November 1, 2011 Author Share Posted November 1, 2011 So that's what the 'a' or 'r' is for! Are those the only commands that are used with it or is there more? Quote Link to comment https://forums.phpfreaks.com/topic/250218-csv-file-read/#findComment-1283937 Share on other sites More sharing options...
Nodral Posted November 1, 2011 Share Posted November 1, 2011 Read this http://php.net/manual/en/function.fopen.php Quote Link to comment https://forums.phpfreaks.com/topic/250218-csv-file-read/#findComment-1283939 Share on other sites More sharing options...
jaArch Posted November 1, 2011 Author Share Posted November 1, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/250218-csv-file-read/#findComment-1283948 Share on other sites More sharing options...
jaArch Posted November 1, 2011 Author Share Posted November 1, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/250218-csv-file-read/#findComment-1283960 Share on other sites More sharing options...
kicken Posted November 1, 2011 Share Posted November 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250218-csv-file-read/#findComment-1283971 Share on other sites More sharing options...
jaArch Posted November 1, 2011 Author Share Posted November 1, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/250218-csv-file-read/#findComment-1283986 Share on other sites More sharing options...
jaArch Posted November 1, 2011 Author Share Posted November 1, 2011 Now I'm getting this error message: Fatal error: Function name must be a string in /home/students/000136921/public_html/10065/php/5/formvalregexpr.php on line 47 What does that mean? Quote Link to comment https://forums.phpfreaks.com/topic/250218-csv-file-read/#findComment-1284051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.