|
Posted by kenoli on 09/02/06 15:34
I'm quite impressed and learned a bunch of things from this including
some things about html I didn't know.
I want to make sure I undeerstand something I didn't know before:
In the tags where you include something like: name = "proc[firstname]"
I presume that what is going on here is that an array called "proc[]"
is being created as an element in the global $_POST variable which
contains the content of the input fileds associated with their name
attributes as keys. For each input, a new array elemtn with key is
concatenated to the proc[] array. Do I understand this correctly?
If so, this is quite elegant.
Is there some specific reason you did it this way. I could imagine
keying the switch statement to a text key assigned by each name
attribute, as well.
I hadn't been aware of how many tags can be associated with html form
elements. Many of those included here are obviously not utilized in
the included php script, but I can see how they could be used. Is
there some reason you included them?
What is your reason for including the CDATA striing in the style
section. I know generally what this does but wonder about its
application here. Is it just to create bulletproof code or is there a
specific function? I presume any interpreter that cares about it will
read through the comment tags.
Any reason for using if indif format instead of {} in the if statement
aside from personal style? Ditto for "foreach."
Thanks so much.
--Kenoli
Koncept wrote:
> In article <1157048879.574032.104880@i3g2000cwc.googlegroups.com>,
> kenoli <kenoli@igc.org> wrote:
>
> > I am designing a database with several "key word" columns. Users
> > wanting to search the database will enter terms into html form fields
> > from drop down menus populated from existing lookup tables or by adding
> > their own terms which may, in some cases, be inserted into the lookup
> > tables for subsequent users. Some of the form fields may be left
> > empty.
> >
>
> You could try something like this as a template:
> ( N.B. Don't forget to properly check and prepare your vars for the
> query! )
> ----------------------------------
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
>
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head>
>
> <style type="text/css" media="screen">
> /* <![CDATA[ */
> label{width:150px;float:left}
> #submit{margin:1em 0 0 150px}
> /* ]]> */
> </style>
>
> <title></title>
> </head>
>
> <body>
> <form action="<?=$_SERVER['PHP_SELF']?>"
> method="post">
> <label for="selectopts">Number:</label>
> <select name="proc[number]">
> <option
> label = "select"
> value = "null"
> selected = "selected">[select]</option>
> <option label="1" value="1">1</option>
> <option label="2" value="2">2</option>
> <option label="3" value="3">3</option>
> </select>
> <br />
> <label for="firstname">First Name:</label>
> <input
> type = "text"
> id = "firstname"
> name = "proc[firstname]" />
> <br />
> <label for="lastname">Last Name:</label>
> <input
> type = "text"
> id = "firstname"
> name = "proc[lastname]" />
> <br />
> <label for="phone1">Phone:</label>
> <input
> type = "text"
> id = "firstname"
> name = "proc[phone1]" />
> <br />
> <input
> type = "submit"
> name = "submit"
> value = "Process »"
> id = "submit" />
> </form>
> <?php
> if($_POST["proc"]):
> foreach($_POST["proc"] as $k => $v):
> if(!empty($v)):
> switch($k):
> case 'number':
> preg_match('/^\d+$/',$v)
> && $q[] = "number = '{$v}'";
> break;
> case 'firstname':
> $q[] = "first_name = '{$v}'";
> break;
> case 'lastname':
> $q[] = "last_name = '{$v}'";
> break;
> case 'phone1':
> $q[] = "phone = '{$v}'";
> break;
> default:
> trigger_error("Unrecognized input",
> E_USER_ERROR);
> break;
> endswitch;
> endif;
> endforeach;
> if($q):
> echo "<code>SELECT * FROM table WHERE ",
> implode(" AND ",$q),"</code>";
> endif;
> endif;
> ?>
> </body>
> </html>
>
> --
> Koncept <<
> "The snake that cannot shed its skin perishes. So do the spirits who are
> prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Navigation:
[Reply to this message]
|