Jump to content

OldWest

Members
  • Posts

    296
  • Joined

  • Last visited

    Never

Posts posted by OldWest

  1. Hello,

     

    I have a table that has 5 fields of data.

     

    In many cases, not all of the fields have data in them.

     

    Each time the View action of the Controller is requested, I would like to only display the fields that have content in them AND the tabular structure of the data will change based on the content as well.

     

    So basically, I think I will need a unique View file for each type of data schema. And there are 3 schemes. For example,

     

    Schema  |  1  |  2  |  3  |  4  |  5  |

     

    A                X      X      X      X      X

    B                X                        X      X

    C                        X      X              X

     

    As you can see, each schema has different data populated.

     

    This tells me I will need 3 View structures to accommodate these schema's.

     

    Can anyone make a recommendation on the best way to handle this in the CakePHP framework. I am thinking an if condition in the Controller, and then render() based on that, but its seems there would be a more conventional method.

     

    Thanks for any thoughts on this.

     

  2. I've been cracking at this for the part of 2 days now, and I cannot get my regex to work properly in my script.

     

    /^[A-Za-z][\w\-',.]*(?<![^A-Za-z][\w\-\'\,\.])$/i

     

    The above is for a last name field, and it needs to be able to accept: Roberts, Jr. (as an example)...

     

    As you can see in my regex, I am allowing all of these characters.. And it's still not validating it.

     

    You can test to see what I mean here:

     

    <?php 
    
    $last_name = "Roberts, Jr.";
    
    if (!preg_match("/^[A-Za-z][\w\-',.]*(?<![^A-Za-z][\w\-\'\,\.])$/i", $last_name )) {
              echo "Sorry no dice!";
          }
    
    ?>

     

     

     

  3. Nice idea. The double for loops seem like the best solution. . Thx.

     

    they are in between because you are looping through each time for each input and each radio

     

    i tested this try it this way

     

    echo "<form action='{$this->actionValue}' method='post'>\n\n";
        for ($j = 1; $j <= sizeof($this->fields); $j++) {
            echo "<p>\n<label>{$this->fields[$j-1]['label']} : </label>\n";
            echo "<input type='text' name='{$this->fields[$j-1]['name']}'>\n</p>\n\n";
        }
        for ($i = 1; $i <= sizeof($this->radios); $i++) {
            echo "\n<label>{$this->radios[$i-1]['rlabel']} : </label>\n";
            echo "\n<input type='radio' name='{$this->radios[$i-1]['rname']}' value='{$this->radios[$i-1]['rvalue']}'>\n\n";
        }		  	
    echo "\n<br/>\n\n<input type='submit' value='{$this->submit}'>\n</form>";
    

  4. I like this. i was just looking at his code and wondering, surely there's a way to not have to be so redundant.

     

    the question i have is.. am i correct in thinking that you physically don't have a $fname, $lname $email in your code anywhere and have to rely on your memory? if so i'm outta luck hehe

     

    Yes. You are thinking correct. Read up on: variable variables

     

    Basically the variable variable works like this:

     

    $variable = "ABC";
    $$variable = "CDE";
    echo $ABC; // CDE
    

    The value assigned to $variable becomes the variable itself. So ABC becomes $ABC becomes CDE in that case when the variable variable $$variable is used.

     

    There are  LOT of uses for the variable variable I am finding lately that really strip out code redundancy! I still have a lot to learn with it, but it's worth the time to read up on.

  5. I am doing some practice with classes. MY class below echoes out input and radio fields based on the instantiation of the Form class (the code below is working!), but I am trying to resolve how to get my form radio fields so they are not in between every other form field as they are supposed to be next to eachother. If you run the below, you will see what I mean. And also is my approach at all sensible? I am only doing this for learning experience, so any advice please.

     

    <?php
      class Form
      {
          private $fields = array();
          private $radios = array();
          private $actionValue;
          private $submit = "Submit Form";
          private $Nradios = 0;
          private $Nfields = 0;
          function __construct($actionValue, $submit)
          {
              $this->actionValue = $actionValue;
              $this->submit = $submit;
          }
          function displayForm()
          {
              echo "<form action='{$this->actionValue}' method='post'>\n\n";
              for ($j = 1, $i = 1; $j <= sizeof($this->fields), $i <= sizeof($this->radios); $j++, $i++) {
                  echo "<p>\n<label>{$this->fields[$j-1]['label']} : </label>\n";
                  echo "<input type='text' name='{$this->fields[$j-1]['name']}'>\n</p>\n\n";
                  echo "<p>\n<label>{$this->radios[$i-1]['rlabel']} : </label>\n";
                  echo "\n<input type='radio' name='{$this->radios[$i-1]['rname']}' value='{$this->radios[$i-1]['rvalue']}'>\n</p>\n\n";
              }
              echo "\n\n<input type='submit' value='{$this->submit}'>\n</form>";
          }
          function addField($name, $label)
          {
              $this->fields[$this->Nfields]['name'] = $name;
              $this->fields[$this->Nfields]['label'] = $label;
              $this->Nfields = $this->Nfields + 1;
          }
          function addRadio($rname, $rvalue, $rlabel)
          {
              $this->radios[$this->Nradios]['rname'] = $rname;
              $this->radios[$this->Nradios]['rvalue'] = $rvalue;
              $this->radios[$this->Nradios]['rlabel'] = $rlabel;
              $this->Nradios = $this->Nradios + 1;
          }
      }
    ?>
    
    <?php
      $contact_form = new Form("process.php", "Submit Data >>");
      $contact_form->addField("first_name", "First Name");
      $contact_form->addField("last_name", "Last Name");
      $contact_form->addRadio("gender", "male", "Male");
      $contact_form->addRadio("gender", "femail", "Female");
      $contact_form->displayForm();
    ?>

     

  6. I'll throw in a quick note, instead of calling mysql_real_escape_string to every single post variable manually, something like this is easier on the fingers and eyes!

    foreach ($_POST as $key => $val)
    {
    $$key = mysql_real_escape_string($val);
    }
    

     

    Zurev,

     

    Thanks for the idea.

     

    I like that a lot. The variable variable put to use to tidy up code. I'll see if I can implement it.

     

    That should make each post name be a variable equivalent to it's escaped version, i.e. the $_POST['fname'] will then create a variable named $fname with the contents of

    mysql_real_escape_string($_POST['fname']);
    

  7. Hello,

    My script below IS finally working, but I was hoping for some aggressive, anal comments for critique.

     

    Keep in mind, I am developing for a php4 platform otherwise I would have used a newer php5 validation function.

     

    <?php
      if (isset($_POST['btnSubmit'])) {
          $first_name = mysql_real_escape_string($_POST['fname']);
          $last_name = mysql_real_escape_string($_POST['lname']);
          $title = mysql_real_escape_string($_POST['title']);
          $company = mysql_real_escape_string($_POST['company']);
          $address1 = mysql_real_escape_string($_POST['address1']);
          $address2 = mysql_real_escape_string($_POST['address2']);
          $city = mysql_real_escape_string($_POST['city']);
          $zip = mysql_real_escape_string($_POST['zip']);
          $phone = mysql_real_escape_string($_POST['phone']);
          $fax = mysql_real_escape_string($_POST['fax']);
          $email = mysql_real_escape_string($_POST['email']);
          
          if (!preg_match("/^[A-Za-z' -]{1,75}$/", $first_name)) {
              $error[] = "Please enter a valid first name.";
          }
          
          if (!preg_match("/^[A-Za-z' -]{1,75}$/", $last_name)) {
              $error[] = "Please enter a valid last name.";
          }
      
      if ($first_name === $last_name && $first_name != "") {
              $error[] = "First Name and Last Name cannot be the same.";
          }
          
          if (!preg_match("/^[A-Za-z' -]{1,150}$/", $company)) {
              $error[] = "Please enter a valid company name.";
          }
          
          if (!preg_match("/^[A-Za-z' -.]{1,150}$/", $title)) {
              $error[] = "Please enter a valid Title.";
          }
          
          if (!preg_match("/^[A-Za-z0-9' - . ]{1,150}$/", $address1)) {
              $error[] = "Please enter a valid mailing address.";
          }
      
      if (!preg_match("/^[A-Za-z0-9' - . ]{1,150}$/", $city)) {
              $error[] = "Please enter a valid city.";
          }
      
      if (!preg_match("/^[0-9' - . ( ) ]{1,150}$/", $phone)) {
              $error[] = "Please enter a valid phone number.";
          }
      
      if (!preg_match("/^[0-9' - . ( ) ]{1,150}$/", $fax)) {
              $error[] = "Please enter a valid fax number.";
          }
          
          if (!preg_match("/([a-z][a-z0-9_.-\/]*@[^\s\"\)\?<>]+\.[a-z]{2,6})/i", $email)) {
              $error[] = "Please enter a valid email address in the format: start@middle.end.";
          }
          
          if (is_array($error)) {
              echo "<div id='errorWrapper'><h2>There are errors in your input. Please correct the following fields:</h2>";
              foreach ($error as $err_message) {
                  echo "<span class='errorText'> >> $err_message" . "</span><br />";
              }
              echo "</div>";
              
              include('../includes/attendee_registration_form.php'); // this is the form
              exit();
          }
          
          else {
              include('../includes/attendee_registration_mailer.php'); // this send the email and populates the table
          }
      } else {
          include('../includes/attendee_registration_form.php'); // this is the form
          exit();
      }
    ?>

  8. My script IS working, but I can't get around a blank array error when no errors exist.

     

    Below is my code, and as you can see, I am being handed this:

    Notice: Undefined variable: error in C:\wamp\www\php\form_validation.php on line 19 as a result of my ... if (is_array($error)) { ... .. I could do if (@is_array($error)) { (note the @), but I hate using that thing... I've tried several things with no luck, so any ideas welcome at this point.

     

    <?php
      if (isset($_POST['set_test'])) {
          if (!preg_match("/^[A-Za-z' -]{1,50}$/", $_POST['first_name'])) {
              $error[] = "Please enter a valid First Name";
          }
          if (!preg_match("/^[A-Za-z' -]{1,50}$/", $_POST['last_name'])) {
              $error[] = "Please enter a valid Last Name";
          }
          if (is_array($error)) {
              foreach ($error as $err_message) {
                  echo $err_message . "<br />";
              }
          }
      }
    ?>

  9. I had a bunch of issue with my regex and script, but I think I am just about solved... Will post solved when there:

     

    <?php 
    if(isset($_POST['set_test'])) {
    
    if(!preg_match("/^[A-Za-z' -]{1,50}$/",$_POST['first_name'])) 
    { $error[] = "Please enter a valid First Name"; }
    else { $error[] = ""; }
    
    if(!preg_match("/^[A-Za-z' -]{1,50}$/",$_POST['last_name'])) 
    { $error[] = "Please enter a valid Last Name"; }
    else { $error[] = ""; }
    
    if(is_array($error)) {
    
    foreach($error as $err_message)
    {
    
    echo $err_message . "<br />";
    
       }
    
    }
    //exit();
    }
    ?>

     

  10. Thanks for the tips. I am still working on this, but I was able to get this far with some results. I still have some mixed results though.

     

    What I don't understand is why preg_match seems to return TRUE if the results are met, and my errors are thrown then.. Does it seem like I am doing this efficiently?

     

     

    <?php 
    if(isset($_POST['set_test'])) {
    
    if(preg_match('/[^A-Za-z]/',$_POST['first_name'])) 
    { $error[] = "Please enter a valid First Name"; }
    else { $error[] = ""; }
    
    if(preg_match('/[^A-Za-z]/',$_POST['last_name'])) 
    { $error[] = "Please enter a valid Last Name"; }
    else { $error[] = ""; }
    
    echo $error[0];
    echo $error[1];
    
    }
    ?>
    
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
          First Name:<br />
             <input name="first_name" type="text" size="50" maxlength="50" /><br /><br />
             Last Name:<br />
             <input name="last_name" type="text" size="50" maxlength="50" /><br /><br />
      <input name="set_test" type="hidden" value="" />
             <input type="submit" /><br /><br />
          </form>

  11. Been screwing around on Google for about 3 hours trying to find a tutorial on what I am trying to do with absolutely no luck!

     

    I am simply trying to get my test script to echo errors from an array when a form criteria does not validate. This is my final revision which is still not working!

     

    Can someone please tell me what I am doing wrong? No matter what I do, I can't get away from this error:

     

    Notice: Undefined variable: error in C:\wamp\www\php\form_validation.php on line 13

     

     

    <?php 
    $o = $error[]; // test
    echo $o; // test
    
    if(!preg_match('/[^0-9A-Za-z]/',$_POST['first_name'])) 
    { $error[] = "Please enter a valid First Name";                                               
    ?>
    
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
          First Name:<br />
             <input name="first_name" type="text" size="50" maxlength="50" /><br /><br />
      
             <input type="submit" /><br /><br />
          </form>

     

  12. ive got a stressful one on my hands and im looking for any input or advice on where is should take this.

     

    im working on a query system, and i need to determine the best way to set up my tables for a few of the characteristics.

     

    my form fields are as follows:

     

    Age: text-entry

    Spouse Age: text-entry

    Number of Children: text-entry

    Select Effective Date (MM/DD/YYYY)  

    Zip Code: text-entry

    Companies: checkboxes

     

    the main problem i am running into is each company has different plans (plans are the results of the query from the above form)..

     

    and each company has different age ranges for each plan.

     

    so i think my first question is (and i have not been able to find a definite answer all day on this) is there a way to search a range in mysql based on an actual range notation like: 0-19?? so that any number entered in "Age" for example, 10 ,would return true for the 0-19?

     

    maybe i am going about this all wrong, but that's what i was looking for any advice on the best way to set this up.

  13. i have a small query system i am working on.

     

    simply put, when the form will run a query on several results, one of which is age. the age must be entered manually in a text field (on the user end when doing the search).

     

    now. the results that output will be reliant on that age BUT there are age ranges.

     

    for example, i have ranges of: 0-19, 20-24, over 65, under 65 etc and kind of random about this.

     

    and if i enter the my age as 22 in the search field, positive matches would of course be 20-24 and under 65.

     

    i have tables: product and age.

    each product has an age range or a plus range (like over 65).

    so all products would be returned that match the age entered (of course which products have a matching age_range_id)

     

    i hope my question is clear. I am basically trying to resolve how my table relationship and structure should be to best accommodate this type of querying or a better solution altogether.

     

    thanks for any advice

  14. i have a stupid simple problem here, but ive never done this exactly this way before and im having a tough time working it out. was looking for any suggestions.

     

    my script is working fine:

     

    $table_name = "plan";
       
    $sql = "SELECT id, plan_name FROM $table_name ORDER BY plan_name";
    
    $query = mysql_query($sql);
    
    if(mysql_num_rows($query) > 0)
    {
    
    echo "<table>";
    while($row = mysql_fetch_array($query))
    {
      echo "<tr><td>" . $row['plan_name'] ."</tr></td>";
    }
    echo "</table>"; ... 

     

    but i have another table  company that are related to plan_name

     

    i have the company_id field in my plan table for the relationship. and that id is related to the company id field of course.

     

    all i am trying to do is display the company_name next to $row['plan_name'], so i know which plans are related to which company.

     

    i think i need to create two seperate queries, but is there a way to include everyone in one query? is there a better way?

     

    here is my sql:

     

    plan table:

    `id` int(11) NOT NULL AUTO_INCREMENT,
      `company_id` int(11) NOT NULL,
      `plan_name` varchar(255) NOT NULL, ... 

     

    company table

    `id` int(11) NOT NULL AUTO_INCREMENT,
      `plan_id` int(11) NOT NULL,
      `company_name` varchar(255) NOT NULL,...

     

    it also seemed to be overkill to have two while loops running.. i am just thinking out loud on the best approach to this.

  15. echo the SQL, what's it look like?

     

    Blank value. It's not being passed from the form it appears, but that's what I can't get my finger on.

     

    I should clarify, it's being passed as 0 (but 0 does not exist as an id in the company table).. I believe it is 0 because i defined it as int as a test: $company_id = (int)$_POST['company_id'];

     

    No value is coming through at all from the form. I check all spelling etc. And I am guessing there is something I cannot see.

  16. i can't seem to see what my problem is.. i am "blind" to this bug as everything i am doing looks fine, so any advice please. i cannot get $company_id to insert a value to the table. NO errors are showing and all other data is being inserted as expected.

     

    Submission page (all other data is going into the table as expected.)

    $sql = "SELECT id, company_name FROM $table_name ORDER BY company_name";
    $query = mysql_query($sql);
    while($row = mysql_fetch_array($query))
    {
      echo "<option value=\"".$row['id']."\">".$row['company_name']."</option>\n";
    }
    

     

    Processing page snippet:

     

    $company_id = $_POST['company_id']; // select box company_id value

     

    $sql = "INSERT INTO $table_name (id, company_id, plan_name, plan_type, plan_details, plan_exclusions, plan_additional_info) VALUES ('','$company_id','$plan_name','','','','')";

     

×
×
  • 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.