codeigniter linking to another page - php

i am new to codeigniter framework. i am having problem with href link. in my home page i have some menu, that goes to different page. for example in normal php if i want to go Sell Books page then i just put sellBook.php in href link. now in codeigniter how can i do this. do i need write something in controller ?
<li >Home </li>
<li>Buy Books </li>
<li>Sell Books </li>
<li>Books on Demand </li>
<li>Request a Book </li>
<li>About Us </li>

as per MVC pattern each of your url is a controller so:
if you, for example, want to link to
http://www.site.com/users
so the controller will look like:
class Users extends CI_Controller{
function index(){
//do somenthing here
}
function list(){
//list your users
}
}
then in your views linking to that controller is simple:
this will link to your users controller and index() method
this will link to your users controller and list() method
the site_url(); method will helps you finding the right link both if
you are using index.php or not in your urls

<a href="<?php echo base_url() ?>controller_name/function_name">

If you are using mod_rewrite to remove the index.php from the URL, you can write your URL as href="/sellbook" otherwise you will have to include it, such as href="/index.php/sellbook".
This assumes, of course, that you have a properly configured route name sellbook. See http://ellislab.com/codeigniter/user-guide/general/routing.html for details on how to achieve this.

If the links are to other pages on your site, then I think your best bet will be to use the url helper, assuming you're using the standard seo friendly format and not query strings. This way, you don't have to take into consideration whether you'll be using .htaccess or not. For example:
echo anchor('your_controller_name/your_function_name/param_1/param_2', 'Sell Books', 'title="Sell Books"');
// or for your home page
echo anchor('/', 'Home', "title='Home'");
If it's an external link, you can use the same function or just a plain html tag:
echo anchor('http://google.com', 'Google Me', 'title="Google Me"');
// OR
<a href="http://google.com" title="Google Me" >Google Me</a>
Note: make sure you load your url_helper in your controller if you'll be using the anchor function:
$this->load->helper('url')
// or add to autoload.php
You can also do it the way #sbaaaang suggested.

Account Details
You can use like this

<li>Home</li>
<li>Buy Books </li>
<li>Sell Books </li>
<li>Books on Demand </li>
<li>Request a Book </li>
<li>About Us </li>

//use
site_url in link with controller name and method
and
$this->load->view('page_name');

Related

How nav bar works in this php code using codeIgniter?

This is a source code of nav bar made using PHP framework CodeIgniter.I didn't understand login behind nav url of "Bulk Conversion" and "Login" which is given using site_url instead of <a href = "bulk_conversion.php"> and <a href = "login.php">. How nav bar code works here?
<div class="header clearfix">
<nav>
<ul class="nav nav-pills pull-right">
<li role="presentation" class="{{ $_nav === 'batch' ? 'active' : '' }}">Bulk Conversion</li>
<li role="presentation">Login</li>
</ul>
</nav>
CodeIgniter is an object-oriented MVC framework. For the most part you don't make calls to .php files by name. Instead, CodeIgniter uses a segment-based approach to URLs with a one-to-one relationship between a URL string and its corresponding controller/method and optionally, parameters. Any URI segments after the first two are considered parameters (variables) passed to the controller's method. Find a better explanation of what I'm trying to explain in the Documentation.
site_url is a CodeIgniter helper function that will construct a full URL from a URI string. So the call to site_url('batch') for a website on example.com will produce the string http://example.com/batch. This will result in the index() method of the controller Batch being executed.
The call site_url('admin') works the same except the Admin controller is being called.
Clear as mud?

codeigniter's controller to main controller in a specific div

<li>SERVICES</li>
<li>ABOUT</li>
<li>CONTACT</li>
I'm using CodeIgniter and this code was in a different controller. suppose http://localhost/defctrl/function1
I want to return to my homepage "maine/home" in a specific div when you click on the list but the above code doesn't work. neither ../maine/home#services.
How can I solve this problem?
if it's on the same page you can use
<li><a href="#services"
if another page
<li><a href="<?php echo base_url() ?>{route or controller name}#services"
Make sure URL helper is loaded and site load with index.php

If I want to mark as active page in the navigation menu in menu.php

I used 'include' php to separate header of my website.
So, I can easily fix if I need to change the navigation menu part in the header, instead fixing more than 20 pages each.
My question is I like to add a class, 'current' in the one of navigation button.
For example, if I am in 'Home' page, then I want to change font color of 'Home' button to red.
If I move to 'Contact' page, I want 'Contact' button to be changed to red and want 'Home' button to normal color.
Since all navigation button codes are in the header.html.
How can I add class 'current', so users can know which page they are looking at?
Thanks in advance.
If you are using php then you can set it like this.
1) Give class to each link
<li class="home">Home</li>
<li class="about">About</li>
<li class="contact">About</li>
Note : Give filename & classname same (If filename is home.php then class for this menu is "home")
2) In header.php use this code.
<?php
$class = basename($_SERVER['REQUEST_URI'], '.php?' . $_SERVER['QUERY_STRING']);
/* This basename function returns filename from url. For example if url is http://www.example.com/home.php?id=15, then this will return "home" only. */
?>
<script type="text/javascript" src="jquery.min.js"> <!-- Link your jquery library -->
<script type="text/javascript">
$(document).ready(function(){
$(".<?php echo $class; ?>").addClass('current');
});
</script>
This is a very basic and unsafe example, just so that you hopefully get the idea.
Find out first what page you're on, maybe you have a URL parameter called page that you call like index.php?page=home or index.php?page=contact.
<?php $page=$_REQUEST['page']; ?>
Then write your HTML:
Home<br>
Contact
Now add the class-checks to your links:
Home<br>
Contact
(This uses fancy inline IF statements, just because they fit the purpose so nicely. If you don't know them yet, I recommend to read up on them.)
If your $page variable is set to "home", this will generate the HTML source like so:
Home<br>
Contact
You could also include the entire class assignment into the PHP check:
<a href="index.php?page=home"<?=($page=='home'?' class="current"':'');?>>Home</a><br>
<a href="index.php?page=contact"<?=($page=='contact'?' class="current"':'');?>>Contact</a>
And that would generate the HTML source like this:
Home<br>
Contact
The most practical way would be to quite simply make a little function that generates everything for you, like for example this one:
<?php
function makeNavLink($pageName){
global $page;
$link='<a href="index.php?page='.$pageName.'"';
$link.=($page==$pageName?' class="current"':'').'>';
$link.=ucwords($pageName).'</a>';
return $link;
}
?>
That would allow you to call the function in your page like this:
<?=makeNavLink("home");?><br>
<?=makeNavLink("contact");?>
And it would also make the HTML output look like this if your page is "contact":
Home<br>
Contact
I can't comment because I don't have 50 rep, but I did some research and found this link How to have the class=“selected” depending on what the current page/url is. $_SERVER['REQUEST_URI'] is the approach that is used in this example. So if you need further clarification, you can look that up too.
Edit: This example does not require JQuery. Or you could try this:
<div class="menu">
<div id="whatever" class="whatever">
<ul>
<li><a href="index.php" <?php if (basename($_SERVER['PHP_SELF']) == "index.php") { ?> class="current" <?php } ?>>Home</a></li>
<li><a href="about.php" <?php if (basename($_SERVER['PHP_SELF']) == "about.php") { ?> class="current" <?php } ?>>About</a> </li>
</ul>
</div>
</div>

add site links to magento template files

What is the best way to add a link to a Magento template file?
At the moment I'm using the below method but I'm wondering if there is a better way which calls a native magento method?
<?php echo $this->__('About Us'); ?>
?php echo $this->__('Shopping Bag'); ?>
I know you can use <img src="<?php echo $this->getSkinUrl('images/test.png');?>" /> to get an image url, is there something similar for links?
Use this code to get URL in template file
$this->getUrl('module/controller/action'); //for controller pages
$this->getUrl('', array('_direct'=>'some-url-key')); //for cms pages
$this->getUrl('module/controller/action', array('_query'=>'a=2&b=5')); //for query params
$this->getUrl('', array('_direct'=>'some-url-key', '_query'=>'a=2&b=5')); //for query params
Refer this link https://magento.stackexchange.com/questions/14443/magento-get-store-url-in-cms-page
You can put the links on the static block and then call that static block in your template file.
In Static block code will be
<ul>
<li>About Us</li>
<li>Customer Service</li>
</ul>

Dynamic Navigation?

Currently for my projects I create the navigation for each page manually and it looks something similar to:
<nav>
<ul id="mainMenu"><!--Main Menu-->
<li><a class="active" href="index.php">Home</a></li>
<li>Contact</li>
</ul>
</nav>
This works fine, however for projects that has many many pages it is not a really good practice or even efficient to do it manually. So I was wondering if there is anyone who can direct me to the right path and advice me on how to make my navigation dynamic? I know about PHP include and the .inc files - they are good. BUT I want to add class .active to the <a> of the page that is currently open. How can I do that?
BTW: I don't know if this s the right place to post this sort of questions here, but the moderator told me to post it here.
Use include to add a central php file, that contains a function which can take the current page as a parameter:
nav.inc:
function renderNavigation($current_page) {
//render you navigation
}
main.php:
require_once("nav.inc");
renderNavigation("Subpage 1")

Categories