Need help to reduce the duplicate php code

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Re: Need help to reduce the duplicate php code

    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...
    >
    <snip>
    > 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

    Jason Dumler Guest

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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)...
    5. 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...
  3. #2

    Default 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 ...
    > 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...
    > >
    > <snip>
    > > 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
    >
    Thanks a lot and I will try it out...

    Regards,
    -Steven


    Steven Guest

  4. #3

    Default 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>...
    > 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...
    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
    }

    }
    lawrence Guest

  5. #4

    Default Re: Need help to reduce the duplicate php code


    "Steven" <xueming@comcast.net> wrote in message
    news:sk5Na.30933$926.1937@sccrnsc03...
    >
    > "lawrence" <lkrubner@geocities.com> wrote in message
    > news:da7e68e8.0307031223.2b4967b8@posting.google.c om...
    > > Jason Dumler <dumlerjason_bugger_spam@netscape.net> wrote in message
    > news:<5FRMa.563220$vU3.543735@news1.central.cox.ne t>...
    > > > 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...
    > >
    > > 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 an
    array
    > (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
    >
    >
    Yes, it is working with a function also. Thanks!!

    -Steven


    Steven Guest

  6. #5

    Default Re: Need help to reduce the duplicate php code

    "Steven" <xueming@comcast.net> wrote in message news:<MrbNa.100357
    > > > 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 an
    > array
    > > (have name and name tag) as input?
    > >
    > > In that case, I would not need anything else for my form.
    > >
    > > Is it possible ?
    Your welcome. As I mentioned before, you'll only run into problems if
    you ever try to run your code on a machine running PHP pre to version
    4.1.
    lawrence Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139