Any CodeIgniter Expert Gurus out here? - jjalvi - 08-30-2015
I am trying to figure out a way to dynamically change the Page title of my product pages and make the URL SEO friendly.
I have the following code in my routes.php file
$sql = "SELECT id, title FROM add_pages";
$result = mysql_query($sql);
while ($info = mysql_fetch_array($result)) {
$str=str_replace(' ','%20',$info['title']);
$route[$str] = 'front_controller/display_content/'.$str;
}
$sql1 = "SELECT * FROM product_mstr";
$result = mysql_query($sql1);
while ($info = mysql_fetch_array($result)) {
$route['sell_your_phone/'.$info['id']] = 'front_controller/sell_your_phone/'.$info['id'];
}
As you can see the url is made of product ID. I am trying to change the product ID to be the value that's saved in product NAME section in my database. The problem here is that the product name is part of the body whereas the title and URL is being being pulled before the content even loads. I can't figure out how to actually achieve SEO friendly URL's for my product pages and Dynamic Page Titles using data stored in my database.
Any Help?
Also the website I am looking at here is www.webuyback.com.au
RE: Any CodeIgniter Expert Gurus out here? - davidgv88 - 08-31-2015
Hi jjalvi.
For dynamically change the Page title of your product pages:
In your view you can send the title from your controller.
In controller:
Code: $data = array();
$data['page_title] = 'The Page title';
$this->load->view('your_view',$data);
In your_view.php
Code: <?php
if(empty($page_title)){
$page_title = 'Default Title';
}
<html>
.....
<title><?php echo $page_title; ?></title>
</html>
?>
RE: Any CodeIgniter Expert Gurus out here? - rtorralba - 08-31-2015
(08-31-2015, 12:14 AM)davidgv88 Wrote: Hi jjalvi.
For dynamically change the Page title of your product pages:
In your view you can send the title from your controller.
In controller:
Code: $data = array();
$data['page_title] = 'The Page title';
$this->load->view('your_view',$data);
In your_view.php
Code: <?php
if(empty($page_title)){
$page_title = 'Default Title';
}
<html>
.....
<title><?php echo $page_title; ?></title>
</html>
?>
I prefer in view to use a ternary operator, but is just a preference:
Code: <title><?= empty($page_title) ? $page_title : 'Default Title' ?></title>
RE: Any CodeIgniter Expert Gurus out here? - jjalvi - 08-31-2015
(08-31-2015, 01:28 AM)rtorralba Wrote: (08-31-2015, 12:14 AM)davidgv88 Wrote: Hi jjalvi.
For dynamically change the Page title of your product pages:
In your view you can send the title from your controller.
In controller:
Code: $data = array();
$data['page_title] = 'The Page title';
$this->load->view('your_view',$data);
In your_view.php
Code: <?php
if(empty($page_title)){
$page_title = 'Default Title';
}
<html>
.....
<title><?php echo $page_title; ?></title>
</html>
?>
I prefer in view to use a ternary operator, but is just a preference:
Code: <title><?= empty($page_title) ? $page_title : 'Default Title' ?></title>
Thanks heaps rtorralba and [b]davidgv88 .[/b]
I managed to implement the code successfully.
Now I am just trying to figure out how to put the title of my product automatically as the title of my page. For emaple if you follow this page http://www.webuyback.com.au/sell_your_phone/154 , The title of the page is "WeBuyback.com.au | Sell Your Phones and Tablets for Quick Cash" I would like it to be ," HTC One X 32GB" .
Is there any way to achieve this?
RE: Any CodeIgniter Expert Gurus out here? - davidgv88 - 09-01-2015
(08-31-2015, 09:56 PM)jjalvi Wrote: (08-31-2015, 01:28 AM)rtorralba Wrote: (08-31-2015, 12:14 AM)davidgv88 Wrote: Hi jjalvi.
For dynamically change the Page title of your product pages:
In your view you can send the title from your controller.
In controller:
Code: $data = array();
$data['page_title] = 'The Page title';
$this->load->view('your_view',$data);
In your_view.php
Code: <?php
if(empty($page_title)){
$page_title = 'Default Title';
}
<html>
.....
<title><?php echo $page_title; ?></title>
</html>
?>
I prefer in view to use a ternary operator, but is just a preference:
Code: <title><?= empty($page_title) ? $page_title : 'Default Title' ?></title>
Thanks heaps rtorralba and [b]davidgv88 .[/b]
I managed to implement the code successfully.
Now I am just trying to figure out how to put the title of my product automatically as the title of my page. For emaple if you follow this page http://www.webuyback.com.au/sell_your_phone/154 , The title of the page is "WeBuyback.com.au | Sell Your Phones and Tablets for Quick Cash" I would like it to be ," HTC One X 32GB" .
Is there any way to achieve this?
Hi jjalvi.
In Controller you have a ID 154.
You can do a SELECT in database and get the title of the product.
For Example (Controller):
Code: public function view_product($id){
$header = array();
$where = array(
'id' => $id
);
$product_info = $this->db->get_where('products',$where)->row_array();
$header['page_title'] = 'WeBuyback.com.au | '.$product_info['title'];
$this->load->view('header',$header);
}
RE: Any CodeIgniter Expert Gurus out here? - rtorralba - 09-02-2015
(09-01-2015, 01:27 AM)davidgv88 Wrote: (08-31-2015, 09:56 PM)jjalvi Wrote: (08-31-2015, 01:28 AM)rtorralba Wrote: (08-31-2015, 12:14 AM)davidgv88 Wrote: Hi jjalvi.
For dynamically change the Page title of your product pages:
In your view you can send the title from your controller.
In controller:
Code: $data = array();
$data['page_title] = 'The Page title';
$this->load->view('your_view',$data);
In your_view.php
Code: <?php
if(empty($page_title)){
$page_title = 'Default Title';
}
<html>
.....
<title><?php echo $page_title; ?></title>
</html>
?>
I prefer in view to use a ternary operator, but is just a preference:
Code: <title><?= empty($page_title) ? $page_title : 'Default Title' ?></title>
Thanks heaps rtorralba and [b]davidgv88 .[/b]
I managed to implement the code successfully.
Now I am just trying to figure out how to put the title of my product automatically as the title of my page. For emaple if you follow this page http://www.webuyback.com.au/sell_your_phone/154 , The title of the page is "WeBuyback.com.au | Sell Your Phones and Tablets for Quick Cash" I would like it to be ," HTC One X 32GB" .
Is there any way to achieve this?
Hi jjalvi.
In Controller you have a ID 154.
You can do a SELECT in database and get the title of the product.
For Example (Controller):
Code: public function view_product($id){
$header = array();
$where = array(
'id' => $id
);
$product_info = $this->db->get_where('products',$where)->row_array();
$header['page_title'] = 'WeBuyback.com.au | '.$product_info['title'];
$this->load->view('header',$header);
}
Just a detail, I think is better make database queries at models:
At model (Products_model):
PHP Code: public function get_row($id) { return $this->db->get_where('products', array('id' => $id))->row(); }
At controller (Products):
PHP Code: public function view($id) { $this->load->model('products_model'); $product = $this->product_model->get_row($id); $header = array(); $header['page_title'] = 'WeBuyback.com.au | '.$product['title']; $this->load->view('header',$header); }
|