Ask a Question related to PHP Development, Design and Development.
-
Jason Dumler #1
Re: Need help to reduce the duplicate php code
Steven wrote:
<snip>> All,
>
> I have these simple php code to check the form input, It will print the
> label red if it is empty and it will save the input if everything about this
> field is right, it will not do anything if it is first visited...
>The following code should work for an arbitrary number of _POST vars.> Regards,
> -Steven
>
>
It could be modified to check for specifics (SSN is formatted correct,
email addresses are valid emails, etc). If could also be modified to
handle some _POST vars being required, and some not with a big
if/elseif/else statement somewhere in the middle.
<?
// create a list representing our posted variables
$varList = array();
// each variable has a _POST name, a display name and whether it was
saved or not
array_push($varList, array("ssn", "SSN", FALSE));
array_push($varList, array("name", "NAME", FALSE));
// we're gonna use this later, init it here
// (not necessary, I just do this)
$i = 0;
// our default for everything being saved is true
$saved = TRUE;
for ($i=0; $i<count($varList); $i++) {
if ( $_POST ) {
$font_color = "#000000";
if ( empty($_POST[$varList[$i][0]]) ) {
// let the remainder of the script know that
// something wasn't saved
$saved = FALSE;
$font_color = "#ff0000";
} else {
// mark this variable saved in the list
$varList[$i][2] = TRUE;
}
print "<span style='font-size: 9pt; color: " .
$font_color . ";'>" . $varList[$i][1] . "</span>";
print "<br/>\n";
} else {
print "<span style='font-size: 9pt; color: #000000;'>" .
$varList[$i][1] . "</span>";
print "<br/>\n";
}
}
// Now the rest of the script knows whether everything was saved or not,
// and
// if something wasn't saved, it knows which value
?>
Jason
Jason Dumler Guest
-
Duplicate Movie Clip & Code?
If I use duplicate movie clip to create another instance of the clip on the main time line, and it gives it a new name, how do I relate the code... -
Best way to reduce pdf?
instead of cropping double click the background layer in the layer pallete that will turn it into a layer then go to the select drop down menu and... -
How to reduce code?
I premise I'm not a C expert... I'm trying to modify some sources and I can't figure out what's the cause of a crash... If I could reduce the... -
how to reduce PHP code volume
HI; I have made a database driven page, using php and mysql, when i made my table and rows and colums for inserting the php code( recordset)... -
mssql, duplicate code, and the dearth of correlation
#1 on my wishlist for future mssql support: The ability to correlate on either a) table-based udfs, or b) parametrized views. I would even... -
Steven #2
Re: Need help to reduce the duplicate php code
"Jason Dumler" <dumlerjason_bugger_spam@netscape.net> wrote in message
news:5FRMa.563220$vU3.543735@news1.central.cox.net ...this> Steven wrote:> > All,
> >
> > I have these simple php code to check the form input, It will print the
> > label red if it is empty and it will save the input if everything aboutThanks a lot and I will try it out...> <snip>> > field is right, it will not do anything if it is first visited...
> >>> > Regards,
> > -Steven
> >
> >
> The following code should work for an arbitrary number of _POST vars.
> It could be modified to check for specifics (SSN is formatted correct,
> email addresses are valid emails, etc). If could also be modified to
> handle some _POST vars being required, and some not with a big
> if/elseif/else statement somewhere in the middle.
>
> <?
> // create a list representing our posted variables
> $varList = array();
> // each variable has a _POST name, a display name and whether it was
> saved or not
> array_push($varList, array("ssn", "SSN", FALSE));
> array_push($varList, array("name", "NAME", FALSE));
> // we're gonna use this later, init it here
> // (not necessary, I just do this)
> $i = 0;
> // our default for everything being saved is true
> $saved = TRUE;
>
> for ($i=0; $i<count($varList); $i++) {
> if ( $_POST ) {
> $font_color = "#000000";
> if ( empty($_POST[$varList[$i][0]]) ) {
> // let the remainder of the script know that
> // something wasn't saved
> $saved = FALSE;
> $font_color = "#ff0000";
> } else {
> // mark this variable saved in the list
> $varList[$i][2] = TRUE;
> }
> print "<span style='font-size: 9pt; color: " .
> $font_color . ";'>" . $varList[$i][1] . "</span>";
> print "<br/>\n";
> } else {
> print "<span style='font-size: 9pt; color: #000000;'>" .
> $varList[$i][1] . "</span>";
> print "<br/>\n";
> }
> }
>
> // Now the rest of the script knows whether everything was saved or not,
> // and
> // if something wasn't saved, it knows which value
> ?>
>
> Jason
>
Regards,
-Steven
Steven Guest
-
lawrence #3
Re: Need help to reduce the duplicate php code
Jason Dumler <dumlerjason_bugger_spam@netscape.net> wrote in message news:<5FRMa.563220$vU3.543735@news1.central.cox.ne t>...
If you're sure the form input will be the only input in $_POST (and> Steven wrote:> > All,
> >
> > I have these simple php code to check the form input, It will print the
> > label red if it is empty and it will save the input if everything about this
> > field is right, it will not do anything if it is first visited...
you're sure your code will always be run on machines that use PHP 4.1
or newer, something I that has never been true for me), then you
should be able to attack your problem in a loop:
while (list($key, $val) = each($_POST) ) {
if ($val) {
// code for filled in input
} else {
// code for input left empty
}
}
lawrence Guest
-
Steven #4
Re: Need help to reduce the duplicate php code
"Steven" <xueming@comcast.net> wrote in message
news:sk5Na.30933$926.1937@sccrnsc03...array>
> "lawrence" <lkrubner@geocities.com> wrote in message
> news:da7e68e8.0307031223.2b4967b8@posting.google.c om...> news:<5FRMa.563220$vU3.543735@news1.central.cox.ne t>...> > Jason Dumler <dumlerjason_bugger_spam@netscape.net> wrote in message> the> > > Steven wrote:
> > > > All,
> > > >
> > > > I have these simple php code to check the form input, It will print> about this> > > > label red if it is empty and it will save the input if everything>> >> > > > field is right, it will not do anything if it is first visited...
> > If you're sure the form input will be the only input in $_POST (and
> > you're sure your code will always be run on machines that use PHP 4.1
> > or newer, something I that has never been true for me), then you
> > should be able to attack your problem in a loop:
> >
> > while (list($key, $val) = each($_POST) ) {
> > if ($val) {
> > // code for filled in input
> > } else {
> > // code for input left empty
> > }
> >
> > }
> Thanks a lot.
>
> Would it be possible to write a function for this using $_POST and anYes, it is working with a function also. Thanks!!> (have name and name tag) as input?
>
> In that case, I would not need anything else for my form.
>
> Is it possible ?
>
> I will try it tonight ...
>
> -Regards,
> Steven
>
>
-Steven
Steven Guest
-
lawrence #5
Re: Need help to reduce the duplicate php code
"Steven" <xueming@comcast.net> wrote in message news:<MrbNa.100357
Your welcome. As I mentioned before, you'll only run into problems if> // code for input left empty> > > If you're sure the form input will be the only input in $_POST (and
> > > you're sure your code will always be run on machines that use PHP 4.1
> > > or newer, something I that has never been true for me), then you
> > > should be able to attack your problem in a loop:
> > >
> > > while (list($key, $val) = each($_POST) ) {
> > > if ($val) {
> > > // code for filled in input
> > > } else {>> > > }> array> >> > > }
> > Thanks a lot.
> >
> > Would it be possible to write a function for this using $_POST and an> > (have name and name tag) as input?
> >
> > In that case, I would not need anything else for my form.
> >
> > Is it possible ?
you ever try to run your code on a machine running PHP pre to version
4.1.
lawrence Guest



Reply With Quote

