Drupal 7 Printing two records with same name - database

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; ?>
}

Related

Custom Fields - Repeater dropdown - list only unique & not blank values

Here's the thing that I'm using the ACF repeater subfield (firma) to store some data. Usually the user is typing here his parent company name like: google, alibaba, sodexo etc. So it might happen that in multiple posts, the value of this field will be the same. At the moment I have following code:
$args = array(
'post_type' => 'opencourses',
'meta_key' => 'terminy_warsztatow'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
echo '<select type="text" class="form-control" name="filtr_lokalizacja">';
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('terminy_warsztatow')):
while (have_rows('terminy_warsztatow')) : the_row();
// display your sub fields
$filtr_var = get_sub_field('firma');
echo '<option value="'. $filtr_var .'">';
echo $filtr_var;
echo '</option>';
endwhile;
else :
// no rows found
endif;
endwhile;
echo '</select>';
endif;
And it works - meaning: it shows all typed values. But instead of showing only UNIQUE values it generates a list similar to this:
Google
Alibaba
Google
Sodexo
Sodexo
Tesla
Tesla
Sodexo
How to avoid showing same values and hide empty as well? I know that there is php function array_unique but I was unable to implement that. I've done sth like:
$filtr_var = get_sub_field('firma');
$result = array_unique($filtr_var);
echo $result;
but then it shows no values at all.
I assume that "firma" is simple text input type in repeater. if so then arrya_unique function won't work for string output.
you need to save each value in array and then use in_array function to make it unique.
see below code.
$args = array(
'post_type' => 'opencourses',
'meta_key' => 'terminy_warsztatow'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
echo '<select type="text" class="form-control" name="filtr_lokalizacja">';
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('terminy_warsztatow')):
// $PreValue = array();
while (have_rows('terminy_warsztatow')) : the_row();
// display your sub fields
$filtr_var = get_sub_field('firma');
// compare current value in saved array
if( !in_array( $filtr_var, $PreValue ) )
{
echo '<option value="'. $filtr_var .'">';
echo $filtr_var;
echo '</option>';
}
// save value in array
$PreValue[] = $filtr_var;
endwhile;
else :
// no rows found
endif;
endwhile;
echo '</select>';
endif;
Hope this will help you!
Enjoy

Categories tags in module (mod_articles_categories)

I need to display tags of every sub category in categories list module (mod_articles_categories).
For articles in similar module (mod_articles_news) I always do something like this:
<?php $itemtags = (new JHelperTags)->getItemTags('com_content.article', $item->id);
$taglayout = new JLayoutFile('joomla.content.tags');
foreach ($itemtags as $tag) { echo $tag->title; } ?>
But code like this doesn't work fork categories even I change 'com_content.article' to 'com_content.category'
Maybe someone knows what I doin' wrong?
Thanks in advance
Ok, finally I got the solution:
<?php foreach ($list as $item) :
$itemtags = (new JHelperTags)->getItemTags('com_content.category', $item->id);
$taglayout = new JLayoutFile('joomla.content.tags');?>
<?php if (!empty($itemtags)) { foreach ($itemtags as $tag) { echo $tag->title.' '; }; } ?>
<div><?php echo $item->title; ?></div>
<?php endforeach; ?>

how can i combine href with id number

Trying to get some data from db but I can't use href with id number in code.
I tried everything but coldn't make it.
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('title')));
$query->select(array($db->quoteName('id')));
$query->select(array($db->quoteName('start_date')));
$query->select(array($db->quoteName('end_date')));
$query->from($db->quoteName('#__rbid_auctions'));
$db->setQuery($query);
$results = $db->loadObjectList();
// display the results
foreach ( $results as $result) {
echo <<<HTML
$result->title .
HTML;
echo "<p>" . $result->start_date . "</p>";
echo "<p>" . $result->end_date . "</p>";
}
?>
I will be appreciated if someone help me.
Thanks in advance
Demonstration of issue then my suggested solution: (Online Demo)
$result = new stdClass();
$result->id = 333;
$result->title = 'title text';
echo <<<HTML
$result->title .
HTML;
Output:
Notice: Undefined variable: id in /in/s1YZG on line 7
title text .
Notice that $id isn't a declared variable. If it was, it would be rendered but all characters between the curly braces are treated literally because you are trying to echo within an echo.
Without heredoc syntax (heredoc can be funny about tabbing depending on php version):
echo "{$result->title} . "; // curly braces may help IDEs with highlighting
New Output:
title text .
As for your query building syntax...
You can save some typing and chain the method calls onto getQuery().
None of the quoteName() calls are necessary for stability/security, but if you insist on toeing Joomla's preferred practices, you can call quoteName() on the array in select().
Suggested Code:
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName(array('title', 'id', 'start_date', 'end_date')))
->from($db->quoteName('#__rbid_auctions'));
$db->setQuery($query);
if (!$results = $db->loadObjectList()) {
echo "No results";
} else {
foreach ($results as $row) {
echo "{$row->title} . ";
echo "<p>{$row->start_date}</p>";
echo "<p>{$row->end_date}</p>";
}
}
Here is another post where loadObjectList() is called after a SELECT query which includes query error checking: https://joomla.stackexchange.com/a/22963/12352
When you have Joomla questions, please post them on Joomla Stack Exchange.
I would try it like this:
also you are trying to echo $id which isnt assigned. should be $results->id
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('title', 'id', 'start_date', 'end_date' ));
$query->from($db->quoteName('#__rbid_auctions'));
$db->setQuery($query);
$results = $db->loadObjectList();
// display the results
foreach ( $results as $key => $result) {
echo ' <a href="index.php?option=com_rbids&task=viewbids&id='.$result->id .'>'.$result->title .'</a>';
echo "<p>" . $result->start_date . "</p>";
echo "<p>" . $result->end_date . "</p>";
}
?>

Kohana parameterised execution of query within view

I'm just trying to get my head around Kohana, so if I'm going about this the wrong way, let me know!
I want to group results in my output. The way I did this in vanilla-PHP / PDO was to prepare a query, and then execute it within the output results. I can't get there in Kohana (3.2.2), though.
In this case, on my 'terms' page, I want to group 'terms' by 'type', with each group of terms separated by a 'type' header.
My 'terms' controller is (simplified):
class Controller_Terms extends Controller {
public function action_index()
{
$view = View::factory('terms');
// types of term - for grouping terms together
$sql = 'SELECT DISTINCT Type
FROM Term';
$view->types = DB::query(Database::SELECT, $sql)->as_object()->execute();
// list of terms - executed separately for each type
$sql = 'SELECT TermId, Term
FROM Term
WHERE Type = :type';
$view->terms = DB::query(Database::SELECT, $sql)->as_object();
$this->response->body($view);
}
}
And the 'terms' view includes (simplified):
<? foreach ($types as $type): ?>
<h2><?= $type->Type ?></h2>
<? $params = array(':type' => $type->Type);
$terms->parameters($params)->execute();
foreach ($terms as $term): ?>
<?= $term->Term ?>
<? endforeach // term ?>
<? endforeach // type ?>
I get the 'type' headers ok, but I don't get any terms within each type.
Any suggestions appreciated!
execute() returns a special object (Database_Result), so you need something like this:
$items = $terms->parameters($params)->execute();
foreach ($items as $term): ?>
...

Group records in CakePHP based on first letter?

I'm wanting to fetch and group records from a database table by the first letter. So for example:
A
Alfred
B
Brian
C
Christopher
...
Ideally, I'd like my data structure to come out as follows:
Array
(
[A] => Array
(
[0] => Array
(
[Person] => Array
(
[id] => 12
[name] => Alfred
)
)
)
)
I could do this with a raw MySQL query, but not sure if CakePHP supports this out of the box. I'm just literally wanting records grouped so I can iterate over them and create an unordered list for each letter:
<h2>A</h2>
<ul>
<li>Alfred</li>
</ul>
<h2>B</h2>
<ul>
<li>Brian</li>
</ul>
<h2>C</h2>
<ul>
<li>Christopher</li>
</ul>
...
What i would do is: order them by name and then sort them in a loop:
$people = $this->Person->find('all', array(
'order' => 'Person.name ASC'
));
And then you could do:
$letter = '';
foreach($people as $person) {
$firstLetter = strtolower(substr($person['Person']['name'], 0, 1));
if ($firstLetter !== $letter) {
$letter = $firstLetter;
}
$_people[$letter][] = $person;
}
$people = $_people;
Or just do it directly in the view so you don't need to loop it twice:
<ul>
<?php $letter = 'first'; ?>
<?php foreach ($people as $person) : ?>
<?php $firstLetter = strtolower(substr($person['Person']['name'], 0, 1)); ?>
<?php if ($firstLetter !== $letter) : ?>
<?php if ($letter !== 'first') : ?>
</ul></li>
<?php endif; ?>
<?php $letter = $firstLetter; ?>
<li><h2><?php echo $firstLetter; ?></h2><ul>
<?php endif; ?>
<li><?php echo $person['Person']['name']; ?></li>
<?php endforeach; ?>
</ul></li></ul>
I wouldn't know how to get this structure just by using a find call (except for making 26 calls or creating a db table containing all letters) and I don't see why that would be neccesairy.
Hope that helps! (ps code isn't tested... but you get the point. should there be any typos)
Try this:
$peoplebyletter = array();
foreach($people as $person){
$peoplebyletter[strtolower(substr($person['Person']['name'], 0, 1))][] = $person;
}
debug($peoplebyletter);
Not tested. Let me know if it works.

Resources