Does anyone know how to target two specific records with the same name seperately?
ie. I'm trying to print two fields from the database with the same name… of course it gives me the same result.
Slightly abbreviated code:
$query = db_select('node', 'n');
$query->join('users', 'u', 'u.uid = n.uid');
$query->join('taxonomy_term_data', 'td', 'td.tid = ti.tid');
$query
->fields('u', array('name'))
->fields('td', array('name'))
foreach ($result as $record) {
User Name: <?php echo $record->name; ?>
Tag Name : <?php echo $record->name; ?>
}
I can't find the answer but guessing something like this
<?php echo $record->td['name']; ?>
But no luck.
You can use the addField method to add aliases to your fields. Your query can be rewrited like this :
$query = db_select('node', 'n');
$query->join('users', 'u', 'u.uid = n.uid');
$query->join('taxonomy_term_data', 'td', 'td.tid = ti.tid');
$query
->addField('u', 'name', 'username')
->addField('td', 'name', 'tagname')
foreach ($result as $record) {
User Name: <?php echo $record->username; ?>
Tag Name : <?php echo $record->tagname; ?>
}