[eluser]mr daniel[/eluser]
Hi guys! So my problem is that I need to grab my variable $page_title from my database 'urls'. I find the error to be "A PHP Error was encountered
Severity: Notice
Message: Undefined variable: page_title
Filename: controllers/g.php
Line Number: 93". I am unable to find the source of this issue as all other sections work.
My model:
Code:
<?php
class Main_model extends CI_Model {
function Main_model()
{
parent::__construct();
}
function store_url($full_url, $page_title)
{
// create the short URL part
$short_url = $this->create_url_part();
$this->load->library('user_agent');
$ip_address = $this->input->ip_address();
$query_str = "INSERT INTO urls (full_url, short_url, ip_address, page_title, date, time) VALUES
( ?, ?, ?, ?, now(), now() )";
$this->db->query($query_str, array($full_url, $short_url, $ip_address, $page_title));
return $short_url;
}
function get_full_url($short_url)
{
$query_str = "SELECT full_url FROM urls WHERE short_url = ?";
$result = $this->db->query($query_str, $short_url);
if ($result->num_rows () ==1)
{
return $result->row(0)->full_url;
}
else
{
return false;
}
}
function create_url_part()
{
$this->load->helper('string');
$is_unique = false;
do {
$url_part = random_string('fourlet', 2);
$is_unique = $this->check_unique_url_part($url_part);
} while (! $is_unique);
return $url_part;
}
function check_unique_url_part($url_part)
{
$query_str = "SELECT id FROM urls WHERE short_url = ?";
$result = $this->db->query($query_str, $url_part);
if($result->num_rows() > 0)
{
return false;
}
else
{
return true;
}
}
function store_redirect_data($short_url, $ip_address, $browser, $referrer)
{
$query_str = "SELECT id FROM urls WHERE short_url = ?";
$result = $this->db->query($query_str, $short_url);
if ($result->num_rows() ==1)
{
$url_id = $result->row(0)->id;
$query_str = "INSERT INTO redirects (url_id, ip_address, browser, referrer, redirect_time) VALUES
( ?, ?, ?, ?, now() ) ";
$this->db->query($query_str, array($url_id, $ip_address, $browser, $referrer));
return true;
}
else
{
return false;
}
}
}
My controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class G extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('main_model');
}
function index()
{
$this->shorten_url();
}
function shorten_url()
{
$this->form_validation->set_rules('url', 'Full URL', 'required|trim|xss_clean|max_length[500]|prep_url');
if ($this->form_validation->run() == FALSE)
{
//not successful yet - show the view
$this->load->view('view_main');
}
else
{
// form validation success - add short url and return to user
$full_url = $this->input->post('url', TRUE);
$page_title = $this->_get_page_title($full_url);
$this->load->library('user_agent');
$ip_address = $this->input->ip_address();
$short_url = $this->main_model->store_url($full_url, $page_title);
// load a view which shows the shortened url
$this->view_data['short_url'] = anchor(base_url() . $short_url, base_url() . $short_url, array('title' => 'Click to try out your short URL'));
$this->view_data['short'] = $short_url;
$this->view_data['full_url'] = $full_url;
$this->view_data['ip_address'] = ($ip_address);
$this->load->view('view_show_short_url', $this->view_data);
}
}
function _get_page_title($url)
{
$file = @file($url);
$file = @implode("",$file);
if(preg_match("/<title>(.+)<\/title>/i",$file,$m))
return $m[1];
else
return '';
}
function o()
{
$short_url = $this->uri->segment(1);
if ($short_url == '')
{
$this->load->view('view_404');
}
$full_url = $this->main_model->get_full_url($short_url);
if(! $full_url)
{
$this->load->view('view_404');
}
else
{
// let's capture reporting
$this->load->library('user_agent');
$ip_address = $this->input->ip_address();
$browser_type = $this->agent->browser();
$referrer = $this->agent->referrer();
$browser_version = $this->agent->version();
$this->main_model->store_redirect_data($short_url, $ip_address, $browser_type . ' ' . $browser_version, $referrer) ;
// redirect them
$this->data['short_url'] = $short_url;
$this->data['url'] = $full_url;
$this->data['page_title'] = $page_title;
$this->data['full_url'] = anchor($full_url);
$this->load->view('view_redirect', $this->data);
}
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
The section
Code:
// redirect them
$this->data['short_url'] = $short_url;
$this->data['url'] = $full_url;
$this->data['page_title'] = $page_title;
$this->data['full_url'] = anchor($full_url);
$this->load->view('view_redirect', $this->data);
loads my view "view_redirect" which is where this $page_title is required for my page title (<title><?php echo $page_title;?> | (website name) is forwarding you!</title>

.
Hopefully it is something obvious that I am missing

Thanks guys!