pepperface Posted October 30, 2007 Share Posted October 30, 2007 Hi, I'm very new to all this so I do apologise if what I'm asking sounds stupid or isn't achievable for any reason. What I want to be able to create is a series of input text fields that are identified by and range from name_01 to name_50. Fields are blank until a user inputs his/her name in the field of their choice, (eg. 'Sammy decides to type her name into name_17) and hits the submit button. Once the submit button is hit the data in any/all the fields is sent to a php script which writes to a txt file, adding/listing each name and assigning it an identifier indicating which field it was typed into (eg Sammy=name_17 or similar...?). The contents of the text file can then be read back and displayed to users; so that they can see which name slots have already been taken. I don't know if any of this is do-able? I've tried to learn from and modify existing scripts (like guestbook or eCard stuff) to try and fit the bill but most of the existing scripts actually do way more than I need (I don't need validators of anything like that) and I end up getting horribly lost in it all :-\ Extra info: I'm using Flash to do all this but don't worry particularly about that. If anyone can nudge me towards anything that might provide the hints or the info I need I can probably muddle through it enough to be able to apply it to the Flash environment. I just need some pointers, really. I've been at this for 18 hours straight, now and am at my wits end! Huge thanks in adavance! Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/ Share on other sites More sharing options...
schme16 Posted October 30, 2007 Share Posted October 30, 2007 Can't see why it wouldn't work You'd simply need to us a ''foreach' fuction and get the name of the field (that's called the 'key') thats currently being read and tell it all to go to a string... then output using a file_function.... perhaps like this <?php foreach($_POST as $post) { if($post != 'The text on your submit button, case sensitive!') { $key = array_keys($_POST, $post); $key = $key[0]; $result = "$key = $post "; fwrite(@fopen('list.txt', 'a'), $result); } } ?> This will at least get you started, you will have to refine this to fit your criteria... but essentially it will output to a file called list.txt and save the value of each one.... you will probably need the explode function to retrieve data from it... and just ask if your not sure hot to go about that... Hope that help! Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381002 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Hi... and HUGE thanks for replying. You're only person on the whole Net who has!!! Even with your kind advice I'm afraid I'm still floundering, though. I found a simple tute about getting php to write to a txt file, appending it rather than overwriting: <?php $File = "YourFile.txt"; $Handle = fopen($File, 'a'); $Data = "Jane Doe\n"; fwrite($Handle, $Data); $Data = "Bilbo Jones\n"; fwrite($Handle, $Data); print "Data Added"; fclose($Handle); ?> I've managed to get this to work fine but obviously it's not quite what I need. Instead of the pre-existing "Jane Doe" etc the users need to be able to define their own names and have the 'key' (?) defined as something more informative than 'Data'. Also, the YourFile.txt output doesn't appear to be in a format that could be read back into fields for the purposes of displaying it to users - it just lists the names: Jane Doe Bilbo Jones Would it be possible to have something fairly basic like the above but instead of $Data , to have $name_01 $name_02 etc? And would something like: $YourFile.txt = "name_01=$name_01&name_02=$name_02"; produce anything like I'm looking for? I've previously and successfully deployed all manner of seemingly more complex php (as mentioned, eCards, guestbooks etc) but I'm so beaten, exhausted and demoralised by this apparently simple task that I'm actually considering just phoning the 50 people concerned, asking then what time slot they'd like and then simply writing their preferences down using a pencil and a piece of paper! Sigh! Thanks, in advance for any extra help! Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381024 Share on other sites More sharing options...
kratsg Posted October 30, 2007 Share Posted October 30, 2007 This is one of my favorite formats for text files, id:user or user:pass. This is an example code to READ the file correctly (assuming you've written it in a constant, standard format). text file code (users.txt) name_1:Jane Doe name_7:Bilbo Jones name_50:Joe Dirt name_24:Darth Vader name_36:Dark Helmet PHP File, construct the array that contains the information <?php function users_exist(){ $file=fopen("users.txt",'r'); $holder = array();//clear off the array while($line = fgets($file)){//while we can still read the file $line=trim($line);//remove the line endings list($id,$user) = explode(':',$line);//$string[0] = $ids, etc... $holder[$id] = $user; } fclose($file); return holder; } ?> This will contain an array of information that contains any user that has already submitted their name in one of the textboxes: $holder["name1"] = "Jane Doe"; Now, let's look at the php that will construct this. <?php $holder = users_exist(); echo "<form name='user_stuff' method='post'>"; while($i=1;$i<=50;$i++){ $name = "name_".$i; if(empty($holder[$name])){ echo "<label>Name $i:<input type='text' name='".$name."'></label><br>"; } else {//it does have a value, so we just echo a span echo "<label><b>Name $i:</b><span id='".$name."'>".$holder[$name]."</span></label><br>"; } } echo "<input type='reset'><input type='submit'></form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381047 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Thanks kratsg but it seems I'm just not destined to be able to grasp any of this Your kind post has come closer (I think ?) to helping me understand what I should be aiming for but sadly, I still can't really make head nor tail of what exactly it is that I'm supposed to be doing (combining the two pieces of code? using them both as a basis for something else?) I now fear that I'm a lost casue (particularly as I now haven't slept for more than 24 hours and so any powers of reasoning that I may have had are rapidly diminishing). It's a fine blow to my conceit but I genuinely did think that with (several years) of Flash actionscripting under my belt I'd be able to quickly recognise ways through the php maze - at least enough to be able to create something I imagined was relatively simple. I hope you'll take it as acompliment when I say it's testament to just how clever you php folk are that I have found myself totally and utterly at sea. I'm extremely grateful for both of your help and advice but having worked fruitlessly on this for a whole day and a night I now have only two hours in which to garner the info I'd hoped to harvest online; so it really is the phone plus pen and pencil for me. Thanks for trying, though... and I'm sorry for not being able to get my head 'round it all. Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381059 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Lawks I can hardly believe it but after one, last final effort I think I might be getting there!!! Taking on board all your advice I've cobbled together the following: <?php $File = "bookings.txt"; $Handle = fopen($File, 'a'); $name1 = "Jane"; fwrite($Handle, $name1); $name2 = "Bobby"; fwrite($Handle, $name2); $name3 = "Tommy"; fwrite($Handle, $name3); $name4 = "Harry"; fwrite($Handle, $name4); $name5 = "Suzie"; fwrite($Handle, $name5); fclose($Handle); echo "name1=".$name1."&name2=".$name2."&name3=".$name3."&name4=".$name4."&name5=".$name5; ?> Which returns: name1=Jane&name2=Bobby&name3=Tommy&name4=Harry&name5=Suzie and also sucessfully writes to the txt file 'bookings.txt' I can retrieve (read back) the above string without a hitch. The names show up just fine in the fields that they're supposed to load into - but unfortunately I'm unable do the same when trying to read back from the txt file which has been written to. This is because instead of the above string, what's created in bookings.txt is: JaneBobbyTommyHarrySuzie Any ideas about what I'd have to alter to alter in the php script to get it to write to the txt file exactly what it returns instead of it removing the name1=Jane& etc. Nearly there... thanks in advance for your help and patience! Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381088 Share on other sites More sharing options...
adam291086 Posted October 30, 2007 Share Posted October 30, 2007 Not sure if this will work as i am at work and can't test it, pluse i am a newbie <?php $File = "bookings.txt"; $Handle = fopen($File, 'a'); $name1 = "Jane"; fwrite($Handle, "name1=", $name1); $name2 = "Bobby"; fwrite($Handle, "name2=", $name2); $name3 = "Tommy"; fwrite($Handle, "name3=", $name3); $name4 = "Harry"; fwrite($Handle, "name4=", $name4); $name5 = "Suzie"; fwrite($Handle, "name5=", $name5); fclose($Handle); echo "name1=".$name1."&name2=".$name2."&name3=".$name3."&name4=".$name4."&name5=".$name5; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381095 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Hi Adam... nope, sadly that doesn't seem to work. Unless I've done something wrong to cock it up? But I don't think so Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381100 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Many thanks for trying, though Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381101 Share on other sites More sharing options...
adam291086 Posted October 30, 2007 Share Posted October 30, 2007 ok thjs may work. I know why its not working but im not sure how to solve. Try the stuff below <?php $File = "bookings.txt"; $Handle = fopen($File, 'a'); $name1 = "name1=Jane"; fwrite($Handle, $name1); $name2 = "name2=Bobby"; fwrite($Handle, $name2); $name3 = "name3=Tommy"; fwrite($Handle, $name3); $name4 = "name4=Harry"; fwrite($Handle, $name4); $name5 = "name5=Suzie"; fwrite($Handle, $name5); fclose($Handle); echo $name1.$name2.$name3.$name4.$name5; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381111 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Hi again... huge apols for the delay in getting back. Had to take my patient but rather peeved looking dog out for a much-needed run! Anyway, thanks once more for all your time and effort. It really is much appreciated, cheers. I slung up the latest version of your code but still no joy I'm afraid. You can have a look at what's produced here: http://falmouthgigclub.co.uk/DB/bookings.php and here: http://falmouthgigclub.co.uk/DB/bookings.txt Don't know if that will help shed any light on the matter? Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381133 Share on other sites More sharing options...
adam291086 Posted October 30, 2007 Share Posted October 30, 2007 mmmmmmmm go back to the code when it was nearly work and show me what you get Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381137 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Okey dokey... hang on two ticks... Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381141 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Okay, if you try those two links again it'll show you the original 'almost does what I need' version of things. Note: Obviously, every time the php page is called it adds another line of the names to the txt file. Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381144 Share on other sites More sharing options...
adam291086 Posted October 30, 2007 Share Posted October 30, 2007 so you just want the text file to have name1="name"? Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381148 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Yeah, basically. The pre-existing names that are included in the current php are really only there to give me something to look at and work back from. Essentially, what I want to be able to do is have users input their own names which will then output in the following format in the booking.txt file: name1=UsersOwnName&name2=OtherUsersOwnName Am I making any sense ??? Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381153 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 I think we're on the same track? Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381155 Share on other sites More sharing options...
adam291086 Posted October 30, 2007 Share Posted October 30, 2007 i know what you want and i am just thinking. If you change to this it should work <?php $File = "bookings.txt"; $Handle = fopen($File, 'a'); $name1 = "name1 Jane"; fwrite($Handle, $name1); $name2 = "name2 Bobby"; fwrite($Handle, $name2); $name3 = "name3 Tommy"; fwrite($Handle, $name3); $name4 = "name4 Harry"; fwrite($Handle, $name4); $name5 = "name5 Suzie"; fwrite($Handle, $name5); fclose($Handle); echo "name1=".$name1."&name2=".$name2."&name3=".$name3."&name4=".$name4."&name5=".$name5; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381173 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Sadly, I'm not sure if we might be going backwards a bit with this one? Unfortunately, it seems that the latest output generated isn't in a readable format as far as loading it back into fields is concerned. The php page produces: name1=name1 Jane&name2=name2 Bobby&name3=name3 Tommy&name4=name4 Harry&name5=name5 Suzie And the txt file shows: name1 Janename2 Bobbyname3 Tommyname4 Harryname5 Suzie Whereas what I need the php to produce is: name1=Jane&name2=Bobby&name3=Tommy&name4=Harry&name5=Suzie And the txt file to similarly show: name1=Jane&name2=Bobby&name3=Tommy&name4=Harry&name5=Suzie Please, please don't work any harder on this than you already have done. I feel just dreadful for the amount of time you now spent! It is very much appreciated, though! Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381210 Share on other sites More sharing options...
adam291086 Posted October 30, 2007 Share Posted October 30, 2007 no we are nearly there on the echo part just put echo $name1, $name2, $name3, $name4, $name5; Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381215 Share on other sites More sharing options...
adam291086 Posted October 30, 2007 Share Posted October 30, 2007 sorry, i am doing it in step to ensure it works Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381227 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Nope, still no joy. What the php now shows is: name1 Janename2 Bobbyname3 Tommyname4 Harryname5 Suzie And what the txt file displays is: name1 Janename2 Bobbyname3 Tommyname4 Harryname5 Suzie So, they are, at least both displaying the same thing, which is GREAT... but the output very definitely needs to have that name1=Jane&name2=Bobby thing going on and at the moment the equal and the & characters just isn't there. Sorry once again for the delay in replying but I've been frantically trying various permutations to try and save you some time, but without any luck. Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381255 Share on other sites More sharing options...
adam291086 Posted October 30, 2007 Share Posted October 30, 2007 i now but we are edging closer. Which is my aim. I take things slowly so i get it right. <?php $File = "bookings.txt"; $Handle = fopen($File, 'a'); $name1 = "name1 Jane&"; fwrite($Handle, $name1); $name2 = "name2 Bobby&"; fwrite($Handle, $name2); $name3 = "name3 Tommy&"; fwrite($Handle, $name3); $name4 = "name4 Harry&"; fwrite($Handle, $name4); $name5 = "name5 Suzie"; fwrite($Handle, $name5); fclose($Handle); echo $name1, $name2, $name3, $name4, $name5; ?> That should show the & sign Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381260 Share on other sites More sharing options...
pepperface Posted October 30, 2007 Author Share Posted October 30, 2007 Good grief, it's so close I can practically taste it!!! The & character is indeed there now. The php and the txt file both display: name1 Jane&name2 Bobby&name3 Tommy&name4 Harry&name5 Suzie So, just the equal symbol to go! I'll owe you a HUGE drink for all this!!!! Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381278 Share on other sites More sharing options...
adam291086 Posted October 30, 2007 Share Posted October 30, 2007 this should work i think <?php $File = "bookings.txt"; $Handle = fopen($File, 'a'); $name1 = "name1 = Jane&"; fwrite($Handle, $name1); $name2 = "name2 = Bobby&"; fwrite($Handle, $name2); $name3 = "name3 = Tommy&"; fwrite($Handle, $name3); $name4 = "name4 = Harry&"; fwrite($Handle, $name4); $name5 = "name5 = Suzie"; fwrite($Handle, $name5); fclose($Handle); echo $name1, $name2, $name3, $name4, $name5; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75327-simple-problem/#findComment-381279 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.