...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
March 24, 2012

Codeigniter controller and model with same name collison

Question by musa

I’m try something from this comment idea Code Igniter Controller/Model name conflicts

find class name variable on core/CodeIgniter.php :

$class = $RTR->fetch_class(); and change like that:
$class = 'Controller' . $RTR->fetch_class();

now change controller name:

class ControllerUser extends CI_Controller { ...

It works, now I can use User model and User controller. But my question is, Does it make sense? or Does the problem? (sorry my bad English)

Answer by Starx

To get around this issue, normally most people add the ‘_model’ suffix to the Model class names

I think it is better to add a suffix to the Controllers instead, since they are almost never referenced by their class names in your code.

First we need to extend the Router class.

Create this file: “application/libraries/MY_Router.php”

class MY_Router extends CI_Router {
    var $suffix = '_controller';

    function __construct() {
        parent::CI_Router();
    }

    function set_class($class) {
        $this->class = $class . $this->suffix;
    }

    function controller_name() {

        if (strstr($this->class, $this->suffix)) {
            return str_replace($this->suffix, '', $this->class);
        }
        else {
            return $this->class;
        }

    }
}

Now edit “system/codeigniter/CodeIgniter.php”

line 153:

if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->controller_name().EXT))  

line 158:

include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->controller_name().EXT);  

Next, edit: “system/libraries/Profiler.php”, line 323:

$output .= " 
<div style="color:#995300;font-weight:normal;padding:4px 0 4px 0">".$this->CI->router->controller_name()."/".$this->CI->router->fetch_method()."</div>";  

Source

Read more

Passing URL in Codeigniter URL segment

Question by vivek

I want to pass a url like http://example.com/test?a=1&b=2 in url segment of codeigniter.

I’m trying to pass something like this http://myurl.com/abc/http://example.com/test?a=1&b=2 and get the “http://example.com/test?a=1&b=2” url. What should be the best way to do this?

Answer by Valeh Hajiyev

Set your URI protocol to REQUEST_URI in application/config/config.php , like this:

$config['uri_protocol'] = 'REQUEST_URI';

then use GET method:

$this->input->get('a');

EDIT:

Since http://example.com/test?a=1&b=2 is not encoded URL, it isn’t possible. So first, I would encode URL with urlencode function like this:

urlencode('http://example.com/test?a=1&b=2');

it returns something like: http%3A%2F%2Fexample.com%2Ftest%3Fa%3D1%26b%3D2

So I would pass the URL like this:

http://myurl.com/?url=http%3A%2F%2Fexample.com%2Ftest%3Fa%3D1%26b%3D2

then get an example URL with GET method.

$this->input->get('url');

Answer by Starx

Use this technique to get the URL

$url = "http://example.com/test?a=1&b=2";
$segments = array($controller, $action, $url);
echo site_url($segments);

// or create a anchor link
echo anchor($segments, "click me");
Read more

onBlur Event on multiple textfields

Question by deepanka

I am creating a web form and inorder to validate the text fields am using onblur event. But what happens is if I leave a textfield empty and go to other a message box appears and when i try to go back to previous one onblur event of the second one is triggered and it goes like an infinite loop. And i cannot use document.form.write.
Is there any other way,using javascript,to print error message?

Answer by Starx

It will be easy if you use jQuery for this.

$('input[type="text"]').blur(function() 
     ...
});

Pure javascript way would be something like

var inputs = document.getElementsByTagName('input');

for (var i = 0; i < inputs.length; i++)
    inputs[i].onblur = functionHandler;

// Common function
function functionHandler() {
    if(this.value == "") { //get the value of tb triggering the event
        alert('empty'); //show the message
    }        
}

Demo

Read more

function not being called, using jquery .delegate

Question by monkey blot

I want to use jquery delegate to call a function, and this works fine:

$("body").delegate("div", "mouseover", function(){
alert("it works");
});

But I also want to be able to use the same function elsewhere. So rather than writing the same function out several times, I can just declare it separately, and call it by name, right?

But written this way, I never see the alert.

function alertMe(){
alert("it works");
};

$("body").delegate("div", "mouseover", alertMe());

Answer by tusar

Drop the parenthisis while defining delegate. just give the function-name

$("body").delegate("div", "mouseover", alertMe);

Answer by Starx

Creating the common event handler is easy

function alertMe(event){
    //you also need to include the event object, for various actions like stopPropagation()
    alert("it works");
};

$("body").delegate("div", "mouseover", alertMe);
Read more

Hide the span with a class name jquery

Question by Sethen Maleno

I am stuck on what to do when hiding a span that has a certain class name. I can’t use this because it refers to the input. Here is my script:

//uncheck all checkboxes
$("input[type=checkbox]").prop("checked", false);

$("input[type=checkbox]").each( function (index) {
    $(this).addClass("doc" + index);
})

$("input").change( function () {

    var docName = $(this).parent().find("span");
    var className = $(this).attr("class");

if(this.checked) {

        $("span.noneAttached").fadeOut('slow', function () {

            docName.clone().appendTo(".attachedDocuments").addClass(className).after("<br />").text();

        });
    }

else if (!this.checked && ($(".attachedDocuments > span").hasClass(className))) {


    //hide the span with the class name

}

});

The else if checks to see if a checkbox is not checked and if the parent div contains any children with the class name. If so, hide it.

Where do I go from here? I am sure this answer is obvious, but I am just not seeing it.

Answer by Starx

Concatenate the class name to the selector like this

$("span."+className).hide();
Read more

javascript in html head, innerhtml not working?

Question by Sam Adams

 <html>
 <head>
 <script type="text/javascript">
 document.getElementById("eee").innerHTML = "7777";
 </script>


 </head>
 <body>
 <p id="eee">aa</p>

 </body>
 </html>

why does the innerHTML not work in the head, but it does work in the body? Excuse the beginners question but last time i used javascript a year ago this was not a problem at all.

Answer by maerics

You need to wait for the HTML document to be loaded before it can be manipulated.

<script type="text/javascript">
  window.onload = function() {
    document.getElementById("eee").innerHTML = "7777";
  };
</script>

Keep in mind that scripts are evaluated in the order they are listed in an HTML document, so your script gets run while the browser is processing the “head” section. Since the browser hasn’t yet parsed the “body” section it doesn’t know about any element with id “eee”.

However, by assigning a function to the “window.onload” event, you can delay the execution of your inner HTML assignment until the window has completely loaded all of the resources and safely manipulate the document.

Note that if your script was in the body section, after the element with id “eee” was listed, then the script would work without the need to wait for the window “load” event.

Answer by Starx

Its because you script cannot find the element. The scripts in the head load first, when the DOM has been loaded properly or still is loading.

<p id="eee">aa</p>
<!-- Use the script after the element, so that the script can find it -->
<script type="text/javascript">
 document.getElementById("eee").innerHTML = "7777";
</script>

Other methods may be executing the script on the onload events of the window

window.onload = function() {
    document.getElementById("eee").innerHTML = "7777";
};
Read more

Are custom elements valid HTML5?

Question by user1282216

I’ve been unable to find a definitive answer to whether custom tags are valid in HTML5, like this:

<greeting>Hello!</greeting>

I’ve found nothing in the spec one way or the other:

http://dev.w3.org/html5/spec/single-page.html

Douglas Crockford claims:

Custom HTML tags have always been allowed in HTML. In HTML 5 they
become first class

… but he offers no proof.

http://www.crockford.com/html/

And custom tags don’t seem to validate with the W3C validator.

So if anyone can help me definitely answer this, please do!

Thanks!

Answer by Starx

Its not

XML

Creating your own elements in HTML is possible but not valid[Spec]. That’s what XML, SGML and other are for. HTML has a set of rules, which browsers, search engine understand and perform. Why do you want to mess around with that?There is a always a better way

There is a javascript technique that is used to enable HTML5 in IE browsers.

document.createElement(elementName)

You can use this to create any element and have CSS style it as well.

Read more

Setting Height to 100% Not Working and Unknown CSS Change

Question by user1249318

I’m working on http://pizzli.com/darrenwp/?p=107. When I try and set the post background height to 100% to extend the background to the entire length of the post, it doesn’t work, however changing the number of pixels does. One other problem is the HTML> seemed to set a Margin-Top: 28px !important and I’m not sure where this is coming from. Any help would be appreciated.

Answer by sandeep

If there is a float in an element so you have to clear his parent. write now inside #postbg the child have float on it so we have to clear it. Write like this:

#postbg {
    overflow: hidden;
}

Answer by Starx

margin-Top: 28px !important 

Is for the workpress bar on the top of the page. Its not the problem.

Read more

How do I prevent auto-scrolling to the top in jQuery?

Question by Johnny

This works in all other browsers and I have no idea what the problem is. I’m kind of in a hurry so I thought I’d ask you guys in case I missed anything obvious. I have a link you click on which initiate a popup, and in Firefox (and possibly opera) the page auto scrolls back to the top.

    $('[class*=popup-link]').click(function(e) {

    /* Prevent default actions */
    e.preventDefault();
    e.stopPropagation();

    /* Get the id (the number appended to the end of the classes) */
    var name = $(this).attr('class');
    var id = name[name.length - 1];

    /* Show the correct popup box, show the blackout and disable scrolling */
    $('#popup-box-'+id).show();
    $('#blackout').show();
    $("html,body").css("overflow","hidden");

});

I need both preventDefault and stopPropagation to stop some other stuff happening. Can you see any errors or a way to stop this auto scrolling to the top? Thanks!

Quick Edit:

I’m also running a function which centers the box using

$(window).scroll(centerBox);

I’m unsure if this would affect scrolling in some odd way in firefox. The contents of this function are just adding CSS, so I doubt they would have any effect on it.

Another Edit:

A link to try it out. Not working in Firefox for me.
http://inserthtml.com/demo/internal-popup/

Answer by Joseph the Dreamer

first, i see nothing wrong in the script. it should be preventing the “top jump” even only with e.preventDefault(). try stripping it down to this. it should tell if this handler is causing it or not

$('[class*=popup-link]').click(function(e) {

    e.preventDefault();   //prevent the click from jumping esp on hashes
    e.stopPropagation();  //prevent from any parent click handlers that didn't prevent the jump

    //no code here for now

    return false;         //the natural way to prevent the jump
});

if this code prevents the jump, then there is something in the rest of your code that causes it especially broken scripts. check the console for errors also

Answer by Starx

e.preventDefault();
e.stopPropagation();

is equivalent to

return false;

Change them, and you will be fine

Read more

how to scroll the content inside of a fixed-height container?

Question by bobighorus

I was wondering how I can realize with jQuery the simple example in the following image.

Basically it’s a fixed-heigt container div and I wish to make the content inside of it (a list of paragraph) scrollable up/down by arrows.

I need arrows commands and no scrollbars and I wish to not use any plugin (if it’s possible), like this old dhtml example.

Consider that I don’t know the exactly height of the entire content, because it’s ajax loaded and so it can be variable.

Any help will be strongly appreciated.

Thanks in advance.

enter image description here

Answer by Starx

You are probably looking for scrollTo Plugin. But, If you want to bring native HTML scroll bars, then defining a overflow property, does the job for you.

div { overflow-y: scroll }

Updates


I made a very simple demo of how the scroller can be create with just simple jQuery.

Another demo, with the scroll limits. Perfect to be called a plugin on the making.

Update 2

What you are looking for is click and hold event, which is not available but, we can use mousedown event to build a workaround.

Check a udpated demo

Basically, the idea is to start a interval on mousedown and clear on mouseup

Read more
...

Please fill the form - I will response as fast as I can!