PHP equivalent of ASP code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    PHP equivalent of ASP code

    Hi all,

    Im trying to duplicate this ASP code in PHP.

    Basically its a form of 10 records pulled from an MySQL table. The
    form lets the user update all 10 records at once if they want to.

    Each text box I name using the ID of the database record and the
    Database Field. Example: 3|Name (3 is the ID and Name is the
    database field)

    When the form is submitted this code is run:

    for x = 1 to request.form.co unt()
    if request.form.ke y(x) <> "Submit" then
    curFld = request.form.ke y(x)
    aryCats = split(curFld, "|")

    ID = aryCats(1)
    Field = aryCats(2)
    Data = request.form(cu rFld)

    strSQL = "UPDATE " & Table & " Set " & Field & "= '" & Data "'
    WHERE ID = " & ID
    'response.write strSQL
    conn.execute strSQL

    end if
    next

    The code loops thru all the objects on the form. It takes each object
    name and splits it into an array using the | in the object name.

    Is there anything in PHP that will count the number of objects on a
    form and let you loop thu them like this script?

    Thanks in advance,

    Mark
    markNOSPAM@erms olutions.com
  • Nikolai Chuvakhin

    #2
    Re: PHP equivalent of ASP code

    mark@ermsolutio ns.com (Mark) wrote in message
    news:<a5b42f60. 0309041344.754a 3939@posting.go ogle.com>...[color=blue]
    >
    > Im trying to duplicate this ASP code in PHP.
    >
    > for x = 1 to request.form.co unt()
    > if request.form.ke y(x) <> "Submit" then
    > curFld = request.form.ke y(x)
    > aryCats = split(curFld, "|")
    > ID = aryCats(1)
    > Field = aryCats(2)
    > Data = request.form(cu rFld)
    > strSQL = "UPDATE " & Table & " Set " & Field & "= '" & Data "'
    > WHERE ID = " & ID
    > 'response.write strSQL
    > conn.execute strSQL
    > end if
    > next[/color]

    Well, if I follow your naming conventions, here's what I get:

    foreach ($_REQUEST as $curFld => $Data) {
    if ($curFld <> 'Submit') {
    list ($ID, $Field) = explode('|', $curFld);
    $strSQL = "UPDATE $Table SET $Field = '$Data' WHERE ID = $ID";
    echo $strSQL;
    $result = mysql_query ($strSQL)
    or die ("Query failed: $strSQL");
    }
    }

    Obviously, I don't know what database you are using, so I put in
    a call to mysql_query(). If you are not using MySQL, you should
    replace it with a function call appropriate for your database engine.
    [color=blue]
    > Is there anything in PHP that will count the number of objects on a
    > form and let you loop thu them like this script?[/color]

    As you can see from the code above, in PHP you don't have to know
    the number of elements in array to loop through it; foreach() takes
    care of that for you...

    Cheers,
    NC

    Comment

    • Zurab Davitiani

      #3
      Re: PHP equivalent of ASP code

      Nikolai Chuvakhin wrote on Thursday 04 September 2003 19:01:

      Just couple of additional comments:

      <snip>[color=blue][color=green]
      >> for x = 1 to request.form.co unt()[/color][/color]
      <snip>[color=blue]
      > foreach ($_REQUEST as $curFld => $Data) {[/color]

      <snip>

      If I am not mistaken, Request.Form is equivalent of $_POST
      [color=blue][color=green]
      >> Is there anything in PHP that will count the number of objects on a
      >> form and let you loop thu them like this script?[/color]
      >
      > As you can see from the code above, in PHP you don't have to know
      > the number of elements in array to loop through it; foreach() takes
      > care of that for you...[/color]

      It's also worth pointing out that if one was implementing a similar process
      in PHP, one would simply use array elements as form element names, such as
      ID[0], ID[1], etc., and then there wouldn't be a need to count or go
      through all form elements, or implement an ugly hack of separating their
      names by | either (which actually restricts how and in what order form
      elements are submitted). Maybe of interest to the OP.

      --
      Business Web Solutions
      ActiveLink, LLC

      Comment

      • Mark

        #4
        Re: PHP equivalent of ASP code

        Thanks for all the input.

        Sorry I didnt mention that I can successfully update the records in
        MySQL.

        Also, the beauty of the asp code is I dont have to know the field
        names. If I use array elements wouldnt I need to know the field names
        before hand?

        Basically I need to write this code and walk away for a couple years.

        Ive given the users a tool that lets them update a table (add fields)
        and that field would thn appear on the form. Completely dynamic and
        out of my hands.

        Assume that everything works. Have had this system working in asp for
        3 years now. But I really want to move to linux and PHP.

        would this also do the trick?
        foreach($HTTP_P OST_VARS as $key => $value)

        Thanks

        Mark

        Comment

        • PIII450@HOME.NL

          #5
          Re: PHP equivalent of ASP code

          On 4 Sep 2003 14:44:23 -0700, mark@ermsolutio ns.com (Mark) wrote:
          [color=blue]
          >Hi all,
          >
          >Im trying to duplicate this ASP code in PHP.
          >
          >Basically its a form of 10 records pulled from an MySQL table. The
          >form lets the user update all 10 records at once if they want to.
          >
          >Each text box I name using the ID of the database record and the
          >Database Field. Example: 3|Name (3 is the ID and Name is the
          >database field)
          >
          >When the form is submitted this code is run:
          >
          >for x = 1 to request.form.co unt()
          > if request.form.ke y(x) <> "Submit" then
          > curFld = request.form.ke y(x)
          > aryCats = split(curFld, "|")
          >
          > ID = aryCats(1)
          > Field = aryCats(2)
          > Data = request.form(cu rFld)
          >
          > strSQL = "UPDATE " & Table & " Set " & Field & "= '" & Data "'
          >WHERE ID = " & ID
          > 'response.write strSQL
          > conn.execute strSQL
          >
          > end if
          >next
          >
          >The code loops thru all the objects on the form. It takes each object
          >name and splits it into an array using the | in the object name.
          >
          >Is there anything in PHP that will count the number of objects on a
          >form and let you loop thu them like this script?
          >
          >Thanks in advance,
          >
          >Mark
          >markNOSPAM@erm solutions.com[/color]

          Have you tried ASP2PHP and see what that does?

          Comment

          • Nikolai Chuvakhin

            #6
            Re: PHP equivalent of ASP code

            Zurab Davitiani <agt@mindless.c om> wrote in message
            news:<vDS5b.111 78$q97.3081@new ssvr25.news.pro digy.com>...[color=blue]
            >
            > <snip>[color=green][color=darkred]
            > >> for x = 1 to request.form.co unt()[/color][/color]
            > <snip>[color=green]
            > > foreach ($_REQUEST as $curFld => $Data) {[/color]
            > <snip>
            >
            > If I am not mistaken, Request.Form is equivalent of $_POST[/color]

            I couldn't remember, so I put in $_REQUEST just in case; it should
            work anyway... Also, I've seen some ASP developers do stuff like
            this:

            <form method="post" action="do_some thing.asp?id=48 ">

            I don't even want to guess what happens there... :)

            Cheers,
            NC

            Comment

            • Guest's Avatar

              #7
              Re: PHP equivalent of ASP code

              "Nikolai Chuvakhin" <nc@iname.com > wrote in message
              news:32d7a63c.0 309051726.2988d d2@posting.goog le.com...[color=blue]
              > <form method="post" action="do_some thing.asp?id=48 ">
              >
              > I don't even want to guess what happens there... :)[/color]

              There's nothing wrong with that... You have your form elements in $_POST and
              the 'id' value set in $_GET.


              Comment

              • Nikolai Chuvakhin

                #8
                Re: PHP equivalent of ASP code

                <barry@blues.no > wrote in message
                news:<T4e6b.759 45$ho5.1473959@ news2.telusplan et.net>...[color=blue]
                > "Nikolai Chuvakhin" <nc@iname.com > wrote in message
                > news:32d7a63c.0 309051726.2988d d2@posting.goog le.com...[color=green]
                > >
                > > <form method="post" action="do_some thing.asp?id=48 ">
                > >
                > > I don't even want to guess what happens there... :)[/color]
                >
                > There's nothing wrong with that... You have your form elements
                > in $_POST and the 'id' value set in $_GET.[/color]

                This is exactly why I used $_REQUEST in my code snippet...

                Cheers,
                NC

                Comment

                Working...