[Solved] Can not use function return value in write context

This code snippet may be useful:

public static function getBlogsAll($idAuthor=NULL)
{
  $aResult = NULL;   

  # get Object
  $oResults = DB::connection('mysqlAuthor') 
          -> table('blogs') 
	  -> orderBy('xrl') 
          -> get( ['title', 'xrl', 'ert'] );

	$aResult = array();
	foreach($oResults as $row) {
	   $ERT = str_replace('.', 'm ', $row->ert) . 's';
          $aResult[$row->xrl] = $row->title 
                             .'<br /><sup>reading time ' 
                            . $ERT 
                            .'</sup>';
	}

  return $aResult;		  
}#endfunc

Thanks John, just wondering how I can use this and what it does?

Will I need to change the column names? And how do I attach this to my extent code. As mentioned, I already have the articles showing, I just need to limit what is showing and potentially, display them by the latest date (last_modified) column.

Cheers, Barry

ps. Isn’t there an easier way? :smile:

Hi Barry,

Have you managed to solve your problem? If so can you post your solution?

The function I supplied was just to show how to iterate an Object and to add results to an array. It was not just to copy and paste into your script.

If you haven’t managed to sort your problem then what I often do is make a verbose solution and then try to tidy the script and make it more readable (for future debugging).

Ha. You’d now if I found a solution I’d be telling everyone :smile:
Spent hours last night trying to fix this, open a new account over on the stackoverflow, here what I was upto last night if you want to get involved ha. link to thread

Thanks, Barry

PS. I’ve also been in touch with the support team today who run the admin on the CMS:

me: Each article page shows all the latest articles in the footer. The problem is, they all show. I need to limit the $children to about 6 and show them in ascending order based on the last_modified column from the navigation table.
them: Personally I prefer a navigation-based template but with additional db-writing functionality to enable custom sorting with whatever creterias.

me: I don’t have access directly to the SQL else things would be a lot easier.
them: You have – but it isn’t a good idea. Please never try to write to navigation-db – this could easily destroy consistence of navigation completely.

More information from support:

Navigation-Api by default sorts after parent_id, position, date.
If this is not enough to solve your problems you should sort an array with for example http://php.net/manual/en/function.uasort.php and related custom sort functions.
I am not sure, whether a Navigation Subtree is sortable by these functions but anyway you can manage a custom array filled before with page_id and date_modified.

Does this change anything for you John?

Without real or test data it is not easy.

Try creating the following table using http://localhost/phpmyadmin/index.php

-- phpMyAdmin SQL Dump
-- version 4.1.12
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Dec 17, 2014 at 06:17 AM
-- Server version: 5.6.16
-- PHP Version: 5.5.11

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Database: `test-123`
--

-- --------------------------------------------------------

--
-- Table structure for table `computerbarry`
--

CREATE TABLE IF NOT EXISTS `computerbarry` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `pageid` varchar(2) NOT NULL,
  `location_type` varchar(8) NOT NULL,
  `location_name` varchar(6) NOT NULL,
  `output` varchar(3) NOT NULL,
  `parent_page_id` varchar(20) NOT NULL,
  `language` varchar(2) NOT NULL,
  `precedence` varchar(1) NOT NULL,
  `file_name` varchar(22) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;

I’m going to make a start on the new table this week or after christmas, trying to prioritise job tasks :smile:

One question I have regarding the sorting issue, quote from admin shown below.

Its clear that after much testing, the subtree is not sortable by these functions.

So how do we create this custom array before with page_id and date_modified?
Is this similar to what I wa trying in my first post?

Thanks, Barry

For anybody who was following me on this @John_Betong @fretburner @Michael_Morris, finally found a solution :smile:

After much testing and talking, we needed to put the result into a separate array which we then sorted. I also created a new column, similar to last_modified though with a fixed date ext_column_4, this was mainly just for a visual so the articles don’t get republished when I regenerate the pages and site

I think I explained this correctly. Nice to hear any feedback or views based on this. For the record, I never could sort the xml data and sorted everything from the main navigation. $topPages are the top level pages from the database I think, the children are the sub pages attached to this.

From another file included on the page:

$topPages = $pageNav->getNavigation(1, 1);

Ok.

The sort function:

function rcmp($a, $b)
{
    if ($a['date'] == $b['date']) {
        return 0;
    }
    return ($a['date'] < $b['date']) ? 1 : -1;
}

The code:

$overAllArray2Sort = array();
    
    foreach ($topPages as $topPage){
    $children = $topPage->getChildren(null,true);
      foreach ($children as $child) {
          $overAllArray2Sort[] = array('pageid' => $child->page_id, 'date'=>strtotime($child->ext_column_4));
      }
    }
    usort($overAllArray2Sort, 'rcmp');
      
    foreach ($overAllArray2Sort as $item){
    $child = $pageNav->getPage($item['pageid']);

I still need to add the related articles somehow, might need another database as discussed, though the sorting is fixed :smiley:

Thanks, Barry

1 Like

I’ve managed to add the array_slice and counter for those interested.

array_slice solution

foreach (array_slice($overAllArray2Sort, 0, 10) as $item){
$articlePage = $pageNav->getPage($item['pageid']);

counter solution

$i = 0;
        foreach($overAllArray2Sort as $item) {
        if($i > 9) {
            break;
        }
        $articlePage = $pageNav->getPage($item['pageid']);
        $i++;

Both of these examples work so you can use either, not sure which is best though I here the counter is better, not sure why?

Barry

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.