3

I saw the examples of how to do the odd even way, but I want the order to flow from one whole column to the next and not alternating left to right for each alphabetical item. Can this be done?

Here is the query

$db = JFactory::getDbo();
$db->setQuery('SELECT title, introtext 
               FROM #__content
               WHERE introtext LIKE "%Still Airing%"
               ORDER BY title ASC');
$row = $db->loadAssocList();

I tried dividing it by 2, but then how do you get each column to appear in html?

$half = count($row) / 2;

What I was thinking was I could use the variable to list that number of items in each column (or table tds) , but don't know the syntax or if I'm doing it right. Like list $half items and then the rest in the next column.

The introtext is just a filter, I only want the titles displayed.

4

Using array_chunk() with the row count cut in half then rounded-up will ensure that the second column always has equal or one less row than the first column.

HTML tables should only be used to display "tabular" data. Since you are building two non-tabular lists, it would be more appropriate to employ other tags (several different tags may be appropriate).

Demo (copy-paste my snippet into phptester.net to see the rich-text result)

// $resultset = $db->loadAssocList();  ...I am supplying my own sample data
$resultset = [
    ['title' => 'A', 'introtext' => 'A intro'],
    ['title' => 'B', 'introtext' => 'B intro'],
    ['title' => 'C', 'introtext' => 'C intro'],
    ['title' => 'D', 'introtext' => 'D intro'],
    ['title' => 'E', 'introtext' => 'E intro']
];

$halves = array_chunk($resultset, ceil(count($resultset) / 2));

foreach ($halves as $half) {
    echo '<div style="display: inline-block; vertical-align: top; border: solid 1px black; padding: 2px;">';
        foreach ($half as $row) {
            echo '<div style="border: 1px solid blue; padding: 2px;">';
                echo '<p style="background-color: green;">' , $row['title'] , '</p>';
                echo '<p style="background-color: yellow;">' , $row['introtext'] , '</p>';
            echo '</div>';
        }
    echo '</div>';
}

Output:

enter image description here


After a bit of back and forth in the comments, here is the direct approach to output only the title values as two columns in the desired arrangement:

$db = JFactory::getDbo();
$query = $db->getQuery(true)
            ->select("title")
            ->from("#__content")
            ->where("introtext LIKE '%Still Airing%'")
            ->order("title");
// echo $query->dump();  // uncomment if you want to see the rendered query
$db->setQuery($query);
$resultset = $db->loadColumn();

$halves = array_chunk($resultset, ceil(count($resultset) / 2));

foreach ($halves as $half) {
    echo '<div style="display: inline-block; vertical-align: top; border: solid 1px black; padding: 2px;">';
        echo '<div>' , implode('</div><div>', $half) , '</div>';
    echo '</div>';
}

Note, by using Joomla's query building methods, it is easier to diagnose problems by printing $query->dump().

  • Not working for me for some reason, nothing displaying. Will play with it. I tried running 3 queries - 1 to get $count (worked), next to limit results to $count, and then 3rd to limit results to $count offset by $count, then echo rowa & rowb in html - but i kept getting an error around the order by in the query. ....LIMIT ' . $count . ' OFFSET ' . $count . ' ORDER BY title ASC'); – Mythic Jun 30 at 22:10
  • That is a separate issue, of course. Please show me your query buliding code as a 3v4l.org link. I am on the train to work. Support time is limited. – mickmackusa Jun 30 at 22:12
  • If that WHERE clause is fed a variable, it is not employing secure practices. joomla.stackexchange.com/a/22712/12352 ...if it is static text, it is okay. – mickmackusa Jun 30 at 22:15
  • 3v4l.org/t0lRA – Mythic Jun 30 at 22:17
  • 1
    did you notice that I changed the query to only include one column and resultset building method? $db->loadColumn() – mickmackusa Jun 30 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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