0

Trying to create a function to show a news and echo it

But facting issue when

a. Using public - its showing 0 - syntax error, unexpected token "public", expecting end of file. Shouldnt public be used in it. If removing public then its working fine

b. I believe there is better way to rewrite code especially echo ShowNewsUpdates();

If can someone guide.

 <?php 
    // No direct access
    defined('_JEXEC') or die; 
    use \Joomla\CMS\Factory;
    ?>
    
    <?php
ini_set('arg_separator.output','&amp;');
$menutype   = $params->get( 'menutype');
$roll   = $params->get( 'roll');
$type = 0;
    public function ShowNewsUpdates(){
        $db = Factory::getDBO();
        $query = "SELECT * FROM `#__easyblog_post` WHERE published=1 order by id desc limit 0,1 ";
        $db->setQuery($query);
        
        $rows = $db->loadObject();  
        $list=""; 
        
    
    if($rows){
            $list.='<p><a href="index.php?option=com_easyblog&view=entry&id='.$rows->id.'&Itemid=1"><span>'.$rows->title.'</span></a></p>       
                <p>'.date("l, F d, Y",strtotime($rows->created)).'</p>      
                    <p>'.$rows->intro.'</p>
                    <p><i>Know More</i></p>';
            }else{
                $list.='<p>No News Added!!</p>';
            }
            echo $list;
        }
        
    echo ShowNewsUpdates(); 
    ?>  

1 Answer 1

0

You are writing procedural code but the 'public' statement is part of the object orientated approach to coding so it needs to be inside a Class.

For example you would normally use it like this

<?php
...
Class Someclass {

    public function yourfunction {
    ...
    }
}

That is why your code works without the public statement, it is only recognised inside of a Class statement.

If your code is to be used inside an extension for Joomla then yes there are better ways to write your code but if it is for the purpose of accessing data from outside of Joomla in an ad-hoc or simple way then whatever works for you is fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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