Jump to content

scottybwoy

Members
  • Posts

    532
  • Joined

  • Last visited

    Never

Posts posted by scottybwoy

  1. Hmm, I tidied this up a bit today, but it's still rather long and it doesn't work, when calling it, it just returns with the enties back in it and no validation messages at all.  Here's the validation code (not quite complete) :
    [code]
    <?php
    function insertCust($data_array)
    {
    global $company, $custId, $data_array, $template;



    foreach ($data_array as $k => $v)
    {
    //take out trailing whitespace
    str_replace('/^\s+|\s+$/g', '', $v);

    if (empty($data_array[$k]))
    {
    $validate[$k] = FALSE;
    } else {
    $validate[$k] = TRUE;
    }

    return $validate;
    }

    if ($validate['company'] == FALSE)
    {
    $invalid = array(1 => "You forgot to insert customer name!");
    } else if ($validate['company'] == TRUE) {
    $company = $data_array['company'];
    $data_array['custId'] = $this->makeCustId($company);
    }

    if ($validate['tel'] == FALSE)
    {
    $invalid = array("You need to insert at least a main telephone number!");
    } else if ($validate['tel'] == TRUE) {
    $email = $data_array['email'];
    preg_match('/^\+?[0-9 ()-]+[0-9]$/', $email);
    }

    if ($validate['addLine1'] == FALSE)
    {
    $invalid = array("You need to insert at least a building name and street!");
    } else if ($validate['addLine1'] == TRUE) {
    $addLine1 = preg_replace('|[^a-z0-9 ]|i', '', $addLine1);
    }

    if ($validate['postcode'] == FALSE)
    {
    $invalid = array("C'mon how are we meant to know where they are without a postcode?");
    }

    if ($validate['type'] == FALSE)
    {
    $invalid = array("It'll make your life a whole lot easier if we know what type of company they are, don't you think?");
    } elseif ($validate['type'] == TRUE) {
    $type = $data_array['type'];

    $valid = $this->recordCheck('cusType', 'tpName', $type);

    if ($valid == FALSE)
    {
    $invalid = array("Sorry you have selected an incorect Type of Customer");
    }
    }

    if ($validate['pay'] == FALSE)
    {
    $invalid = array("Now how we gonna pay you if they don't pay us?");
    } elseif ($validate['pay'] == TRUE) {
    $type = $data_array['pay'];

    $valid = $this->recordCheck('cusPay', 'pyName', $type);

    if ($valid == FALSE)
    {
    $invalid = array("Sorry you have selected an incorect Payment Type for Customer");
    }
    }

    if (isset($invalid))
    {
    print_r($invalid);
    $template = CUST_PAGE;
    } else {
    //insert $data_array into table
    echo "info is correct";
    $template = CUST_PAGE;
    }
    }
    ?>
    [/code]
    Thanks for taking a look ;)
  2. Thanks Fellas,

    Sorry I was very tired yesterday, had 4hrs sleep over the w/e! and was also busy at work.  Just letting you know cos with a fresh head this morning, I noticed that I had confused myself by looking at a wrong function that I had already completed and tried to make it do something else, lol, sorry for taking your time.

    However let me go on to what I am actually trying to do.  I want to create server side validation for my form.  I already have client side validation via JavaScript.  Am I going the right way about it, cos it seems a bit long, here's what I have so far :

    [code]
    <?php
    function insertCust($data_array)
    {
    global $company, $custId, $data_array, $template;

    $company = $data_array['company'];

    foreach ($data_array as $v)
    {
    //take out whitespace
    str_replace('/^\s+|\s+$/g', '', $v);
    }

    if (empty($company))
    {
    echo "You forgot to insert customer name!";
    $template = CUST_PAGE;
    break;
    } else if (!empty($company)) {
    $data_array['custId'] = $this->makeCustId($company);
    }

    if (empty($data_array['tel']))
    {
    echo "You need to insert at least a main telephone number!";
    $data_array['custId'] = $this->makeCustId($company);
    $template = CUST_PAGE;
    break;
    } else if (!empty($data_array['tel'])) {
    ('/^\+?[0-9 ()-]+[0-9]$/');
    }

    if (empty($data_array['addLine1']))
    {
    echo "You need to insert at least a building name and street!";
    $data_array['custId'] = $this->makeCustId($company);
    $template = CUST_PAGE;
    break;
    }

    if (empty($data_array['postcode']))
    {
    echo "C'mon how are we meant to know where they are without a postcode?";
    $data_array['custId'] = $this->makeCustId($company);
    $template = CUST_PAGE;
    break;
    }

    if (empty($data_array['type']))
    {
    echo "It'll make your life a whole lot easier if we know what type of company they are, don't you think?";
    $data_array['custId'] = $this->makeCustId($company);
    $template = CUST_PAGE;
    break;
    }

    if (empty($data_array['pay']))
    {
    echo "Now how we gonna pay you if they don't pay us?";
    $data_array['custId'] = $this->makeCustId($company);
    $template = CUST_PAGE;
    break;
    }

    foreach ($data_array as $k => $v)
    {
    $_GET[$k];
    }
    }

    function addClient()
    {
    global $clientId, $custId, $data_array, $template;

    $custId = $_GET['custId'];
    $company = $_GET['company'];

    if (empty($company) && empty($custId))
    {
    echo "Dying";
    die("You forgot to select a company!");
    } else {

    $this->makeClientId($custId);

    $data_array = array("company" => $company, "clientId" => $clientId);
    $template = CLIENT_PAGE;
    }
    }
    ?>
    [/code]
  3. Hi Billys

    I am trying to make an assosiative array of $_GET / $_POST values, is this poss.  May it be possible to get the complete url and extract the $k = $v and put them into an associative array?  Could someonr point me in the right direction of functions to use, or if my method asked above is viable.  Thanx
  4. Hi All,

    I have a template that has a form information on it with two Submit buttons, for two seperate functions.

    However in my php script, it is not picking it up and proccessing what I want.

    This is the script I'm using :
    [code]
    <?php
    if (!empty($data_array))
      {
      if (isset($_GET['addclient']) && $_GET['addclient'] == "Add Client")
      {
      $this->addClient();
      } elseif (isset($_GET['commit']) && $_GET['commit'] == "Commit") {
      $this->insertCust($data_array);
      }

      $template = CUST_PAGE;
      } else {

      $custId = $this->makeCustId($company);
      $data_array = array("company" => $company, "custId" => $custId);

    $template = CUST_PAGE;
      }
    ?>
    [/code]

    The URI displays the variable passed to the data_array, however is not picking up &addclient=Add+Client part asked for in the $_GET['addclient'] section that I want.  All it displays is the $template = CUST_PAGE; and not $template = CLIENT_PAGE; That I want, issued from my addClient() script.

    Any ideas?  Thanks
  5. Hmm, no I can't do that because of the construct of my site, this is how the page is loaded :
    [code]
    <?php
        function displayHome()
        {
        global $company, $product, $template;

        $content = $_REQUEST['content'];
        $company = $_REQUEST['company'];
        $product = $_REQUEST['product'];

        if (!empty($company))
        {
        require(COMPANY_SCR);

        } else if (!empty($product)) {
        include(PRODUCT_SCR);
       
        } else if ($content == "") {
        global $user_level;

        $user_level = $_SESSION["USER_LEVEL"];
    //    $template = include(ADMIN_PAGE);
        $template = $this->loadTemplate(ADMIN_PAGE);

        } else {
    $template = $this->LoadTemplate(TEMPLATE_DIR . "//" . $content . ".inc");
    }

    require_once(HEADER);

    echo "<table width=100% valign='left'><tr><td rowspan='2' width=60%>";
    print $template;
    echo "</td><td width=40%>";
    include(TEMPLATE_DIR . "/inform.php");
    echo "</td></tr><tr><td>";
    include(TEMPLATE_DIR . "/prodDisp.php");
    echo "</td></tr><tr><td colspan='2'>";
      require_once(TEMPLATE_DIR . '/footer.php');
    echo "</td></tr></table>";

    }
    ?>
    [/code]

    Do you see what I mean?
  6. Sorry mjdamato, that won't/doesn't work.  If you look at my loadTemplate function posted above, you will see that what it does is tack all the content out of it and print it into the $template var.  So I thaught your option would just work, however it's just being passed to the client-side browser, so would I need to edit that function to account for this?  If so what would you suggest I do.

    Thanks
  7. That's simpler than I thaught, great!!
    However the parsing of the HTML thinks that the <?php if ($user... > is html code and so breaks the script.  Do I need a / or to parse my templates differently? This is my template loader :
    [code]
    <?php
    function loadTemplate($file)
    {
    global $template;

    $strfile = $file;

        if (!file_exists($strfile)) print"Loading template failed!";
        $thisfile = file($strfile);

        while(list($line,$value) = each($thisfile))
        {
            $value = ereg_replace("(\r|\n)","",$value);
            $template .= "$value\r\n";
        }

        return $template;
    }
    ?>
    [/code]
    Then $template is echoed out into the page stucture I have formed.  This doesn't seem to implement the php code.  Any Ideas?
  8. Thanks, crayo, I already have that set up, already, the array was just a refrence for the template.

    I like your idea mjdamato I didn't think of that.  But I still have trouble, grasping the concept.  Would I need to have diffrent templates for each user level, as that seems long.  How do you mean to set up the templates?

    Lol after just getting that section working!  The problem was that I did not define $user_level & $template.

    Yeah I realised your second points on Problem 2 and have been fidling with it, I now have it working this is code :
    [code]
    <?php
    function makeCustId($company)
    {
    global $company, $rowInt, $custId;

    $comp = preg_replace('|[^A-Z]|i', '', $company);
      $comp = substr($comp, 0, 3);

      $this->appCon();

      $sql = "SELECT custId FROM customers WHERE custId LIKE '" . $comp . "%'";
    $result = mssql_query($sql);

    if ($result == TRUE)
    {
    $rowInt = sprintf("%03d", 0);

    while ($custCount = mssql_fetch_row($result))
    {
    $custCount = $custCount[0];
    $custInt = substr($custCount, 3, 5);

    if ($custInt != $rowInt)
    {
    break;
    }

    $rowInt = ++$custInt;
    $rowInt = sprintf("%03d", $rowInt);
    $custCount = $comp . $rowInt;
    $custId = $custCount;
    }

    return $custId;

    } else {
    $custId = $comp . sprintf("%03d", 0);
    }

      return $custId;
    }
    ?>
    [/code]
  9. After working a little more, I think I have made a little more sense of it, but still have not got them working.  Here is the updated scripts :

    Problem 1) user level display :
    [code]
    <?php
    function templateLevel($file)
    {
    global $template, $user_level, $USER_TYPE;

    $this->userLevel($this->getUserName());
    $this->loadTemplate($file);

    $USER_TYPE = array('9' => 'Admin',
      '4' => 'Man',
    '3' => 'Tech',
    '2' => 'Acc',
    '1' => 'Sale');

    foreach ($USER_TYPE as $k => $v)
    {
    if ($k <= $user_level)
    {
    $template = str_replace("<!-- BEGIN " . $v . "", "", $template);
    $template = str_replace("END " . $v . " -->", "", $template);
    }

    }

    return $template;
    }
    ?>
    [/code]
    This seems to not uncomment the template at all, can I use an "if" within a foreach?

    Problem 2) CustId Insert
    [code]
    <?php
    function makeCustId($company)
    {
    global $company, $rowInt, $custId;

    $comp = preg_replace('|[^A-Z]|i', '', $company);
      $comp = substr($comp, 0, 3);

      $this->appCon();

      $sql = mssql_query("SELECT custId FROM customers WHERE custId LIKE '" . $comp . "%'");
    $custCount = mssql_fetch_row($sql);

    if (isset($custCount))
    {
    $rowInt = sprintf("%03d", 0);
    $custCount = $comp . $rowInt;

    while ($custCount == mssql_fetch_row($sql))
    {
    $rowInt = substr($custCount, 4, 6);
    $rowInt = $rowInt++;
    $rowInt = sprintf("%03d", $rowInt);
    $custCount = $comp . $rowInt;
    echo $custCount . "<br>";
    return $custCount;
    }

      $custId = $comp . $rowInt;

    } else {
    $custId = $comp . sprintf("%03d", 0);
    }

      return $custId;
    }
    ?>
    [/code]
    This still does not increment the custId as when it returns, it still shows as "000".  Could this be due to the while loop executing the mssql_fetch_rows for the second time?  I tries using mssql_result for the first one, but it didn't like it.  Wrong Perameter Count.
  10. I am trying to get my head around while loops and also using array data with them, or with another kind of loop.  What I am trying to do isn't too hard, but is just out of my reach, so was hoping someone here could help.  I have two problems, but if I get help with one I may be able to work out the other.  Here goes :

    First problem is for displaying the right content to users, for this I have templates made with comment blocks for the user sections.  Then I have an array containing the type of users and a corresponding integer like so :
    [code]
    <?php
    $USER_TYPE = array('9' => 'Admin',
      '4' => 'Man',
    '3' => 'Tech',
    '2' => 'Acc',
    '1' => 'Sale');
    ?>
    [/code]
    Then I have a function that is meant to find out which userLevel they are and to scan the template for the areas they are allowed and uncomment them, although I'm unsure how to put this into practice.  This is what I have so far :
    [code]
    <?php
    function templateLevel($file)
    {
    global $template, $username;

    $this->userLevel($this->getUserName());
    $this->loadTemplate($file);

    while ($USER_TYPE[$k] <= $user_level)
    {
    $type = $USER_TYPE[$k];
    $template = str_replace("<!-- BEGIN " . $type . "", "", $template);
    $template = str_replace("END " . $type . " -->", "", $template);

    }

    return $template;
    }
    ?>
    [/code]
    As you can see, it is meant to scroll through the array like a foreach untill it reaches the $user_level in the $USER_TYPE key value.  Then I want it to replace the $type as the value in the $USER_TYPE array for each key upto $user_level.

    My other problem, may be a little more complex but I still not got my head around while loops so it may not be ;) .  What I am trying to do here, is create a customer ID from the name, that bit works fine, however I want it to find a gap or the final id number for insertion in the database that is the problem.  Please take a look at my code :
    [code]
    <?php
    function makeCustId($company)
    {
    global $company, $rowInt, $custId;

    $comp = preg_replace('|[^A-Z]|i', '', $company);
      $comp = substr($comp, 0, 3);
    echo $comp . "<br>";

      $this->appCon();

      $sql = mssql_query("SELECT custId FROM customers WHERE custId LIKE '" . $comp . "%'");
    $rowcount = mssql_fetch_row($sql);

    if (isset($rowcount)) {
    $start = 1;
    } else {
    $start = 0;
    }

    echo $start;
    $rowInt = sprintf("%03d", $start);
    echo $rowInt . "<br>";

    while (substr(mssql_fetch_row($sql), 4, 6) == $rowInt) {
    $rowInt = substr(mssql_fetch_row($sql), 4, 6);
    $rowInt = ++$rowInt;
    $rowInt = sprintf("%03d", $rowInt);
    echo $rowInt . "<br>";
    }

      $custId = $comp . $rowInt;

      return $custId;
    }
    ?>
    [/code]
    So as you can see $rowInt is a 3char integer that is appended to $cust to form the complete $custId.  I want it to go through the database where the $custId starts the same, and when it finds no incremental value, to create it and come out of the loop.  This has all just gone a little bit confusing for me, with my lack of experience.  But I'm really getting into this programming malarky so really want to nail this important thaught process.  Thanks for helping
  11. OK, thanks for all your help so far, now I have taken the initiative to implement a while loop, to find any gap's in my custId's so that I don't get any duplicates, from using $rowcount = mssql_num_rows($sql);  But I've never used a while loop b4 so if you could help me on this one last point, I should be on my way, Thanks.

    Here's what I have now :
    [code]
    <?php
    function makeCustId($company)
    {
    global $company, $rowInt, $custId;

    $comp = preg_replace('|[^A-Z]|i', '', $company);
      $comp = substr($comp, 0, 3);

      $this->appCon();

      $sql = mssql_query("SELECT custId FROM customers WHERE custId LIKE '" . $comp . "%'");
    $rowcount = mssql_fetch_row($sql);

    if (isset($rowcount)) {
    $start = 1;
    } else {
    $start = 0;
    }

    $rowInt = sprintf("%03d", $start);

    while (substr(mssql_fetch_row($sql), 4, 6) == $rowInt) {
    $rowInt = substr(mssql_fetch_row($sql), 4, 6);
    $rowInt = ++$rowInt;
    $rowInt = sprintf("%03d", $rowInt);
    echo $rowInt . "<br>";
    }

      $custId = $comp . $rowInt;

      return $custId;
    }
    ?>
    [/code]

    I know there needs to be something more, but I'm not sure what.  Thanks for all your time again.
  12. Hmm, yeah we sort of understand each other.  If you look at the code in the first entry, you'll see that I have done something similar.  As the max suffix would be diffrent for each beginning company, the new table would be just as big as the first, (well in terms of rows sort of).

    What I mean is that if I have these companies :
    Boo Radleys, Book Stoor, Comics, Communication Center.
    Their Cust Id's may be : BOO001, BOO002, COM001, COM002.

    So what you were saying is that I'd have to have <b>name</b> Sufix BOO <b>value</b> 002 etc.

    What I have done, is create the initial first letters and SELECT similar from the CustId value then count the Rows:
    [code]
    <?php>
    $comp = substr($comp, 0, 2);
    $sql = mssql_query("SELECT custId FROM customers WHERE custId LIKE '" . $comp . "%'");
    $rowcount = mssql_num_rows($sql);
    $rowInt = ++$rowcount;
    ?>
    [/code]

    This is working fine, until a record gets deleted (thanks for making me think of that, I'll cross that bridge in a bit) But how do I get it to put the 00 before the int i.e 1 => 001?

    I've just fount sprintf() but it's not working this is what I have now :
    [code]
    <?php
    if ($rowInt <= '99')
    {
      $rowInt = sprintf("%03d", $rowInt);
      return $rowInt;
    }
    ?>
    [/code]

    Then a simple concatonation of $comp . $rowInt as before.  Thanks again
  13. Thanks Obsidian, however now I get a wrong parameter count, I echoed the $company var and it appears correct, (uppercase company name) looking in the manual, I think I may need preg_match or a string function.  What I want it to do is strip out all numbers and non alpha characters, so if $company = 1st o'brian then it would return $comp = stobrian.  Then further down the line is it possible to add a string to an integer so that if $rowInt = 4 it would become $rowInt = 004.  Or am I going wrong their too, thanks for your time and help.
  14. Hi php freaks,

    I have a function to make a customer ID from the customers name, however I can't get it to work.  I have placed in some echo's to see where it is not working, and it seems to fail on preg_match.  And that provides a knock on effect for the rest of the function.  So I was wondering if anyone can spot wher I'm going wrong.

    Here's the code :
    [code]
    <?php
    function makeCustId($company)
    {
    global $company, $rowInt, $custId;

    $comp = preg_match('|^[A-Z]+$i |', $company);
    echo $comp;
      $comp = substr($comp, 0, 2);


      $this->appCon();

      $sql = mssql_query("SELECT custId FROM customers WHERE custId LIKE '" . $comp . "%'");
      $rowcount = mssql_num_rows($sql);
      $rowInt = ++$rowcount;

      if ($rowInt <= 9)
      {
      $rowInt = "00" . $rowInt;
     
      return $rowInt;
      } elseif ($rowInt <= 99) {
      $rowInt = "0" . $rowInt;

      return $rowInt;
      }

      $custId = $comp . $rowInt;

      return $custId;
    }
    ?>
    [/code]

    What I want it to do is Take the input for customer name, strip all the numbers, spaces and other characters, make it into 3 letters then compare it with the others in the database and increment the last 3 digits by 1.  i.e if database has TES001 and I input TEST LTD it would create TES002.  Thanks for your help.
  15. Yeah you can get JavaScript to run a php file, not sure how on a seperate server.  I'm not too hot with JS but here's a snippet I fished out of an external .js file I use to get results from a database and use them in a drop down menu.
    [code]
    <?php

    var ajax_list_externalFile = "../lib/php/ajax-list-companies.php";

            ajax_optionDiv.innerHTML = '';
    var ajaxIndex = ajax_list_objects.length;
    ajax_list_objects[ajaxIndex] = new sack();
    var url = ajax_list_externalFile + '?' + paramToExternalFile + '=1&letters=' + inputObj.value.replace(" ","+");
    ajax_list_objects[ajaxIndex].requestFile = url; // Specifying which file to get
    ajax_list_objects[ajaxIndex].onCompletion = function(){ ajax_option_list_showContent(ajaxIndex,inputObj,paramToExternalFile); }; // Specify function that will be executed after file has been found
    ajax_list_objects[ajaxIndex].runAJAX(); // Execute AJAX function
    ?>
    [/code]

    Don't know If you can make more sense of that but I hope it helps ;)
  16. Hello buddies,

    I'm trying to insert data into a template file I have, if the content is available.  At the moment the template is loaded into the right place however the fields are not inserted :( here's my replacement code :
    [code]
    <?php
    function insertCompInfo()
    {
    global $recordset_array, $template;

    $this->LoadTemplate(DISPLAY);

    foreach ($recordset_array as $key => $value)
    {
    str_replace("<!-- " . $key . " -->", $value, $template);
    }

    return $template;
    }
    ?>
    [/code]
    This is allot simpler than the tutorial I was using but seems like it should do the job, however it doesn't.
    Here is also a screen print of a test array ($recordset_array) :

    Array
    (
        [custId] => DIG001
        [company] => DIGITROL LTD
        [addLine1] => Test
        [addLine2] => Test
        [townCity] => Test
        [county] => Test
        [country] => UK
        [postcode] => WA1 3SS
        [type] => 1
        [pay] => 1
        [tel] => 1234567890
        [fax] => 1234567890
        [email] => enquiries@digitrol.com
        [web] => www.digitrol.com
    )

    And here is how I have my template replacement entries :
    [code]
    <?php
    <!-- START_CUST -->
    <table width="570" class="form">
    <tr>
    <td width="160" class="labelL">Company Name</td>
    <td width="200"><input type="text" name="company" value="" size="40" style="input"><!-- [company] --></td>
    <td width="160" class="labelR">Customer Id</td>
    <td width="50"><input type="text" name="custId" value="" size="15" style="input"><!-- [custId] --></td>
    </tr>
    </table>
    ...etc
    <!-- END_CUST -->
    [/code]
    So as you can see I am using an associative array, and using the fields in my SQL database in the template for easy transfer.  But all I have is just the template with no fields filled in.  Thanks in advance ;)
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.