views:

76

answers:

4

Hey, can someone help me make this an object please.

Obviously not all my code is here, but i'm sure you'll get the gist.

<?php
$product_name_1 = $_POST['product_name_1'];
$region_1 = $_POST['region_1'];
$start_date_1 = $_POST['start_date_1'];
$end_date_1 = $_POST['end_date_1'];
$sku_1 = $_POST['sku_1'];

$product_name_2 = $_POST['product_name_2'];
$region_2 = $_POST['region_2'];
$start_date_2 = $_POST['start_date_2'];
$end_date_2 = $_POST['end_date_2'];
$sku_2 = $_POST['sku_2'];

$product_name_3 = $_POST['product_name_3'];
$region_3 = $_POST['region_3'];
$start_date_3 = $_POST['start_date_3'];
$end_date_3 = $_POST['end_date_3'];
$sku_3 = $_POST['sku_3'];
?>



<form action="" method="post" accept-charset="utf-8">
<div id="product_information">
<table id="product_1">
    <tr>
        <th><label for="product_name">Product Name</label></th>
        <th><label for="region">Select A Region</label></th>
        <th class="date"><label for="start_date">Start Date</label></th>
        <th class="date"><label for="end_date">End Date</label></th>
        <th><label for="sku">SKU</label></th>
    </tr>
    <tr>
        <td><input type="text" name="product_name_1" value="" id="product_name_1"></td>
        <td><input type="radio" name="region_1" value="upper_north" id="upper_north_1"><label for="upper_north_">Upper North Island</label><br />
                <input type="radio" name="region_1" value="lower_north" id="lower_north_1"><label for="lower_north_">Lower North Island</label><br />
                <input type="radio" name="region_1" value="south_island" id="south_island_1"><label for="south_island">South Island</label>   </td>
        <td class="date"><input type="text" class="date" name="start_date_1" value="" id="start_date_1"></td>
        <td class="date"><input type="text" class="date" name="end_date_1" value="" id="end_date_1"></td>
        <td><input type="text" name="sku_1" value="" id="sku_1"></td>
    </tr>
</table>
<span class="product"></span>
<div class="add-product">&nbsp;</div>
</div>

<script type="text/javascript" charset="utf-8">

var i = 1;

$('.add-product').click(function(){
    i++;
    $('span.product').replaceWith('<table id="product_'+i+'">'
        +'<tr>'
            +'<th><label for="product_name">Product Name</label></th>'
            +'<th><label for="region">Select A Region</label></th>'
            +'<th class="date"><label for="start_date">Start Date</label></th>'
            +'<th class="date"><label for="end_date">End Date</label></th>'
            +'<th><label for="sku">SKU</label></th>'
        +'</tr>'
        +'<tr>'
            +'<td><input type="text" name="product_name'+i+'" value="" id="product_name'+i+'"></td>'
            +'<td><input type="radio" name="region'+i+'" value="upper_north" id="upper_north'+i+'"><label for="upper_north'+i+'">Upper North Island</label><br />'
                    +'<input type="radio" name="region'+i+'" value="lower_north" id="lower_north'+i+'"><label for="lower_north'+i+'">Lower North Island</label><br />'
                    +'<input type="radio" name="region'+i+'" value="south_island" id="south_island"><label for="south_island">South Island</label>   </td>'
            +'<td class="date"><input type="text" class="date" name="start_date'+i+'" value="" id="start_date'+i+'"></td>'
            +'<td class="date"><input type="text" class="date" name="end_date'+i+'" value="" id="end_date'+i+'"></td>'
            +'<td><input type="text" name="sku'+i+'" value="" id="sku'+i+'"></td>'
        +'</tr>'
    +'</table>'
    +''
    +'<span class="product"></span>');
});
</script>
+5  A: 

You can use this object to start. After that define methods for saving and retrieving it. And maybe proper constructor, for creating different products from the post params.

class Product {

    private $Name;
    private $Region;
    private $StartDate;
    private $EndDate;
    private $Sku;

    public function setName( $value )    
    {
            //make some validation or manipulation on data here, if needed
        $this->Name = $value;    
    }

    public function getName()    
    {                
        return $this->Name;    
    }    

    public function setRegion( $value )    
    {
            //make some validation or manipulation on data here, if needed
        $this->Region = $value;    
    }

    public function getRegion()    
    {                
        return $this->Region;    
    }    
    public function setStartDate( $value )    
    {
            //make some validation or manipulation on data here, if needed
        $this->StartDate = $value;    
    }

    public function getStartDate()    
    {                
        return $this->StartDate;    
    }    

    public function setEndDate( $value )    
    {
            //make some validation or manipulation on data here, if needed
        $this->EndDate = $value;    
    }

    public function getEndDate()    
    {                
        return $this->EndDate;    
    }    

    public function setSku( $value )    
    {
            //make some validation or manipulation on data here, if needed
        $this->Sku= $value;    
    }

    public function getSku()    
    {                
        return $this->Sku;    
    }    
}

Hope that helps!

anthares
Ok, cool, so thats the object model for the product. Thankyou, that does help on that side of things.
mdskinner
@mdskinner Good luck! :)
anthares
+1  A: 

You don't necessarily need to use an object - when dealing with forms, associative arrays are easier (since that is what HTML passes to PHP). In your form it would be more beneficial to use field names like this:

  • product[1][name]
  • product[1][region]
  • product[1][start_date]
  • product[1][end_date]
  • product[1][sku]
  • product[2][name] ...etc

When receiving the data in PHP, it will be in $_POST['product'], which itself will be an array of each product. Your new PHP code will depend on what you want to do with the data, but you might loop though it like this:

<?php
foreach( $_POST['product'] as $prod )
{
   echo $prod['name']; // outputs each product name in turn
}

If you require an object, you could use $prod_obj = (object) $prod then $prod_obj->name and so on.

DisgruntledGoat
Ok, thats cool.. thanks, but i want to be able to create an infinite amount of "products" wouldn't this method still require repeating the code endlessly?I don't want to have to limit how many products a user can add.
mdskinner
@mdskinner: No, there isn't a limit. I meant use the names with `[1]` as your initial fields, but when the user adds another product, the javascript would write the same code but with `[2]` or `[3]` etc.
DisgruntledGoat
By the way, there must be an easier way to do the javascript part. I think you should be able to clone the code that's already there and replace `[1]` with `[2]` the form names. At the moment if you change any HTML you have to do it twice.
DisgruntledGoat
A: 

So now onto instantiating and using the new object.

Could i make a construct like this?

<? 
class Product {

  private $Name;
  private $Region;
  private $StartDate;
  private $EndDate;
  private $Sku;
  public $i;

    function __construct($Name="product_name_$i" $Region="region_$i" $StartDate="start_date_$i" $EndDate="end_date_$i" $Sku="sku_$i")
    {
        $i++
    }

}
?>

And then how would i actually instantiate the object and use it in my code?.. can i do this with my JavaScript?

mdskinner
A: 

anthares has given you the object; DisgruntledGoat has shown you how to use the foreach to run through the input. :) The constructor you've got needs a little work, though. If you're going to pass parameters individually, you'll need to separate each with a comma; your default values are things that are used if values aren't passed. I'd do something like this (which is what I do in my OO PHP when filling my objects with a row from the database).

/**
 * Constructor for the product.
 *
 * @param string=>string[] An associative array used to create the object.
 */
public function __construct($aData = null) {
    if (!is_null($aData)) {
        $this->setName     ($aData["name"]);
        $this->setRegion   ($aData["region"]);
        $this->setStartDate($aData["start_date"]);
        $this->setEndDate  ($aData["end_date"]);
        $this->setSku      ($aData["sku"]);
    }
}

In the HTML code, you'd define your fields like...

<input type="text" name="product[1][name]" />
<input type="text" name="product[1][sku]" />
...

Then, in PHP, you'd do something like

$aProducts = array();
foreach ($_POST["product"] as $aProduct) {
    $aProducts[] = new Product($aProduct);
    // NOTE - here you could also make an empty object and fill it.
    $oProduct = new Product();
    $oProduct->setName($aProduct["name"]);
    // etc. - then add it to the array when you're done.
}

// Display the name of product 2 (if it exists).
if (2 <= count($aProducts)) {
    echo $aProducts[1]->getName();
}

Finally, I'm not sure what the "$i" is giving you. I usually just create fields like...

<input type="text" name="name[]" />
<input type="text" name="sku[]" />
<!-- This next one is if I need a database ID once it's posted back. -->
<input type="hidden" name="id[]" value="something"/>
...
<input type="text" name="name[]" />
<input type="text" name="sku[]" />
...etc...

Then, on the server, I have a bunch of arrays, but their keys all match. So, I can do something like...

$aProducts = array();
foreach($_POST["id"] as $iKey => $aValue) {
    $oProduct = new Product();
    $oProduct->setName($_POST["name"][$iKey]);
    $oProduct->setSku ($_POST["sku" ][$iKey]);
    $aProducts[] = $oProduct;
}

6 of 1, 1/2 dozen of the other. My constructors usually take PDORow objects for their parameters, so I do it this way on page post, when I'm working with associative arrays.

Daniel