ASP to PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sclarkstone
    New Member
    • Jun 2008
    • 9

    ASP to PHP

    Hi. i have this page in ASP that pulls an entered postcode and displays the apropriate details for that area and i need to translate it into PHP. can anyone help?
    Im using a MySQL database.thank you for any help that you can give, much appreciated.

    Function RemoveWhiteSpac e(strText) 'function to remove white spaces from post code
    Dim RegEx
    Set RegEx = New RegExp
    RegEx.Pattern = "\s+"
    RegEx.Multiline = True
    RegEx.Global = True
    strText = RegEx.Replace(s trText, "")
    RemoveWhiteSpac e = strText
    End Function

    Dim nptCode, nptDistrict, noVals, origPostCode

    origPostCode = Request.Form("p ostcode") 'do not alter this value

    If (origPostCode <> "") Then 'we've come from the postcode search page

    Dim cleanPostCode, wiltsCode

    wiltsCode=False 'is it a wiltshire post code (see select statement)
    cleanPostCode = UCase(RemoveWhi teSpace(origPos tCode))

    Select Case Left(cleanPostC ode,2)
    Case "BA", "SN", "SP", "GL", "RG"
    wiltsCode=True
    Case Else
    Response.Redire ct("postcodesea rch.asp?e=nw&pc ode="&Request.F orm("postcode") &"")'go to post code error page with url extention (nw: not wiltshire)
    End Select

    If(wiltsCode)Th en
    Dim rs_pcode
    Dim rs_pcode_cmd
    Dim rs_pcode_numRow s

    Set rs_pcode_cmd = Server.CreateOb ject ("ADODB.Command ")
    rs_pcode_cmd.Ac tiveConnection = MM_maincms_STRI NG
    rs_pcode_cmd.Co mmandText = "SELECT npt_code, dis_name FROM q_npt_pcodes WHERE UCASE(pc_postco de) = ?"
    rs_pcode_cmd.Pr epared = true
    rs_pcode_cmd.Pa rameters.Append rs_pcode_cmd.Cr eateParameter(" param1", 200, 1, 10, cleanPostCode) ' adVarChar

    Set rs_pcode = rs_pcode_cmd.Ex ecute
    rs_pcode_numRow s = 0
    If(rs_pcode.BOF OR rs_pcode.EOF)Th en
    Response.Redire ct("postcodesea rch.asp?e=nf&pc ode="&Request.F orm("postcode") &"")'go to post code error page with url extention (nf: not found)
    Else
    nptCode=(rs_pco de.Fields.Item( "npt_code").Val ue)
    nptDistrict=(rs _pcode.Fields.I tem("dis_name") .Value)

    Dim MM_editCmd
    Set MM_editCmd = Server.CreateOb ject ("ADODB.Command ")
    MM_editCmd.Acti veConnection = MM_maincms_STRI NG
    MM_editCmd.Comm andText = "INSERT INTO t_pcode_searche s (pcsearch_pc, pcsearch_outcom e, pcsearch_stamp, pcsearch_ip) VALUES (?,?,?,?)"
    MM_editCmd.Prep ared = true
    MM_editCmd.Para meters.Append MM_editCmd.Crea teParameter("pa ram1", 202, 1, 10, Request.Form("p ostcode")) ' adVarWChar
    MM_editCmd.Para meters.Append MM_editCmd.Crea teParameter("pa ram2", 202, 1, 10, "ok") ' adVarWChar
    MM_editCmd.Para meters.Append MM_editCmd.Crea teParameter("pa ram3", 135, 1, 50, Now()) ' date
    MM_editCmd.Para meters.Append MM_editCmd.Crea teParameter("pa ram4", 202, 1, 50, Request.ServerV ariables("REMOT E_ADDR")) ' adVarWChar
    MM_editCmd.Exec ute
    MM_editCmd.Acti veConnection.Cl ose
    End If
    End If

    Else ' we've come from the map page

    'get url params
    nptCode=UCase(R equest.QueryStr ing("id"))
    nptDistrict=LCa se(Request.Quer yString("distri ct"))
    'set district to correct value
    Select Case nptDistrict
    Case "salisbury"
    nptDistrict="Sa lisbury"
    Case "swindon"
    nptDistrict="Sw indon"
    Case "nwilts"
    nptDistrict="No rth Wiltshire"
    Case "wwilts"
    nptDistrict="We st Wiltshire"
    Case "kennet"
    nptDistrict="Ke nnet"
    Case Else
    nptDistrict=""
    End Select

    End If

    'set variable to check we have everything we need (used later)
    If (nptCode="" Or nptDistrict="") Then
    novals=True
    End If


    Dim xmlDoc
    Set xmlDoc=Server.C reateObject("Mi crosoft.XMLDOM" )
    xmlDoc.async=fa lse
    xmlDoc.load(Ser ver.MapPath("xm l/nptdata.xml"))
    xmlDoc.setPrope rty "SelectionLangu age", "XPath"

    If xmlDoc.parseErr or.errorcode<>0 then
    Response.Write( xmlDoc.parseErr or.errorcode)
    Response.Write( "</br>Error processing xml file")

    End If


    nodepath="/NPTInfo/District[@DistrictName=' "&nptDistrict&" ']/NPT[@NPTCode='"&npt Code&"']"
    %>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Well, as rule goes, will we never just hand out code - feels like I'm saying this alot recently, and others, too. Anyway, what I can do is: refer you to some tutorials on mysql/php; but before that you might want to actually learn PHP.

    I hope all goes well in your project.

    ps. When you've shown some initiative and given us a show of how you'v had a go at this yourself, we'll be glad to help more!

    pss. When posting code in the forum - ASP in this case - wrap it in [code] tags. Makes it alot easier to read.

    Comment

    • sclarkstone
      New Member
      • Jun 2008
      • 9

      #3
      Sorry, i was hoping someone could translate it and then i could compare it to my attempt. here is my attempt, would you be able to take a look please?

      [HTML]mysql_select_db ($database_wpol conn, $wpolconn);
      $query_rs_pcode = "SELECT npt_code, dis_name FROM q_npt_pcodes ";
      $rs_pcode = mysql_query($qu ery_rs_pcode, $wpolconn) or die(mysql_error ());
      $row_rs_pcode = mysql_fetch_ass oc($rs_pcode);
      $totalRows_rs_p code = mysql_num_rows( $rs_pcode);
      ?>
      <?
      function removeWhiteSpac e($strText) { return preg_replace('~ \s+~', '', $strText);} //function to remove white spaces from post code

      $nptCode, $nptDistrict, $noVals, $origPostCode

      origPostCode = @$GET ["postcode"]; //do not alter this value
      If ($origPostCode ! =""){ //we've come from the postcode search page

      $cleanPostCode, $wiltsCode;
      }

      $wiltsCode="Fal se"; //is it a wiltshire post code (see switch statement )
      $cleanPostCode = ucwords($Remove WhiteSpace($ori gPostCode));

      switch substr($cleanPo stCode,0, 2); {
      case "BA", "SN", "SP", "GL", "RG";
      $wiltsCode="Tru e";
      default
      header("postcod esearch.asp?e=n w&pcode="& @$_GET("postcod e")&"");//go to post code error page with url extention (nw: not wiltshire)
      }

      if (!function_exis ts("GetSQLValue String")) {
      function GetSQLValueStri ng($theValue, $theType, $theDefinedValu e = "", $theNotDefinedV alue = "")
      {
      $theValue = get_magic_quote s_gpc() ? stripslashes($t heValue) : $theValue;

      $theValue = function_exists ("mysql_real_es cape_string") ? mysql_real_esca pe_string($theV alue) : mysql_escape_st ring($theValue) ;

      switch ($theType) {
      case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
      case "long":
      case "int":
      $theValue = ($theValue != "") ? intval($theValu e) : "NULL";
      break;
      case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theV alue) . "'" : "NULL";
      break;
      case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
      case "defined":
      $theValue = ($theValue != "") ? $theDefinedValu e : $theNotDefinedV alue;
      break;
      }
      return $theValue;
      }
      }










      //get url params
      $nptCode= ucword(@$_GET(" id"));
      $nptDistrict= lcword(@$_GET(" district"));
      //set district to correct value
      switch ($nptDistrict){
      case "salisbury" :
      $nptDistrict="S alisbury"
      case "swindon":
      $nptDistrict="S windon"
      case "nwilts":
      $nptDistrict="N orth Wiltshire"
      case "wwilts":
      $nptDistrict="W est Wiltshire"
      case "kennet":
      $nptDistrict="K ennet"
      default
      $nptDistrict=""
      }
      }
      //set variable to check we have everything we need (used later)
      If ($nptCode=="" Or $nptDistrict==" "){
      $novals="True";
      }

      $xmlDoc
      //Set xmlDoc=Server.C reateObject("Mi crosoft.XMLDOM" ) - not needed in PHP
      //xmlDoc.async=fa lse
      //xmlDoc.load(Ser ver.MapPath("xm l/nptdata.xml"))
      //xmlDoc.setPrope rty "SelectionLangu age", "XPath"

      If $xmlDoc.parseEr ror.errorcode<> 0 {
      echo "$xmlDoc.parseE rror.errorcode" ;
      echo "</br>Error processing xml file";

      }


      nodepath="/NPTInfo/District[@DistrictName=' "&nptDistrict&" ']/NPT[@NPTCode='"&npt Code&"']"
      %>
      <?php
      mysql_free_resu lt($rs_pcode);
      ?>[/HTML]

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Have you tried it yet? Does it work?

        Comment

        • sclarkstone
          New Member
          • Jun 2008
          • 9

          #5
          Yes i have tried it and unfortunatly it doesnt work.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by sclarkstone
            Yes i have tried it and unfortunatly it doesnt work.
            Give more details. Do you get an error message?

            Comment

            • sclarkstone
              New Member
              • Jun 2008
              • 9

              #7
              Ive made a few amends but still not working it gives the error Parse error: syntax error, unexpected '!' from this line:
              [HTML]if ($origPostCode ! == ""){ //we've come from the postcode search page[/HTML]

              i need the in there though because it is supposed to say is not equal to nothing.

              [HTML]mysql_select_db ($database_wpol conn, $wpolconn);
              $query_rs_pcode = "SELECT npt_code, dis_name FROM q_npt_pcodes ";
              $rs_pcode = mysql_query($qu ery_rs_pcode, $wpolconn) or die(mysql_error ());
              $row_rs_pcode = mysql_fetch_ass oc($rs_pcode);
              $totalRows_rs_p code = mysql_num_rows( $rs_pcode);
              ?>
              <?
              function removeWhiteSpac e($strText) { return preg_replace('~ \s+~', '', $strText);} //function to remove white spaces from post code


              $origPostCode = @$_GET ["postcode"]; //do not alter this value
              if ($origPostCode ! == ""){ //we've come from the postcode search page




              $wiltsCode="Fal se"; //is it a wiltshire post code (see switch statement )
              $cleanPostCode = ucwords($Remove WhiteSpace($ori gPostCode));


              switch substr($cleanPo stCode,0, 2); {
              case "BA", "SN", "SP", "GL", "RG";
              $wiltsCode="Tru e";
              default
              header("postcod esearch.asp?e=n w&pcode="& @$_GET("postcod e")&"");//go to post code error page with url extention (nw: not wiltshire)
              }

              if (!function_exis ts("GetSQLValue String")) {
              function GetSQLValueStri ng($theValue, $theType, $theDefinedValu e = "", $theNotDefinedV alue = "")
              {
              $theValue = get_magic_quote s_gpc() ? stripslashes($t heValue) : $theValue;

              $theValue = function_exists ("mysql_real_es cape_string") ? mysql_real_esca pe_string($theV alue) : mysql_escape_st ring($theValue) ;

              switch ($theType) {
              case "text":
              $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
              break;
              case "long":
              case "int":
              $theValue = ($theValue != "") ? intval($theValu e) : "NULL";
              break;
              case "double":
              $theValue = ($theValue != "") ? "'" . doubleval($theV alue) . "'" : "NULL";
              break;
              case "date":
              $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
              break;
              case "defined":
              $theValue = ($theValue != "") ? $theDefinedValu e : $theNotDefinedV alue;
              break;
              }
              return $theValue;
              }
              }










              //get url params
              $nptCode= ucword(@$_GET(" id"));
              $nptDistrict= lcword(@$_GET(" district"));
              //set district to correct value
              switch ($nptDistrict){
              case "salisbury" :
              $nptDistrict="S alisbury"
              case "swindon":
              $nptDistrict="S windon"
              case "nwilts":
              $nptDistrict="N orth Wiltshire"
              case "wwilts":
              $nptDistrict="W est Wiltshire"
              case "kennet":
              $nptDistrict="K ennet"
              default
              $nptDistrict=""
              }
              }
              //set variable to check we have everything we need (used later)
              if ($nptCode=="" Or $nptDistrict==" "){
              $novals="True";
              }

              $xmlDoc
              //Set xmlDoc=Server.C reateObject("Mi crosoft.XMLDOM" ) - not needed in PHP
              //xmlDoc.async=fa lse
              //xmlDoc.load(Ser ver.MapPath("xm l/nptdata.xml"))
              //xmlDoc.setPrope rty "SelectionLangu age", "XPath"

              if $xmlDoc.parseEr ror.errorcode<> 0 {
              echo "$xmlDoc.parseE rror.errorcode" ;
              echo "</br>Error processing xml file";

              }


              nodepath="/NPTInfo/District[@DistrictName=' "&nptDistrict&" ']/NPT[@NPTCode='"&npt Code&"']"
              %>
              <?php
              mysql_free_resu lt($rs_pcode);
              ?>[/HTML]

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                !=

                with one = sign.

                Comment

                • sclarkstone
                  New Member
                  • Jun 2008
                  • 9

                  #9
                  I just tried it and it is still giving the same error.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by sclarkstone
                    I just tried it and it is still giving the same error.
                    Same error message on same line number?

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Originally posted by r035198x
                      !=

                      with one = sign.
                      !== would still work. It compares an exact match - type and value. It was the space that was causing the error.

                      Comment

                      • sclarkstone
                        New Member
                        • Jun 2008
                        • 9

                        #12
                        That sorted it, thanks. now im getting Parse error: syntax error, unexpected T_STRING on line 21.

                        Comment

                        • Jeigh
                          New Member
                          • Jul 2007
                          • 73

                          #13
                          Originally posted by sclarkstone
                          That sorted it, thanks. now im getting Parse error: syntax error, unexpected T_STRING on line 21.
                          I think your problem is here:

                          switch substr($cleanPo stCode,0, 2); {

                          The semi colon isn't needed I believe, so try:

                          switch substr($cleanPo stCode,0, 2) {

                          Comment

                          • sclarkstone
                            New Member
                            • Jun 2008
                            • 9

                            #14
                            Just tried it, same error on the same line.

                            Comment

                            • Markus
                              Recognized Expert Expert
                              • Jun 2007
                              • 6092

                              #15
                              Originally posted by sclarkstone
                              Just tried it, same error on the same line.
                              Change the switch statement to this, see if it helps.

                              Code:
                              $pc = array("BA", "SN", "SP", "GL", "RG");
                              if( in_array ( substr ($cleanPostCode, 0, 2 ), $pc)
                                  $wiltsCode="True";
                              else
                                  # header...

                              Comment

                              Working...