Codeigniter--add “active” css class, how to apply on a link? - php

i have a url like this
http://localhost/bestbookfinder.com/viewallbooks/books/pgn/grid/6
where:
bestbookfinder.com: Project name
viewallbooks : class name
books :function
pgn :Constant Parameter
grid :view type
6 :pagination current page number
so now i am trying to read the above shown url and to apply the CSS class with the following code, but unfortunately my code is not working.
I think i am making mistake when i tried to read the url.
Please help me to solve this problem
<?php
if ( $this->uri->uri_string() == '/books/pgn/grid' )//i think mistake is here
{
echo "".anchor(base_url().'viewallbooks/books/pgn/grid/'.$this->uri->segment(5),' Grid', array('id' => 'gridactive'))."";
}
?>

From the codeigniter manual - uri_string() returns a string with the complete URI. So in your example that would be "viewallbooks/books/pgn/grid/6".
Therefore "viewallbooks/books/pgn/grid/6" != '/books/pgn/grid'
Just do something like this:
if ($this->uri->segment(4) == "grid")
{
// do something
}

if($this->uri->segment(4) == 'grid'){
//carry on

Related

Access id of paginator in controller

Hello guys I am new to Zendframework2.
I have the following line in view\partial\paginator.phtml
<a href="<?php echo $this->url($this->route);?>?page=<?php echo $page; ?>?id=<?php echo $this->id?>">
In browser it looks like http://new_project.localhost/districts?page=2?id=4
new_project is the name of my project, districts is route of controller and
in id=4, 4 is id which I want to access in controller.
I tried:
$_GET['id'];
$this->params()->fromRoute('id'); //also
$this->params()->fromQuery('id'); //also
But non of these works.
How I will access this id in controller?
I solve it, just by putting condition like below:
if (isset($_GET['id']))
{
// perform operation here.
}
Don't build your A element tag href yourself but use the framework like you should.
Url view helper:
// $this->url($routeName, $routeParams, $options, $reuseMatchedParams = false)
$this->url('my/route', ['id' => $object->getId()], ['query' => ['page' => $page]])
Controller plugin:
$this->params('id') // will take ID either from routeParameters or queryParameters.
$this->params()->fromRoute('id') // will take the id from the routeParameters
$this->params()->fromQuery('page') // will take the page from the route query
So when you've got a route with a routeParameter id but your query also has the id within it's query its best practice to keep on using the $this->params()->fromRoute('id') or $this->params()->fromQuery('id') so you always know where the value comes from instead of using $this->params('id').
As with the params plugin you can also define default values on the second argument, like: $this->params()->fromQuery('page', 1). So page will always be 1 if the user did not provide one. The default of the second argument is null. Hope this helps and makes you stop using $_GET['bla']
I think your url should look like this:
http://new_project.localhost/districts?page=2&id=4
^
not ?
Because of the mistake you made the parser probably doesn't parse the id in your url correctly meaning you cannot access it like you tried.
It would be better to use the url view helper for rendering the query parameters in the url then you wouldn't make such a mistake. You can check this post on StackOverflow for reference on how to add query parameters to a url using the url view helper. In your case it would look something like this:
<?php
$href = $this->url($this->route, [], ['query' => [
'page' => $page,
'id' => $id,
]]);
?>
<a href="<?php echo $href ?>">
Now you get the correct url:
http://new_project.localhost/districts?page=2&id=4
Meaning the parser also manages to parse the id correctly.
You should be able to access it now like you tried with:
$this->params()->fromQuery('id'); //returns your id from the url

Using three elements condition in PHP

I have a weird bug.
I have A class names Lessons with 3 elements: video, audio & written.
A functios called IsReadOnly supposed to check if a lesson has something only in the 'written' cell while the 'audio' and the 'video' are empty.
This is how it looks:
public function IsReadOnly() {
if ($this->Audio=="" && $this->Video=="" && $this->WrittenLesson!="")
return true;
else
return false;
}
In the PHP page I'm calling the function:
if (!$Less->IsReadOnly()) {
// ...
}
else {
// ...
}
as far as i get - while video/audio will return false, the full condition will return false also and the IF (not the ELSE) will be executed.
But, thats not working. If the video is empty and the audio isn't - everythig is fine. but if the audio is empty and the video isn't - the ELSE is executed. (I've tried replacing between them, nothing...).
What am I doing wrong?
Thanks.
Perhaps you need to make the if more forgiving:
if ($this->WrittenLesson && !$this->Audio && !$this->Video)
Apparently the solution was to make the SSCCE...
While testing I've found the problem. The video wasn't the only cell I had to check because in the database there were two fields that represent video.
Thanks!

Zend Framework 1 pass parameters using get to the route

I hope the title does not sound too confusing, but I had no idea how to name my problem.
Brief intro:
I'm using Zend 1.1X.
At the moment I've been working with a search form sending few parameters via POST.
Now I have to change it to use GET, I have a route created looking similar to that:
"search/what/:what/shape/:shape"
and so on, I also have 2 optional parameters which takes null as default.
I'm trying to generate an URL (using Zend View Helper Url) at form's action, but it throws an exception:
Uncaught exception 'Zend_Controller_Router_Exception' with message what is not specified
I Now don't have idea what should I do. If I change my route to "search" only, it then sends the form correctly, but I end up with "search?what=XXXX&shape=YYYY" instead of "search/what/XXXX/shape/YYYY".
Is there any way that could be handled the way I like??? :>
#EDIT
I think this should also be mentioned - I have a different form, similar one, pointing to a route without parameters specified as well and the uri gets "translated" to the form of "key/value" pairs. The only difference between them is that the first one does not use Url helper, instead has the method part hard-coded and my form is being submitted programatically (button => jQuery stuff => submit). Would that make a difference here, as I believe it should not? :>
I hope any possible source of this behaviour will come up to you, because I'm really stuck at the moment and I simply can't find what's wrong..
Thanks in advance!
With the GET method a form generates an url like this: action?param1=val1&param2=val2&....
I see two solutions:
The first is to regenerate the URL by javacsript, we can imagine something like this:
<form method="get" id="id_form">
....
</form>
<script>
var objet_form = document.getElementById('id_form');
function gestionclic(event){
var url = objet_form.action;
for(var i = 0; i < objet_form.length; i++){
url += "/" + objet_form[i].name + "/" + objet_form[i].value;
}
objet_form.action = url;
}
if (objet_form.addEventListener){
objet_form.addEventListener("submit", gestionclic, false);
} else{
objet_form.attachEvent("onsubmit", gestionclic, false);
}
</script>
But I don't think this is a good solution.
The second is to manage it with a plugin:
For the plugin, it must be declared in the bootstrap.
For example:
public function _initPlugins(){
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Application_Plugin_PRoutage());
}
with this example, the application/plugins folder, create the PRoutage.php plugin like this:
class Application_Plugin_PRoutage extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
...
}
}
and with the variable $request you have access to your data as an array with $request->getParams().
We can imagine something like this:
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$param = $request->getParams();
$what = "";
$shape = "";
if (isset($param['what']) $what = $param['what'];
if (isset($param['shape']) $shape = $param['shape'];
if ($what == "XXXX" && $shape == "YYYY"){
$request->setControllerName('other_controler')
->setActionName('other_action')
->setDispatched(true) ;
}
}
I hope it will help you

Codeigniter if controller

First of all sorry if its a noob question.
But is it posibble to do this in codeingiter, like if i have a sidebar but i only want to load it in 2 pages
if(controller == 'blog') {
//load sidebar
}
just like in wordpress if is_page
Use $this->router->fetch_class()
if($this->router->fetch_class() == 'blog') {
//load sidebar
}
Also $this->uri->segment(2) will work in most cases, but in some cases like mod_rewrite or when using subfolder or route it may fail.
More simply you can do like this.
$controller_name = $this->CI->router->fetch_class();
if($controller_name === "your_controller_name")
{
//your logic
}

Site search with CodeIgniter?

I need to make a simple site search with pagination in it; could anyone tell me how to do it without affecting the URL structure? Currently I'm using the default CodeIgniter URL structure and I have removed index.php from it. Any suggestions?
You could just use a url like /search/search_term/page_number.
Set your route like this:
$route['search/:any'] = "search/index";
And your controller like this:
function index()
{
$search_term = $this->uri->rsegment(3);
$page = ( ! $this->uri->rsegment(4)) ? 1 : $this->uri->rsegment(4);
// some VALIDATION and then do your search
}
Just to update this question. It is probably best to use the following function:
$uri = $this->uri->uri_to_assoc()
and the result will then put everything into an associative array like so:
[array]
(
'name' => 'joe'
'location' => 'UK'
'gender' => 'male'
)
Read more about the URI Class at CodeIgniter.com
Don't quite understand what you mean by "affecting the url structure". Do you mean you'd want pagination to occur without the URL changing at all?
The standard pagination class in CI would allow you to setup pagination so that the only change in the URL would be a number on the end
e.g if you had 5 results to a page your urls might be
http://www.example.com/searchresults
and then page 2 would be
http://www.example.com/searchresults/5
and page 3 would be
http://www.example.com/searchresults/10
and so on.
If you wanted to do it without any change to the URL then use ajax I guess.
Code Igniter disables GET queries by default, but you can build an alternative if you want the url to show the search string.
Your url can be in the notation
www.yoursite.com/index.php/class/function/request1:value1/request2:value2
$request = getRequests();
echo $request['request1'];
echo $request['request2'];
function getRequests()
{
//get the default object
$CI =& get_instance();
//declare an array of request and add add basic page info
$requestArray = array();
$requests = $CI->uri->segment_array();
foreach ($requests as $request)
{
$pos = strrpos($request, ':');
if($pos >0)
{
list($key,$value)=explode(':', $request);
if(!empty($value) || $value='') $requestArray[$key]=$value;
}
}
return $requestArray ;
}
source: http://codeigniter.com/wiki/alternative_to_GET/

Categories