Welcome Guest, Not a member yet? Register   Sign In
Page display problem
#1

[eluser]jude[/eluser]
I have configured a news website which works well on my own computer. The "readmore" button is enabled with the following code:

echo '<a href="' .base_url().'Db/article/'.$id.'">Read More</a>';

That triggers the 'article' function in the controller (Db) to display the relevant page on the basis of the given $id.

Only problem is this link does not work from my website. This triggers a 404 page. Also, the url at the top of the 404 page shows the relevant $id. Thus the $id has been received, but the page is not displaying.

Urgently want a solution to this!!

I have php5 on my computer. The hosting company has php4.3.9.

I have inserted the ci url helper before the code. Form helper too, just in case its needed.

Can anyone unlock this puzzle for me?
#2

[eluser]ontguy[/eluser]
Please paste the url and the relevant parts of the controller. 404 page apprears on both your computer and the host?
#3

[eluser]jude[/eluser]
#This is the article controller (class is called Db) code:

function article()
{
$this->load->helper('url');
$this->load->database();
$id = $this->uri->segment(3, 0);
$query ['info'] = "SELECT id, title, author, article, metdes, metkey, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts

WHERE id = $id";
$this->load->view('articleview.php', $query);
}


#To call this from the mainpageview, I put the following code:

echo '<a href="' .base_url().'Db/article/'.$id.'">Read More</a>';

#This is implemented only on my computer, NOT on the Net



#In contrast, this is a menu controller. There's no $id to pass. It gets implemented on the Net and on my computer.


function mobile()
{

$this->load->helper('url');
$this->load->database();
$query ['info']= "SELECT id, nid, title, author, intro, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts WHERE nid
BETWEEN 31 AND 40 ORDER BY id DESC LIMIT 30
";
$this->load->view('menupageview.php', $query);
}

#To call this from the mainpageview is easy:

<a href="&lt;?=base_url()?&gt;db/mobile">mobile</a>
#4

[eluser]ontguy[/eluser]
How does the views/articleview.php look?

Not sure if this related to the 404 maybe with how the query results are being passed to the view.

I think you could use this which would be the same as $this->uri->segment(3, 0).

function article ($id = 0) {
$temp = “SELECT id, title, author, article, metdes, metkey, DATE_FORMAT(date, ‘%M %d, %Y’) as sd FROM news_posts
WHERE id = $id”;

$query['info'] = $temp->result();
$this->load->view(’articleview.php’, $query);
}
#5

[eluser]jude[/eluser]
Is there any way of transferring a variable (say an $id) over the Net with CodeIgniter's default routing system (which uses segments)?

I dont want to switch to the query strings approach (that would make variable transer along with a link easy).

Under the segment approach I type: <a href="'.base_url().'Db/article/'.$id.'"

Is there any way to assure that the $id actually gets to the "article" function in the controller, and is processed.

Is sessions compulsorily needed?

_POST fails if I dont have a form to match.

Any viable methods? Please?
#6

[eluser]ontguy[/eluser]
I am sure how else it could be done outside a form.

If you are worried that someone could replace $id with something, you could just validate $id in your article function and set it to 0 if invalid.

Code:
function article ($id = 0) {
if (!is_numeric($id))
$id = 0;
:
:
}

This method could be used for getting the parameter in a query; the array is escaped automatically; http://ellislab.com/codeigniter/user-gui...eries.html

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick'));

The secondary benefit of using binds is that the values are automatically escaped, producing safer queries. You don't have to remember to manually escape data; the engine does it automatically for you.
#7

[eluser]xwero[/eluser]
[quote author="jude" date="1203720500"]#This is the article controller (class is called Db)
Code:
function article()
    {
                $this->load->helper('url');
                $this->load->database();          
$id = $this->uri->segment(3, 0);            
$query ['info'] = "SELECT id, title, author, article, metdes, metkey, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts  

WHERE id = $id";
  $this->load->view('articleview.php', $query);
    }
[/quote]
This code seems odd to me. You don't process the query in your controller but in your view? Normally you put database actions in a model but you can do it in the controller
Code:
function article()
{
    $this->load->helper('url');
    $this->load->database();  // you can load the database in the constructor or autoload it        
    $id = $this->uri->segment(3, 0);
    if($id == 0)
    {
         redirect('articles'); // no id no article
    }
    else
    {        
        $query = $this->db->query("SELECT id, title, author, article, metdes, metkey, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts WHERE id = $id");
        if($query->num_rows() == 0)
        {
            redirect('articles'); // no result no article
        }
        else
        {
              $data['info'] = $query->result_array(); // or result() if you want the object
              $this->load->view('articleview.php', $data); // show article
        }
    }

}
#8

[eluser]jude[/eluser]
Thanks a lot ontguy and xwero.

I do the processing at the controller, not in the view. I have recently attempted pagination, and that necessitated a rewrite to include a model also.

I am basically a newbie and my needs are simple. In the CI user guide it is mentioned that you can do "query based strings" instead of segment based URIs, which CI adopts as default. I have adopted the CI default which is more search engine friendly. However, what is the substitute for the code below, if you decide to go by segment based URIs?

index.php?c=products&m=view&id=345

Surprisingly, my substitute so far, which is given below, works like the breeze if you are using localhost. If you are using a www-type URL, it fails to fire. Possibly, the Controller fails to get the $id.

echo '<a href="'.base_url().'Db/article/'.$id.'">Read More</a>';

I have tried a lot of _POST code, but get lots of warnings from PHP. Basically, you can do _POST only if you use a form. I need a simple readon link. Even my attempts at using sessions were not to successful.

Basically, I need a better substitute for the code above. What are my options?

Thanks.
#9

[eluser]ontguy[/eluser]
These portions of code may help:

views/articles.php
Code:
&lt;? foreach ($articles as $article) { ?&gt;
      <tr>
        <td>&lt;?= anchor('articles/view/' . $article->id, $article->title); ?&gt;</td>
        <td>&lt;?= unix_to_human($article->modified); ?&gt;</td>
      </tr>
    &lt;? } ?&gt;

controllers/articles.php
Code:
function view($id) {
    $query = $this->db->getwhere('articles', array('id'=>$id));
    $this->data->article = $query->row();
    $this->load->view('view', $this->data);
  }

views/view.php
Code:
<h4>&lt;?=$article->title?&gt;</h4>
  <p>
    &lt;?=$article->text?&gt;
#10

[eluser]xwero[/eluser]
If i understand correctly you have a products controller and in it a view method and you want to call it with the url db/article?

This can be done by using a route in the config/routes.php file
Code:
$route['db/article/:num'] = 'products/view/$1';




Theme © iAndrew 2016 - Forum software by © MyBB