Display data from a data base

Hi,

I am creating my custom module, i want to display some data select from two data tables.
I find a solution, it works with one table… but on this page, i had to select from two table.

The solution i found :

$entries = AscentStorage::load_bash();
// Tell the user if there is nothing to display.
if (empty($entries)) {
$form['no_values'] = array(
'#value' => t('No entries exist in the table dbtng_example table.'),
);
return $form;
}

$keyed_entries = array();
foreach ($entries as $entry) {
$options[$entry->pid] = t('@pid: @file_name @author', array(
'@pid' => $entry->pid,
'@file_name' => $entry->file_name,
'@author' => $entry->author,
));
$keyed_entries[$entry->pid] = $entry;
}

// Grab the pid.
$pid = $form_state->getValue('pid');
// Use the pid to set the default entry for updating.
$default_entry = !empty($pid) ? $keyed_entries[$pid] : $entries[0];

// Save the entries into the $form_state. We do this so the AJAX callback
// doesn't need to repeat the query.
$form_state->setValue('entries', $keyed_entries);

$form['pid'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('Choose entry to update'),
'#default_value' => $default_entry->pid,
'#ajax' => array(
'wrapper' => 'updateform',
'callback' => array($this, 'updateCallback'),
),
);

$form['file_name'] = array(
'#type' => 'textfield',
'#title' => t('Updated file_name'),
'#size' => 15,
'#default_value' => $default_entry->file_name,
);

$form['author'] = array(
'#type' => 'textfield',
'#title' => t('Updated author'),
'#size' => 15,
'#default_value' => $default_entry->author,
);

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
return $form;
}

/**
* AJAX callback handler for the pid select.
*
* When the pid changes, populates the defaults from the database in the form.
*/
public function updateCallback(array $form, FormStateInterface $form_state) {
// Gather the DB results from $form_state.
$entries = $form_state->getValue('entries');
// Use the specific entry for this $form_state.
$entry = $entries[$form_state->getValue('pid')];
// Setting the #value of items is the only way I was able to figure out
// to get replaced defaults on these items. #default_value will not do it
// and shouldn't.
foreach (array('file_name','author') as $item) {
$form[$item]['#value'] = $entry->$item;
}
return $form;
}

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/4/feed