Codeigniter links for search results - codeigniter

I'm a new to CodeIgniter and I have a problem when I display a list of items, I want each items to be a link, so when the user clicks that particular item, it will show the details of that item. Or perhaps give some suggestion on what I should google for. I have literally no idea whats terms or keywords that should I search about.
controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller
{
public function index()
{
$this->getListOfAgent();
}
function getListOfAgent()
{
$this->load->model("agentDB_model");
$data['results'] = $this->agentDB_model->getAllAgentInfo();
$this->load->view("viewAgent_view", $data);
}
function userInformation()
{
//how to i get the selected item which the user clicked and show the details accordingly?
}
}
model
<?php
class agentDB_model extends CI_Model
{
function getAllAgentInfo()
{
$query = $this->db->query("SELECT * FROM user");
return $query->result();
}
function addAgent($newAgent)
{
$this->db->insert("user", $newAgent);
}
}
view
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<div id="content">
<h1>Home Page</h1>
<p>List Of Agent in database</p>
</div>
<?php
foreach($results as $row)
{
echo "<a href = 'Site/userInformation'>$row->userName</a>";
echo "</br>";
echo "</br>";
}
//$this->load->controller(Site/userInformation);
?>
<div id="footer">
<p>Copyright (c) 2012 basicsite.com</p>
</div>
</body>
</html>

You need to tell the Site/userInformation method for what record you want the information. You use URI segments for this.
So, in your view, change the following line:
echo "<a href = 'Site/userInformation'>$row->userName</a>";
to:
echo "<a href = 'Site/userInformation/$row->userID'>$row->userName</a>";
Then, in your controller method, add the parameter to the method declaration:
function userInformation($userID)
{
// now, you use the model to get the correct record from the db based on
// the $userID and display the information
}

Related

Trying to get property of non-object - Undefined variable: data/ Codeigniter

i'm try to get news_id from database but when go to view say : error -> Trying to get property of non-object / Message: Undefined variable: data
this model - >
class Model1 extends CI_Model {
public function get_art()
{
$query = $this->db->get('entries');
return $query->result();
}
}
here controller Code - >
class Home extends CI_Controller
{
public function members()
{
$this->load->model('model1');
$data=$this->model1->get_art();
$this->load->view('members', $data);
}
}
and this Full View - >
<html>
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<h1>
<? echo $data->body; ?>
</h1>
</body>
</html>
This is because of you invalid pass data to view. At controller replace line
$data=$this->model1->get_art();
with
$data["query"] = $this->model1->get_art();
Then at view you will have var $query with results of your database query.
You can use it like this:
<h1>
<? foreach($query as $row) {
echo $row->body;
}
?>
</h1>

Oauth Implementation using PHP and JS facebook SDK

Hai Fellow Developers,
I am implementing A facebook Login to my Web App using codeigniter (E-commerce kind of platform).
So, I have lot of filters in my site based upon them i am trying to fetch data and requesting a service. so user has to log in to request a service, so at last i am forcing user to login to continue (like buying something).
Here comes the problem, I implemented Facebook login using Javascript SDK and trying to get accesstoken. and created a FACEBOOK library in codeigniter which fetches user's data using FacebookJavaScriptLoginHelper. and now i should update user details in all sessions and attach the user name dynamically to the current view in codeigniter.
you can look below what i have tried upto now:
Facebook Libabry:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( session_status() == PHP_SESSION_NONE ) {
session_start();
}
// Autoload the required files
require_once( APPPATH . 'libraries/facebook/autoload.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\FacebookJavaScriptLoginHelper;
use Facebook\GraphObject;
use Facebook\GraphUser;
class Facebook {
var $ci;
var $helper;
var $session;
var $permissions;
public function __construct() {
$this->ci =& get_instance();
$this->permissions = $this->ci->config->item('permissions', 'facebook');
// Initialize the SDK
FacebookSession::setDefaultApplication( $this->ci->config->item('api_id', 'facebook'), $this->ci->config->item('app_secret', 'facebook') );
$this->helper = new FacebookJavaScriptLoginHelper();
// No session exists
try {
$this->session = $this->helper->getSession();
} catch( FacebookRequestException $ex ) {
} catch( Exception $ex ) {
// When validation fails or other local issues
}
}
/**
* Returns the login URL.
*/
public function login_url() {
return $this->helper->getLoginUrl( $this->permissions );
}
/**
* Returns the current user's info as an array.
*/
public function get_user() {
if ( $this->session) {
$request = ( new FacebookRequest( $this->session, 'GET', '/me' ) )->execute();
// Get response as an array
$user = $request->getGraphObject()->asArray();
return $user;
}
return false;
}
public function logout(){
session_start();
session_destroy();
}
}
And in My controller i have my index function like this
public function index(){
$fb_data = $this->facebook->get_user();
$profile_data=array(
'name'=>$fb_data['name'],
'id' =>$fb_data['id'],
'image'=>'http://graph.facebook.com/'.$fb_data['id'].'/picture?width=300',
'email'=>$fb_data['email'],
'oauthProvider'=>'facebook',
);
$this->session->set_userdata('user_name', $fb_data['name']);
echo json_encode($profile_data);
}
My Header View:
<?php if($this->session->userdata('user_name')): ?>
<li id="userName" class="dropdown">
Welcome <span class="user_name"><?php echo $this->session->userdata('user_name')?></span><span class="caret"></span>
<ul class="dropdown-menu">
<li>Account settings</li>
<li>Logout</li>
</ul>
</li>
<?php else: ?>
<li id="login">Login</li>
<?php endif; ?>
Ajax call
$.ajax({
url:"<?php echo base_url('redirectoauth');?>",
success:function(data){
var data=$.parseJSON(data);
$('.user_name').html(data.name);
$('#userName').css('display','block')
$('#login').css('display','none');
$('#socialLogin').modal('hide');
}
});
Any references or tuts,suggestions on architecture to overcome this problem.
this will fix your issue
ajax call
$.ajax({
url:"<?php echo base_url('redirectoauth');?>",
success:function(data){
var data=$.parseJSON(data);
$('.user_name').html(data.name);
$('#logined').css('display','block')
$('#sociallogined').css('display','none');
$('#socialLogin').modal('hide');
}
});
view
<ul id="logined" <?php echo $this->session->userdata('user_name') ? 'style="display:none"' :''; ?>>
<li id="userName" class="dropdown">
Welcome <span class="user_name"><?php echo $this->session->userdata('user_name')?></span><span class="caret"></span>
<ul class="dropdown-menu">
<li>Account settings</li>
<li>Logout</li>
</ul>
</li>
</ul>
<ul id="sociallogined">
<li id="login">Login</li>
</ul>

Undefined variable : variable from controller to view

Hi i dont know why its not any more working.
i got undefinied variable if i try to echo the variable in the view.
Here the controller
class save_settings extends CI_Controller {
function save()
{
$data['test'] = 'content';
$this->load->view('help', $data);
}
}
and view
<!DOCTYPE HTML>
<html lang="de">
<head></head>
<body>
<?php echo $test ; ?>
</body>
</html>
Some issue to check -
1) Correct view name and path
2) Correct URL - /save_settings/save
3) Try to print and exit something to check whether the controller is loading perfectly.
function save()
{
$data['test'] = 'content';
echo '<pre>'; print_r($data); exit; // <== Debug
$this->load->view('help', $data);
}
// Should give
Array(
[test] => content
)

jquery-mobile + codeigniter

What must be the problem with this code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $title; ?></title>
<?php
echo link_tag('assets/css/jquery.mobile-1.3.2.min.css', 'stylesheet');
echo script_tag('assets/js/jquery-1.10.2.min.js');
echo script_tag('assets/js/jquery.mobile-1.3.2.min.js');
?>
</head>
<body>
<div data-role="page">
<header data-role="header">
Show
<h3><?php echo $title; ?></h3>
<div data-role="controlgroup" data-type="horizontal" class="ui-btn-right">
My Account
Logout
</div>
</header>
I'am using jquery mobile for my client-side script and PHP(codeigniter) for server-side script.
When I refresh the page after including the in anchor the page now doesn't display the page anymore.
Can anyone tell what's wrong with the code or I'am just missing something.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Event_management_c extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('html', 'url', 'form');
}
public function index() {
$data['title'] = 'Events';
$data['reply_title'] = 'Reply Message';
$this->load->view('fragments/header', $data);
$this->load->view('fragments/nav', $data);
$this->load->view('events/index', $data);
$this->load->view('fragments/footer', $data);
}
}
?>
Problem here
Logout
to
Logout
you can use template library for codeigniter like TEMPLATE LIBRARY
and reload the script and style files.
Based on your extra answers from the comments; the problem is most likely that the function site_url() isn't defined. The result of this is a fatal error, which isn't shown to you due to settings in php concerning error_report.
To solve this, run $this->load->helper('url') in relevant controller, or simply add url to the helper array in application/config/autoload.php. Since this function is so common, I recommend to autoload it.

Laravel without Blade - Controllers and Views

I am more efficient at setting up my view logic with straight up php. Blade is cool but it's not for me. I am trying to translate all the Blade specific examples and docs to just php. I don't like the fact that I need to assign all the variables for my views in an array of View::make(). I did found all of this so far.
controllers/home.php:
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public function action_index()
{
$this->layout->name = 'James';
$this->layout->nest('content', 'home.index');
}
}
views/layouts/default.php:
// head code
<?php echo Section::yield('content') ?>
// footer code
views/home/index.php
<?php Section::start('content'); ?>
<?php echo $name ?>
<?php Section::stop(); ?>
I am greeted with this error: Error rendering view: [home.index] Undefined variable: name. I know that $this->layout->nest('content', 'home.index', array('name' => 'James')); works but that negates my point about having to send all my variables to an array. This can't be the only way.
The view templating docs doesn't seem to touch on doing variables with nested views from controllers.
you can pass variables this way;
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public function action_index()
{
$this->layout->nest('content', 'home.index')
->with('name', 'James');
}
}
Here's an example of how I'm templating with laravel.
Class Products_Controller extends Whatever_Controller {
public $layout = 'layouts.main';
public function get_index()
{
// .. snip ..
$view = View::make('home.product')
->with('product', $product); // passing all of my variable to the view
$this->layout->page_title = $cat_title . $product->title;
$this->layout->meta_desc = $product->description;
$this->layout->content = $view->render(); // notice the render()
}
}
my main layout looks like
<html>
<head>
<title> {{ $page_title }} </title>
<meta name="description" content="{{ $meta_desc }}" />
</head>
<body>
{{ $content }}
</body>
</html>
and the home/product page looks like
<div class="whatev">
<h1> {{ $product->title }} </h1>
<p> {{ $product->description }} </p>
</div>
Hope that helps you clear some things up
I know it has been a while on this question, but since it was asked, Laravel 4 has come out and there are newer ways to do things.
If you are reading this these days you should consider using View Composers to prepare the data for your views.
Example:
class MyViewComposer {
public function compose($view){
$view->title = 'this is my title';
$view->name = 'joe';
...
$view->propertyX = ...;
}
}
After setting up your view composer register it with the app:
View::composer('home.index', 'MyViewComposer');
For more information check out the laravel docs on view composers:
http://laravel.com/docs/responses

Resources