schris403 Posted November 3, 2007 Share Posted November 3, 2007 I'm trying to write a fairly simple php script which looks at a csv text file in the same directory for its data based on a variable that is passed in the address but am really struggling on this.. An example of what the address could be is: http://www.something.com/script.php?field1=1234 The database.txt file will contain something like this: 1234,yes,yes,yes 4567,no,yes,yes 8901,yes,no,no When field1 is 1234, it should then look at the next three fields to determine what result to display. For example, yes,yes,yes should give a result of "I work" no,yes,yes should result "I am disabled" yes,no,no should be "I am done" Any help or guidance would be greatly appreciated, I've been struggling with this for weeks and can't even partly get it to work the way I want.. Thanks so much! Chris Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 here you go I could make it a little better if you wish but here is the initial code $_GET['field1'] = 1234; $strData = <<< EOF 1234,no,yes,yes 4567,no,yes,yes 8901,yes,no,no EOF; if (preg_match("/".$_GET['field1']."\,yes,yes,yes/",$strData)) { echo "I work"; } else if (preg_match("/".$_GET['field1']."\,no\,yes\,yes/",$strData)) { echo "I am disabled"; } else if (preg_match("/".$_GET['field1']."\,yes\,no\,no/",$strData)) { echo "I am done"; } Quote Link to comment Share on other sites More sharing options...
schris403 Posted November 3, 2007 Author Share Posted November 3, 2007 Thanks! Can we make the strData read from a seperate txt file? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 yes $strData = file_get_contents("filename and path"); Quote Link to comment Share on other sites More sharing options...
schris403 Posted November 3, 2007 Author Share Posted November 3, 2007 Thank you so much, this works great! Now the last thing I need is something to read and write that text file so I can go in and change some of the parameters through a webpage. Should I use php for that as well? Thanks, Chris Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 would you elaborate ? Quote Link to comment Share on other sites More sharing options...
schris403 Posted November 3, 2007 Author Share Posted November 3, 2007 Sure, I would like to have a way to create a new record ie: 4112,yes,yes,yes with a text box for the number field and drop downs for each yes/no This page should also show what already exists in the text file with a way to change the existing records yes/no fields or delete the record completely. Chris Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 3, 2007 Share Posted November 3, 2007 Something like the following? <?php if(isset($_POST['add'])) { $new_entry = $_POST['id']; $new_entry .= ',' . implode(',',$_POST['option']) . "\n"; echo 'Adding: ' . $new_entry; // write new entry to file $handle = fopen('file.txt', 'a'); // open fwrite($handle, $new_entry); // write fclose($handle); // close } ?> <form action="#" method="post"> <table border="0" cellpadding="5" cellspacing="0"> <tr> <th colspan="4">Add New</th> </tr> <tr> <th>ID</th> <th colspan="3">Options</th> </tr> <tr> <td><input type="text" name="id" size="10" /></td> <td><select name="option[]"> <option value="yes">Yes</option> <option value="no">No</option> </select></td> <td><select name="option[]"> <option value="yes">Yes</option> <option value="no">No</option> </select></td> <td><select name="option[]"> <option value="yes">Yes</option> <option value="no">No</option> </select></td> </tr> <tr> <td colspan="4"><input type="submit" name="add" value="Add" /></td> </tr> </table> </form> Change file.txt (in the fopen() function) to your file that contains the data. Quote Link to comment Share on other sites More sharing options...
schris403 Posted November 3, 2007 Author Share Posted November 3, 2007 Thats it! Can we add something to display the existing data at the top so I can modify the records? ie delete or change the yes/no fields. Thanks once again, Chris Quote Link to comment Share on other sites More sharing options...
schris403 Posted November 6, 2007 Author Share Posted November 6, 2007 Any one have thoughts on this? Thanks, Chris Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 don't expect that we will feed on every code you wish from us try it then ask if theres an error or learn i suggest learn the basics http://www.w3schools.com Quote Link to comment Share on other sites More sharing options...
schris403 Posted November 6, 2007 Author Share Posted November 6, 2007 I understand and I greatly appreciate everyone's help so far and this community as a whole. I am with a non-profit Boys & Girls Club and have very little money to spend on things so I've been trying to take a stab at this latest project. I have definately been learning a ton so far on other php pages that I have made but this one I am lost... I can't even find any examples which come close to this. This is why I ask for anyones help that might be able to finish this up or point me in the right direction with an example. Thanks once again, Chris Quote Link to comment 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.