2

I have created a table in my database as part of my custom module.

I would like to add user information into a text field from the custom table based on parameters in the address bar.

So far I have created code that will pull the correct information from the database, although this code is placed in the index controller (just to see if it would work). I am now thinking on a more realistic/useable level how to utilise this code.

I have considered putting the code in a block or a model (i'm assuming a block is the thing to use) then calling that from the template file, but I cant work out where to put the code and how to call it.

I am guessing there will need to be more data in my layout file, but I don't know what it should be?

The code to get the data

    $params = $this->getRequest()->getParams();
    $prefs = Mage::getModel('prefcentre/prefcentre');
    $prefs->load($params['id']);
    $data = $prefs->getData();
    return $data

The layout file

<user_preferences>
    <block type="core/template" name="pref_options" output="toHtml" template="ps/prefcentre/preferences.phtml" />
</user_preferences>

The template

<label>First Name<span></span></label>
<input type="text" name="FirstName" id="FirstName" value="<?php //datahere ?>" class="text">
<label>Last Name<span></span></label>
<input type="text" name="LastName" id="LastName" value="<?php //datahere ?>" class="text">

I have tried to work out a solution myself by looking at customer/form/edit.phtml and customer.xml customer_account_edit node but i've not been able to work it out.

When looking at edit.phtml I could only find

<div class="input-box">
    <input type="text" name="email" id="email" value="<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text required-entry validate-email" />
</div>

Where would is the first and last name boxes?

Summary of questions

  1. how do I pass data from the database into an textbox

  2. why is first name and last name not on the edit.phtml template

1
  • Have you created a model to represent the entity in the database (db table) ? or is it just a table ? Commented Aug 27, 2014 at 9:29

1 Answer 1

2

To answer your second question first :)

The first and last name information is added via a widget:

app/code/core/Mage/Customer/Block/Widget/Name.php
app/design/frontend/base/default/template/customer/widget/name.phtml

In the registration process these are added via:

echo $this->getLayout()->createBlock('customer/widget_name')
         ->setObject($this->getFormData())
         ->setForceUseCustomerAttributes(true)->toHtml()

I guess this was done as the name can be used in many locations, registration, address and checkout. By doing this via a widget you only need once place that adds the form fields and will cut down on duplication of code.

Now onto your first question. To pass data from the database into a textbox I would suggest that you add a new function into your block for this example getPrefcentreData.

What this function would do would take your snippet of code and load the information that you need from the database.

Then in your template you can simply call something like:

<input type="text" name="your_field" id="your_field" value="<?php $this->getPrefcentreData() ?>" class="text" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.