I am attempting to modify code that was created by Component Creator. The modification I am trying to make is to get the select list to save the partno instead of the index value.
There are two important notes: 1. Component Creator assumes all tables are in the same Joomla databse so it created the code accordingly. 2. I have created a custom field type that connects to the other database and selects the data from the table.
It all appears to work fine and the form saves the data to the table on submit. The drop down list displays my part numbers fine and allows me to select the one I want. The problem is that when I view the data in phpMyAdmin the table field partno has an integer which appears to be the index of the array behind the list (not the record ID in the table).
I can see in the ./site/views/[formname]/tmpl/default.php where the field is being rendered. I am not sure how to modify this to get the actual partno instead of the index to be saved.
<?php echo $this->form->renderField('partno'); ?>
<?php foreach((array)$this->item->partno as $value): ?>
<?php var_dump($value); ?>
<?php if(!is_array($value)): ?>
<input type="hidden" class="partno" name="jform[partnohidden][<?php echo $value; ?>]" value="<?php echo $value; ?>" />';
<?php endif; ?>
<?php endforeach; ?>
Any help would be greatly appreciated!
Additional info:
generated HTML for list
<div class="control-group">
<div class="control-label">
<label id="jform_partno-lbl" for="jform_partno">
Partno</label>
</div>
<div class="controls">
<select id="jform_partno" name="jform[partno]" style="display: none;">
<option value="0"> for [1/2 FPT SCH</option>
<option value="1"> for [3/4 FPT SCH</option>
<option value="2"> -> [GALLOUP3140777 3/</option>
<option value="3"> -> [GALLOUP3206216 1/</option>
<option value="4"> (B) DYN-AVI DA1246023-1</option>
<option value="5"> and (C) DYN-AVI DA12460</option>
<option value="6"> 1. NO REDUCTION IN TUBING D</option>
<option value="7"> 2. NO BURRS ALLOWED ON THE </option>
<option value="8"> 3. HOLES MAY BE DRILLED PRI</option>
<option value="9"> 4. HOLES ARE 90 Degrees +/-</option>
<option value="10"> 5. HELIX PITCH IS 163 +/- 2</option>
</select>
<div class="chzn-container chzn-container-single" style="width: 220px;" title="" id="jform_partno_chzn">
<a class="chzn-single">
<span> for [1/2 FPT SCH</span>
<div><b></b></div>
</a>
<div class="chzn-drop">
<div class="chzn-search">
<input autocomplete="off" type="text">
</div>
<ul class="chzn-results"></ul>
</div>
</div>
</div>
</div>
Here are two additional files that have been requested:
./site/models/forms/taskform.xml
<form>
<fieldset>
<field name="id" type="text" readonly="true" class="readonly" default="0" description="JGLOBAL_FIELD_ID_DESC"/>
<field name="task" required="true" message="Please enter a task." label="COM_TODO_FORM_LBL_TASK_TASK" description="COM_TODO_FORM_DESC_TASK_TASK" hint="COM_TODO_FORM_LBL_TASK_TASK"/>
<field name="description" cols="80" rows="6" filter="safehtml" type="textarea" label="COM_TODO_FORM_LBL_TASK_DESCRIPTION" description="COM_TODO_FORM_DESC_TASK_DESCRIPTION" hint="COM_TODO_FORM_LBL_TASK_DESCRIPTION"/>
<field name="due_date" class="inputbox" format="%d-%m-%Y" timeformat="12" filter="server_utc" minyear="-1" showtime="false" singleheader="true" todaybutton="false" weeknumbers="false" filltable="false" type="calendar" label="COM_TODO_FORM_LBL_TASK_DUE_DATE" description="COM_TODO_FORM_DESC_TASK_DUE_DATE" hint="COM_TODO_FORM_LBL_TASK_DUE_DATE"/>
<field name="task_status" type="foreignkey" label="COM_TODO_FORM_LBL_TASK_TASK_STATUS" description="COM_TODO_FORM_DESC_TASK_TASK_STATUS" class="inputbox" input_type="list" table="#__todo_status" key_field="id" value_field="task_status" required="true" default=""/>
<field name="partno" key_field="partno" value_field="partno" type="e2part" label="COM_TODO_FORM_LBL_TASK_PARTNO" description="COM_TODO_FORM_DESC_TASK_PARTNO" hint="COM_TODO_FORM_LBL_TASK_PARTNO"/>
<field name="assigned_to" query="SELECT * FROM #__contact_details WHERE published = 1 ORDER BY name" key_field="id" value_field="name" type="sql" label="COM_TODO_FORM_LBL_TASK_ASSIGNED_TO" description="COM_TODO_FORM_DESC_TASK_ASSIGNED_TO" hint="COM_TODO_FORM_LBL_TASK_ASSIGNED_TO"/>
<field name="created" type="timecreated" label="COM_TODO_FORM_LBL_TASK_CREATED" description="COM_TODO_FORM_DESC_TASK_CREATED" hidden="true" hint="COM_TODO_FORM_LBL_TASK_CREATED"/>
<field name="modified" type="timecreated" label="COM_TODO_FORM_LBL_TASK_MODIFIED" description="COM_TODO_FORM_DESC_TASK_MODIFIED" hidden="true" hint="COM_TODO_FORM_LBL_TASK_MODIFIED"/>
<field name="version_note" type="text" label="JGLOBAL_FIELD_VERSItoON_NOTE_LABEL" description="JGLOBAL_FIELD_VERSION_NOTE_DESC" class="inputbox" size="45" labelclass="control-label"/>
</fieldset>
<fieldset name="accesscontrol">
<field name="asset_id" type="hidden" filter="unset"/>
<field name="rules" type="rules" label="JFIELD_RULES_LABEL" translate_label="false" filter="rules" validate="rules" class="inputbox" component="com_todo" section="task"/>
</fieldset>
</form>
./site/models/fields/e2part.php
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
JFormHelper::loadFieldClass('list');
class JFormFieldE2Part extends JFormFieldList {
protected $type = 'e2part';
public function getOptions() {
$app = JFactory::getApplication();
$partno = $app->input->get('partno'); // partno is the dynamic value which is being used in the view
$servername = "localhost";
$username = "someuser";
$password="somepassword";
$dbname = "bmp";
$table = "estim";
$db= new mysqli($servername, $username, $password, $dbname);
if ($db->connect_error){
die("E2 Part (Estim) Connection failed: " . $conn_error);
}
$query = "SELECT a.PartNo FROM " . $table . " AS a ";
$query .= "WHERE a.PartNo > '' ";
$query .= "ORDER BY PartNo";
$result = mysqli_query($db, $query);
if($result){
//output parts
while($row = mysqli_fetch_assoc($result)) {
$parts[] = $row["PartNo"];
}
}else {
die("E2 Part (Estim) Query failed: ");
}
// Merge any additional options in the XML definition.
$options = array_merge(parent::getOptions(), $parts);
return $options;
}
}