CodeIgniter Forums
Fix an anchor issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Fix an anchor issue (/thread-73918.html)



Fix an anchor issue - Mekaboo - 06-24-2019

Hello all!

I uploaded code from git into my site but having an issue anchoring the controller to view. This is my anchor:
<?= anchor('chat/index','Chat');?>


This is the controller/function that Im trying to connect:

class Chat extends Controller {

    function Chat() {
        parent::Controller();
        $this->load->database('cultured_codeignite');
        $this->load->model('mod_chat');
        $this->load->library(array('session', 'authentication'));

    }

    function index() {
        //outsiders keep out
        $this->authentication->sentry();
        $ctr = 0;
        //get all chat messages from buddy
        $username = $this->session->userdata('username');
        $recipient = $this->session->userdata('recipient');
        $fetchchat = $this->mod_chat->fetchchat($username, $recipient);
        $chatreverse = '';
        //reverse results of chat, bottom message is latest
        foreach ($fetchchat as $chatorig) {
            $chatreverse[$ctr]['username'] = $chatorig->username;
            $chatreverse[$ctr]['message'] = $chatorig->message;
            $time = explode(' ', $chatorig->time);
            $reformat_time = date("g:i a", strtotime($time[1]));
            $chatreverse[$ctr]['time'] = $reformat_time;
            ++$ctr;
        }
        $data['chat'] = '';
        $data['buddy'] = $this->session->userdata('recipient');
        $buddyimage = $this->mod_chat->get_buddy_image($this->session->userdata('recipient'));
        $data['buddyimage'] = $buddyimage[0]->image;
        //throw messages to view
        if (is_array($chatreverse)) {
            $users['username'] = $username;
            $users['recipient'] = $recipient;
            $this->mod_chat->message_read($users);
            $data['chat'] = array_reverse($chatreverse);
        }
        $this->load->view('view_chat', $data);
    }


The viewer is view_chat that Im trying to connect to. How do I get this to work?

Heart Heart ,
Mekaboo


RE: Fix an anchor issue - php_rocs - 06-24-2019

@Mekaboo,

What errors are you seeing? Also, what does <?= anchor('chat/index','Chat');?> put out?


RE: Fix an anchor issue - InsiteFX - 06-25-2019

Your constructor is using the old style.

PHP Code:
// change this
function Chat()
{
 
   parent::Controller();
 
   
    $this
->load->database('cultured_codeignite');
 
   $this->load->model('mod_chat');
 
   $this->load->library(array('session''authentication'));
}

// to this
function __construct()
{
 
   parent::__construct);
 
   
    $this
->load->database('cultured_codeignite');
 
   $this->load->model('mod_chat');
 
   $this->load->library(array('session''authentication'));


Try that and see if it works.


RE: Fix an anchor issue - Mekaboo - 06-25-2019

(06-24-2019, 10:10 PM)php_rocs Wrote: @Mekaboo,

What errors are you seeing? Also, what does <?= anchor('chat/index','Chat');?> put out?

Hey! I get a 404 error saying that the page is not found. Basic error..the page isnt loading at all. Im trying to figure out if its because of the function.


RE: Fix an anchor issue - Mekaboo - 06-25-2019

(06-25-2019, 03:46 AM)InsiteFX Wrote: Your constructor is using the old style.

PHP Code:
// change this
function Chat()
{
 
   parent::Controller();
 
   
    $this
->load->database('cultured_codeignite');
 
   $this->load->model('mod_chat');
 
   $this->load->library(array('session''authentication'));
}

// to this
function __construct()
{
 
   parent::__construct);
 
   
    $this
->load->database('cultured_codeignite');
 
   $this->load->model('mod_chat');
 
   $this->load->library(array('session''authentication'));


Try that and see if it works.
Unfortunately it didnt help, Im still getting a 404 page dont exist code. Trying to figure out how to fix the function.


RE: Fix an anchor issue - php_rocs - 06-25-2019

@Mekaboo,

I need to see what anchor value is being displayed on the page. Is it something like... https://yoursite.com/index.php/chat/index

If so, then try removing the index and see if that works. Like this <?= anchor('chat','Chat');?>

Also, I noticed that you didn't load the url helper ( https://www.codeigniter.com/user_guide/helpers/url_helper.html )


RE: Fix an anchor issue - Wouter60 - 06-25-2019

Is your controller Chat.php ? With a capital C ?
If that's the case, what's the url that shows up in your browser after you clicked the "anchor"?


RE: Fix an anchor issue - Mekaboo - 06-26-2019

Thank ya all! I tried various ways to correct things from copy/paste from one controller to another as well as doing the same process for the models. Parts worked but not all. I looked up alternatives and found this one:

https://www.sourcecodester.com/php/6318/login-and-chat-application-ajaxphp-and-mysql.html

Had the "Undefined Variable error but this time I seem to fix on my own (WooHoo)..now its time to test.

Thank you all for the help,it means alot Smile