Codeigniter passing no URI segment - php

I'm using codeigniter for a project.
The URL is segmented like this:
example.com/admin/events
example.com/admin/events/add
I have a class called Admin which has a function called events
function events($data)
{
echo $data;
}
this will echo 'add' for the events/add page but when I try and access the events page alone i get an error
Missing argument 1 for Admin::events()
I thought that I could make two functions, one with a variable being passed and one without but apprently php doesn't allow that.
Fatal error: Cannot redeclare Admin::events()
How can I make this work?
Thanks.

Try that
function events($data = NULL)
{
echo $data;
}

Related

Concrete5 package helper - How to fix Fatal error: Call to undefined method NavigationHelper::getURLSegment() error?

Sorry, we've looked at a million answers but we either don't understand them or they don't apply.
Navigation helper:
We have a concrete5 package and have created a helper file at:
/packages/package-name/helpers/navigation.php
It contains the following:
<?php defined('C5_EXECUTE') or die('ACCESS DENIED') ?>
<?php
class NavigationHelper {
// Get URL segments
public function getURLSegments() {
return explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
}
// Get a specific URL segment
public function getURLSegment($n) {
$segs = $this->getURLSegments();
return count($segs) > 0 && count($segs) >= ($n-1)?$segs[$n]:'';
}
}
Loading the helper
Then we have an element file where we load the package helper like this:
$navHelper = Loader::helper('navigation','package-name');
Calling a method
But when we try to call a method like this:
if ($navHelper->getURLSegment(1) == 'whats-on') {
echo "What's on";
}
We see:
Fatal error: Call to undefined method NavigationHelper::getURLSegment()
We've been starring at the method and package names for an hour so are obviously missing something basic - what are we missing?
Any help would be much appreciated.
Cheers
Ben
For anyone else having this problem, custom tool files can't have the same file name as C5 tools.
navigation.php was already being used by C5 so changing the file name of my custom file made this all work.

Unable to pass a parameter to my function?

I have the following in a common.php file:
$debug = true;
function debug_to_screen(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
print("Debug Mode: " + $value);
}
}
}
In my main file I then call this function with the following:
require("common.php");
if(!isset($_COOKIE["qcore"]))
{
debug_to_screen("Cookie not found: ", var_dump($_COOKIE['qcore']));
}
However I'm receiving the following error:
Fatal error: Cannot pass parameter 1 by reference in
C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\wp-content\plugins\QCORE\login.php
on line 8
This is one of the first time's I've tried making a function that can be passed multiple values, so I don't have the skill set to understand why this isn't working. Basically - I'd like to make a debug function that I can pass multiple values to, that is defined in my common file.
Your problem is here:
debug_to_screen("Cookie not found: "...
You are passing to the function which is expecting the data to be passed by reference - meaning you are sending it the ACTUAL variable, not a copy of it.
You will need to make the array first, then pass i by reference to the function.
Something like this:
$array=$_COOKIE;
debug_to_screen($array);
In your function, you defined it as:
function debug_to_screen(&$array)
The extra & means the function is taking the ACTUAL variable, not a copy of it.
This will also not work:
function julie(&$bob)
{
// something..
}
julie(5);
This is because although the function can change the 5, it cannot be returned via the reference pass.
$var1=5;
julie($var1);
This would have worked perfectly well, as $var1 can be modified and the external code calling the function can use the changed variable.
You function function debug_to_screen(&$array) accepting only one argument and however you are sending text as first argument and cookies as second argument.
So
replace
debug_to_screen("Cookie not found: ", var_dump($_COOKIE['qcore']));
to
debug_to_screen($_COOKIE['qcore']);
There's no need to pass by reference, and you're passing two arguments when only one is defined. You're also trying to print an array, var_dump prints not returns.
I think you need to look at the PHP docs for the functions you're using, there are a number of issues here.

Fatal error: Call to a member function load() on a non-object Joomla

I have Joomla! 1.7.2 Stable and when I try to Log in to administration area I get this error
Fatal error: Call to a member function load() on a non-object in /home/shomos1/public_html/portal/libraries/joomla/user/user.php on line 822
this is the error line in my user.php
public function load($id)
{
// Create the user table object
$table = $this->getTable();
var_dump($table);
// Load the JUserModel object based on the user id or throw a warning.
if (!$table->load($id)) {
JError::raiseWarning('SOME_ERROR_CODE', JText::sprintf('JLIB_USER_ERROR_UNABLE_TO_LOAD_USER', $id));
return false;
}
// Set the user parameters using the default XML file. We might want to
// extend this in the future to allow for the ability to have custom
// user parameters, but for right now we'll leave it how it is.
$this->_params->loadString($table->params);
// Assuming all is well at this point lets bind the data
$this->setProperties($table->getProperties());
return true;
}
}
I am not good in Joomla and I can't understand what is the problem there
please need help what can I do to solve this problem
Thanks
the default com_user component is conflicting with your component so you must rename your component files they must not have name "user" ...

php - creating a function, to use later in same page

I am trying to add a function named "addNotification" to my page "functions.php"
My function:
function addNoti($text,$userid)
{
mysql_query("INSERT INTO notifications (time,text,userid) VALUES('".time()."','$text','$userid')");
if(mysql_affected_rows() != 1)
return 2;
else
return 100;
}
A couple of hundreds line BELOW that^ (same page), I have my registration function "register":
function doRegister()
{
global $datesetting;
mysql_query("query to register goes here");
addNoti("You just joined us!","$userid");
}
Although, whenever I process my registration form, I get the following error:
Fatal error: Call to undefined function addNoti() in /home/domain/public_html/path/to/functions.php on line 278
(Line 278 is where the addNoti is called)
Whenever I try to move addNoti to another page, and load that page in functions.php I get no error.
Please help.
Thank you in advance.
Keep in mind that in PHP you can declare function in conditional block. It could be that in your code execution took the other branch and never got to the function definition. Check the code blocks, check if the code with the function definition gets executed (echo or var_dump are your friends)
Is it a typo? In your question you name the function addNotification and addNoti()...
Do you call doRegister() before addNoti() occurred in your script?! That is the question!
From the two functions only it is hard to say what the problem can be. Did you accidently put the function addNoti inside another function (before the closing bracket)?

How to call function from Controller to include into the View page with Codeigniter?

I want to call function from Controller to include into the View page with Codeigniter. Usually when I open any page, I call $this->load->view() in Controller for open that page. Now I want to include sub page into main page, but it can't include any function in View. I try to include function like this.
<body><? include(site_url().'/login'); ?></body>
<body><? include('/login'); ?></body>
<body><? include('./login'); ?></body>
I can open page with this link http://localhost/ci_house/index.php/login. but when I open main page for run my code it show these error.
A PHP Error was encountered
Severity: Warning
Message: include(http://localhost/ci_house/index.php/login) [function.include]: failed to open stream: no suitable wrapper could be found
Filename: views/main.php
Line Number: 8
A PHP Error was encountered
Severity: Warning
Message: include() [function.include]: Failed opening 'http://localhost/ci_house/index.php/login' for inclusion (include_path='.;C:\php5\pear')
Filename: views/main.php
Line Number: 8
I want to show 2 view in 1 page .
function test1()
{ $data['var_for_login_view'] = 'get table1';
$this->load->view('main1',$data);
}
function test2()
{ $data['var_for_login_view'] = 'get table2';
$this->load->view('main2',$data);
}
In views/main.php:
$this->load->view('test1');
$this->load->view('test2');`
I want to show like
<body>
include('main1.php');
include('main2.php');
</body>
but I can show like this in Codeigniter.
I really can't understand your question well, but hey, you can "include" any view within another view without problems..
In main.php:
$this->load->view('login');
You don't even need to pass it the paramwters, as they are buffered so available to any child view you might insert. But please, be more clear on what you actually need.
If you want to include in main() the same views you load in login() method, of course you don't have to include a CI URI, but just create the variables you need to pass inside the controller's method login(), and then call whatever view you want, be it a view which is designed for this specific method or for any other controller's method.
So, for.ex.
function login()
{
$data['var_for_login_view'] = 'a variable';
$data['var_for_this_view'] = 'another variable';
$this->load->view('main');
}
In views/main.php:
echo $var_for_this_view;
$this->load->view('login');
echo $var_for_login_view;
// see? $data was not passed to $this->load->view('login'), but it's still there nonetheless!
What I understand is this: You have some functions defined in one of your controllers and you want to be able to call these functions from another controller/view.
If this is correct, then we can't normally do this. Here are a couple of alternatives:
Move these functions to a library/helper. Load that library/helper wherever you want and then you can call these functions.
If you absolutely need to call these functions from the controller, you can look into HMVC extension.
You can't include using site url, use this $this->load->view('template', $data); for codeigniter.

Categories