[PHP] reading file into a keyword-indexed array
I would like to read and write user-settings to a keyword-value file and be able to extract it. The following code works fine but i wondered if there was a more elegant/direct way of doing this. -Durwood $file = user_settings.txt; // Read user settings from file if it exists, otherwise set to defaults and // save them ... a separate method exists for users to change their settings. if ($is_readable($file)) { $parm = read_defaults($file); } else { $parm['method'] = bsort; $parm['tsize'] = 200; $parm['per_row'] = 5; $parm['sortby'] = last_name; $parm['order'] = ascending; write_defaults($file, $parm); } // Read keyword-value pairs from file into array function read_defaults($default_file) { $fp = file($default_file); foreach ($fp as $line) { $line = trim($line); list ($key, $value) = explode( , $line); $param[$key] = $value; } return $param; } // Write keyword-value pairs from array function write_defaults($default_file, $param) { $fp = fopen($default_file, w); while (list($key, $value) = each ($param)) fwrite ($fp, $key $value\n); fclose($fp); } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using custom button form element instead of standard submit?
Durwood Gafford [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... To make the issues more clear (assuming everyone's not totally burned out thinking about this one), I will submit links to working, example code in a followup post. I'm swamped with a proposal i'm working on for my 'day' job, however -- so stay tuned. you can go to http://www.berzerker.net/durwood/phptest and see 3 samples of a simple directory traversal php script ... this is, in essence, the problem i'm facing. Read the readme.txt file there for more info .. but basically there are 2 solutions that work but aren't optimal ... and one that don't work. and i couldn't figure out how to do the image version -- it's explained in teh readme file. so if anyone can come up with a better solution, let me know! thx, durwood -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using custom button form element instead of standard submit?
sure this would work but it'd be VERY inefficient in my application. It's a file browser application so i have hundreds of folder icons on a page .. the 'submit' element will pass back the name of the button clicked and it would be available immediately/directly, which is what i want. BUT an 'image' element would require me to loop through ALL buttons doing an 'isset' test on each one. I can't believe html provides no means for using an icon as a submit button. well i'm off pursuing a javascript solution now. by the way .. unrelated question ... why would one want to use $_REQUEST['foo'] instead of just $foo? doesn't php give you access to form variables using the latter construct which is much cleaner? Philip Olson [EMAIL PROTECTED] wrote in message Pine.BSF.4.10.10301300503280.83137-10@localhost">news:Pine.BSF.4.10.10301300503280.83137-10@localhost... [snip] I assume i could use solution A above, and parse the _x out of the name value .. but this seems pretty hoaky and it's my understanding soultion B is [snip] This is how it's done. If this is set, the button was clicked. For example: if (isset($_REQUEST['thebuttonname_x'])) { print The button was clicked; } Regards, Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using custom button form element instead of standard submit?
input type=submit name=sample_a input type=image name=sample_b src=myimg.gif With the first method, you'd need to test for a button named 'sample_a'. For the second method, you'd need to test for a button named 'sample_b_x'. no ... with the first example you can have numerous buttons on the same HTML page with the same name and use the 'value' attribute to see which one is pressed. I did this and it works quite elegantly for my application. input type=submit name=sample value=a input type=submit name=sample value=b input type=submit name=sample value=c input type=submit name=sample value=d your php form handler can now simply do: print The user selected button $samplebr\n) otherwise, with the image solution you'd have to do something like this... foreach ($_GET as $button) { if (isset($button)) { $button_name = substr($button, -2, 0); // or whatever to remove the _x print The user selected button substr($button_name); break; } } Although arrays might provide a solution that wouldn't quite be as good as the 'submit' button solution, but not as worse as a straight name-based 'image' solution. Given that the 'button' element with type = submit does NOT work, maybe the javascript solution is the most elegant. but i haven't gotten that to work yet either! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using custom button form element instead of standard submit?
1lt John W. Holmes [EMAIL PROTECTED] wrote in message If that's what you want, then just make each button a normal a href link and pass an variable to the next page in the link (like someone has already suggested). You'd get the same end result. a href=page.php?sample=aimg/a a href=page.php?sample=bimg/a a href=page.php?sample=cimg/a a href=page.php?sample=dimg/a print The user selected button $samplebr\n; Indeed this solution does appear to be the best in that it does not rely on client-side scripting, it allows for control of the icon/button appearance, and it allows direct access to the value associated with the selected button. I had hoped to use POST instead of GET, however -- i didn't want to pass the information via the URL. To make the issues more clear (assuming everyone's not totally burned out thinking about this one), I will submit links to working, example code in a followup post. I'm swamped with a proposal i'm working on for my 'day' job, however -- so stay tuned. thanks for all of the insights/assistance! -Durwood -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] using custom button instead of standard submit form element
I can't figure out how to tell which button was pressed by a user when i'm using a button instead of a standard submit form element. This works: input type=submit name=parent value=foo $parent will equal foo This doesn't work: button type=submit name=parent value=fooimg src=icon.gif/button $parent will equal img src=icon.gif NOT foo How do I get the value of foo to be returned in $parent and still use a graphical icon instead of a standard submit button? thanks, durwood ([EMAIL PROTECTED]) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Using custom button form element instead of standard submit?
I can't figure out how to tell which button was pressed by a user when i'm using a button instead of a standard submit form element. This works: input type=submit name=parent value=foo $parent will equal foo This doesn't work: button type=submit name=parent value=fooimg src=icon.gif/button $parent will equal img src=icon.gif NOT foo How do I get the value of foo to be returned in $parent and still use a graphical icon instead of a standard submit button? thanks, durwood ([EMAIL PROTECTED]) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using custom button form element instead of standard submit?
This suggested solution is not quite what i was looking for (I was aware of it already): A: input TYPE=IMAGE NAME=parent VALUE=foo SRC=icon.gif The above uses an image as a bitmap and gets the x,y values where it was clicked. I'd like to use an ICON (instead of a standard submit button) and only care about WHICH icon was clicked, not where on the bitmap. Presumably the correct HTML is to use the BUTTON type as follows: B: button type=submit name=parent value=fooimg src=icon.gif/button but this does not work. And of course, C: input type=submit name=parent value=foo gives the standard submit button. One suggestion was to add a src=icon.gif attribute to the submit type directly above (example C), but that didn't change the button ... it still had the standard button. I assume i could use solution A above, and parse the _x out of the name value .. but this seems pretty hoaky and it's my understanding soultion B is correct HTML and SHOULD work! Is it a bug in php that it is causing it not to work? or is the button construct depricated? or am i misunderstanding how that BUTTON construct is supposed to work? Sorry to be dense about this but it seems like a simple concept that's frustratingly difficult. -Durwood Don Read [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... On 27-Jan-2003 Durwood Gafford wrote: I can't figure out how to tell which button was pressed by a user when i'm using a button instead of a standard submit form element. This works: input type=submit name=parent value=foo $parent will equal foo This doesn't work: button type=submit name=parent value=fooimg src=icon.gif/button $parent will equal img src=icon.gif NOT foo How do I get the value of foo to be returned in $parent and still use a graphical icon instead of a standard submit button? input TYPE=IMAGE NAME=parent VALUE=foo SRC=icon.gif Regards, -- Don Read [EMAIL PROTECTED] -- Beer is proof that God loves us and wants us to be happy. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php