HomeOffice
HomeOffice

Reputation: 139

codeigniter url link returns same article text regardless of article id

I've tried to crab from another question asked before at link going through tracking script pulls correct article id, incorrect article text , but, alas, no love. I'm no stranger to the CI 3x docs, but I haven't found anything to help me with this and I suspect there may be more than one issue at play here. I've made sure that the appropriate libraries, helpers, and models are all previously included through the autoloader. The issue is the same as in the previously-mentioned article; the article id URLs are all perfect as far as referencing the correct id. However, the same text is being returned no matter which article link is clicked upon.

I'm accessing the article view page (and passing in the id variable) from my controller like so;

Articlelist.php

public function view($id)
{
    $this->load->view('view_article');
}

In the referenced view, there's a query that's supposed to return a single article text by its' id;

$query = $this->db->query("SELECT * FROM articles ORDER BY article_id DESC LIMIT 1");

and then I'm able to use $query->result() in a foreach loop to access my content elements. I'm returning these results through two different pages and the reader is able to click on a link to navigate with;

echo '<a href="articlelist/view/id='.intval($row->article_id).'">  
<font size="5" color="#0000CC">Click Here To View Article</font></a><hr>';

I actually tried to append ?id='.intval($_GET['id']) to the view page (view_article.php) previously, but was left with a messy error trail. I even tried as last-ditch rewrites;

$query_str="SELECT * FROM articles ORDER BY article_id DESC LIMIT 1";
$query=$this->db->query($query_str);
 // fetch one row data
 $record=$query->row();
 echo $record->title;
 echo $record->name;
 echo $record->date;
 echo $record->category;
 echo $record->article_text;

===================================================================

$query = $this->db->get('articles, 0, 1);
foreach($query->result() as $row) {
  echo '<font face="arial black" color="#0000CC" size="5">Title:&nbsp;' .$row->title. '</font>' .'<br />';
  echo '<font face="arial black" color="#0000CC" size="5">Publisher:&nbsp;' .$row->name. '</font>' .'<br />'; 
  echo '<font face="arial black" color="#0000CC" size="5">Category:&nbsp;' .$row->category. '</font>' .'<br />';
  echo '<font face="arial black" color="#0000CC" size="5">Published:&nbsp;' .$row->date. '</font>' .'<br />';
  echo '<font face="arial black" color="#0000CC" size="5">Content:<br />' .$row->article_text. '</font>' .'<br />';
}

but, I'm still left with the issue of the incorrect article text being returned under the correct article id. I even tried to ascertain that it wasn't a matter of routing by setting a baseline catch-all;

$route['(:num)/(:any)']='articlelist/view/id/$1';

I'm almost where I want to be as article content is being returned when items on the articlelist page are clicked upon, but what am I missing in order to connect to the correct article id? If any other information is needed for an assessment, please request it, thanks.

UPDATE

I went back and connected a model process to the view page as it was missing;

public function view($id)
{
    $data['results'] = $this->article_model->getArticle($id);
    $this->load->view('view_article', $data);
}

but it added nothing by the way of a resolution and was redacted shortly thereafter.

UPDATE 2

Thanks to @JefreN & @cr05s19xx for their answers - let me add how the model and the relevant controller code is;

Article_model.php

 function getArticle($id)
{
    $this->db->select('*')->order_by('article_id', 'desc')->limit(1);
    $query = $this->db->query("SELECT * FROM articles ORDER BY article_id DESC LIMIT 1");
    return $query->result();

}

Articlelist.php

public function view($id)
{
    $data['results'] = $this->article_model->getArticle($id);
    $this->load->view('view_article', $data);
}

Even with the updates applied, I'm still only getting the content from the URL

http://localhost/articlelist/view/id=33

no matter which article link I click on.

Upvotes: 0

Views: 326

Answers (3)

HomeOffice
HomeOffice

Reputation: 139

For some reason, it started poking at me that in the page where the pagination results were being returned, there was no on-page query. THAT job, being taken care of in the article_model.php file. Deciding to do a little creative re-arrangement;

function getArticle($id)
{
    $query = $this->db->select('*')->get_where('articles', array('article_id' => $id));
    if($query->num_rows() > 0)
    {
        foreach($query->result() as $row)
        {
           $data[] = $row;
        }
           return $data;    
    }

}

After that, it was only a hop, skip, and a jump to the realization of the elimination of the other link-point on-page query, so I could switch to working code in the controller to eliminate my issue;

public function view($id)
{
    $data = array();
    $data['results'] = $this->article_model->getArticle($id);
    $this->load->view('view_article', $data);
}

aaaaaaaaannnnnnnnndd...having solved this problem led to the pagination being all borked.

For anyone in the future having this problem; CodeIgniter documentation stipulates that pages (being represented via the URI segments) are of the format;

http://domain(.*)/controller/method/arguments 

On the 1st page of returned pagination results, the URI given when hovering over each result;

http://localhost  /articlelist  /view     /33 
       (domain)   (controller)  (method)  (arguments)

However, from the second to the last page of pagination results, there was an EXTRA segment;

http://localhost  /articlelist  /index    /view     /33 
       (domain)   (controller)  (?????)   (method)  (arguments)

because of the $config['base_url'] = site_url('articlelist/index'); - needed to keep the pagination results corralled in place for display.

This was corrected by correcting the borky part of the view page listing the db query results (articles_list.php);

echo '<h3><a title="Click Here For Content" href="'?>
<?php echo base_url().'articlelist/view/'.$row->article_id.'">
'. $row->title. '</a></h3><hr>';

(probably should bother to point out that the last opening php tag is closed further down in code not quoted on-page for those seeing this as a solution candidate)

Thanks to all who threw their $0.02 out there for consideration - helped me find my answer. Tested over 10 selections from the two link-point areas, and pulled the CORRECT article content to match the article id URL in the browser.

Upvotes: 0

josevoid
josevoid

Reputation: 677

As I can see with your URL given,

Even with the updates applied, I'm still only getting the content from the URL

http://localhost/articlelist/view/id=33 no matter which article link I click on.

You should only pass the 33 as id directly next to the view method. See below:

http://localhost/articlelist/view/33

This then calls your ariclelist->view method.

public function view($id) { ... }

Model query can also be written in this option using the codeigniter prepared methods.

public function get_article_info($id) 
{
    $this->db->select()->from('articles')->where('article_id', $id);
    $query = $this->db->get();

    return $query->result_array(); 
}

See more on Generating Query Results - CodeIgniter .

Upvotes: 2

cross19xx
cross19xx

Reputation: 3487

The correct query is SELECT * FROM articles WHERE article_id = $id

In Codeigniter a model would have been written like this

<?php
defined ('BASEPATH') or exit ('No direct script access allowed');

class Example_model extends CI_Model {

    public function get_article_info($id) {
        $query = $this->db->query("SELECT * FROM `articles` WHERE `article_id` = '$id'");
    // Rest of the code comes here
    }

Upvotes: 1

Related Questions