...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
January 5, 2011

Limit what classes can extends another class

Question by Sondre

I’m currently writing a solution that contains some abstract classes. One of these should only be extended from a few other classes as using it from other places than intended could cause a little bit of a mess. So what I want to do is limit what classes can extend this class.

So if I have a class named ImportantStuff which is abstract I want to say that only Class A and Class B are able to extend it while any other class can’t.

You might wonder why I would want to do something like this. Well the code is written for work so there will be lots of other programmers working with it later, so I wish to make it clear for those that this class is not intended to be used from any other places than it already is. Yes I know I can write it in a class description and I have, but I want to make it 100% clear by blocking it as not all programmers are good at reading comments:) Also I’m kinda curious if it can be done in a good fashion as I couldn’t find a good answer on the web.

So my question is: what is the best way to make this happen? I guess I can send a key or something that is handled in the constructor, but is there a cleaner way to do this?

Answer by Hannes

hmmm, make the constructor final (but add an init function) and check there which class is currently constrcuted, thats possbilie, but requires at least php 5.3

    abstract class bigOne{
        private $_allowedClasses = array('smallOne');

        final public function __construct() {
            if(!in_array(get_called_class(), $this->_allowedClasses)){
                throw new Exception('can not extend to Class: ' . get_called_class());
            }
            $this->init();
        }

        abstract public function init();
    }

    class smallOne extends bigOne{
        public function init(){

        }
    }

    class badOne extends bigOne{
        public function init(){

        }
    }

    $oSmallOne = new smallOne();
    $obadOne = new badOne();

Answer by Starx

I suppose you can use database to accomplish this.

put class name the list of its extendable class in a row like this

-------------------------------------------------------------------
Class       |       Extendable class
-------------------------------------------------------------------
child 1     |       parent1,parent2,parent3
---------------------------------------------------------------

Then use this data in your code to see if a certain class is extendable

May be a sample will be this

$extendableClass = explode(',',$datarow['extendable class']);
$toextendClass = "parent4";
if(in_array($toextendClass,$extendableClass)) {
    //extend
}
else { 
    //dont extend
}

you can also use plain file or xml to store the class name instead of database or storing them in multidimensional-array might also get this done….

Read more
January 4, 2011

storing multiple values of a variable in cookie and comparing

Question by John

How can i store multiple values for a variable in cookie using php, for example
$id = 1,2,4,5
Then how can i compare the stored values with a variable? for example, $new_id=4, i want to check if $new_id value exists in the stored values of $id in cookie.
Thanks for help and have a nice day.

Answer by tdammers

You can store arbitrary strings in cookie elements, so a serialized array should work. Example:

// To store:
$ids = array(1, 2, 3, 4);
setcookie('ids', serialize($ids));

// To retrieve:
$serialized = $_COOKIE['ids'];
$ids = unserialize($serialized);
// sanity check: $ids needs to be an array.
assert(is_array($ids));

// Now let's check:
if (in_array(4, $ids)) {
    // Yes, it's here.
}

A few caveats though:

  • The cookie is completely in the hands of the client, and cookie values should never be trusted. Treat them just like you would treat query string parameters or POST data.
  • Cookies offer very limited storage (IIRC, the standard gives you 4096 bytes to work with).

With these in mind, it might be a better idea to store the array in $_SESSION instead – this will give you virtually unlimited storage, and the only way for the client application to fiddle with the values is through your code.

Answer by Starx

Store array in cookie and then compare them

Read more
December 29, 2010

CSS files vs skin files

Question by just_name

Q: I want to know :

  1. Are the skin files can replace the
    CSS files?
  2. What are the advantages of
    skin files?

  3. Are those files can provide more
    brilliant things the CSS files
    can’t?

    and

  4. When to use skin files in my web
    application?

Answer by Geoff Appleford

Asp.net skins are one part of asp.net Themes. From the MSDN docs:

Themes are made up of a set of
elements: skins, cascading style
sheets (CSS), images, and other
resources. At a minimum, a theme will
contain skins. Themes are defined in
special directories in your Web site
or on your Web server.

Skins are used to define default markup for asp.net server controls:

<asp:button runat="server" BackColor="lightblue" ForeColor="black" />

whereas CSS is used to style your native HTML elements (of course asp.net server controls render as native elements so CSS can also be used to style these

Answer by Starx

CSS Files is a .css file contains style tags nothing more (for example, page.css)


But, a theme or skin file might be a set of files containing images, icons, css files, js files which can be used or integrated as sort of a plugin to change the appearance of the Application. (for example, a joomla template)

Read more
December 19, 2010

When uploading, where do I set the php.ini parameters?

Question by gAMBOOKa

In the script where the upload form is, or the script that processes the action?

Example parameters:

memory_limit

EDIT: Ok, let me clarify. First, I didn’t know the 2nd, 3rd and 4th parameter could not be changed using ini_set(). Now, I want to know where exactly I should call the ini_set() function. In the form script or the action handling script?

Here’s an example to illustrate what I’m looking for:

form.php

<form action="post.php">
  <input type="text" name="search" />
  <input type="submit" />
</form>

post.php

<?php //handle search ?>

So under which file must my ini_set() function should go?

Answer by Starx

You have to set these parameters initially from php.ini. You cannot change this from a PHP script except memory_limit, for the remaining Just search for those keys you mentioned in php.ini and change their values.

For memory_limit you can use following snippet at your processing page handling the uploads like in your case at post.php

ini_set("memory_limit","16M");

OR

You can create a .htaccess file at the root directory, if your server support mod_rewrite modules you can change them like this

php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200
Read more
December 16, 2010

How do I get a div background image to scroll?

Question by liamacheung

I want the background image of a div (with id=”library_tracks”) to be repeated in both x and y directions and to scroll with the div. For whatever reason, the background is staying fixed. I imagine the solution is pretty straight forward… any ideas?

Here’s my CSS:

#library_tracks {
    overflow-y: scroll;
    height: 400px;
    border: thin solid #DADADA;
    background-image: url('../img/track_background.png');
    background-attachment: scroll;

}

Answer by somethingkindawierd

The trick is to set the background not on the container div, but on the content div within it. Here’s a working example where the <div id="library_tracks"> div displays the scrollbar, but the <div id="content"> is the actual content that’s moved.

http://jsfiddle.net/somethingkindawierd/Cj29C/

This works because it’s the content div that moves. The library_tracks div is stationary, but everything in it scrolls.

Answer by Starx

A entire divison will scroll on only one circumstance i.e. the contains does not fit within the division area.

Whether you have small background images or big one, if the content can fit within the div we will not get the scrolling even if you set the overflow option.

Read more
December 15, 2010

Adding to browser context menu?

Question by Blender

Is it possible to add item to the default browser right button click menu?

Answer by alex

No, but you can create your own.

Look at the event oncontextmenu().

Here is some good further reading.

Answer by Starx

You can’t modify the client’s application using a web page. If this would be possible, just think about how hackers could exploit our computer.

What you can do, is define your own custom menu, while user right clicks.

Check this jquery plugin: http://abeautifulsite.net/blog/2008/09/jquery-context-menu-plugin/

Read more
December 10, 2010

How to change the height of popup box in magic zoom plus

Question by shalu

I am facing some problem not exactly the problem I want little change in this script.Actually I want to change the height of the `magic zoom plus window’ but I am not able to find the solution reason is that I am not more aware with javascript.you can see from here

http://www.magictoolbox.com/magiczoomplus/

In this example when mouse over the bike image then the selected portion of bike shown in other window I want to change the size of that window.

Please provide me any help regard this.I shall be very thankful to u.
Please reply me ASAP

Thanks

Answer by Starx

Change the options parameter

    <script type="text/javascript">
        MagicZoomPlus.options = {
            'opacity': 30,
            'background-opacity': 70,
            'show-title': 'bottom',
            'zoom-height': '100'
        };
    </script>

Source: http://www.magictoolbox.com/magiczoomplus/integration/

Read more
December 7, 2010

The most semantic way of making this container

Question by theorise

I want to make the following shape using divs and border radius, with fall back to square corners for old browsers. No images please.

I am having a bit of trouble making the bottom corner next to the title (highlighted with the red box). I don’t want a lot of unnecessary divs, it has to be as simple and semantic as possible.
alt text

<div class="container">
   <div class="header">Title</div>
   <div class="body">Content</div>
</div>

.container{width: 660px; background: #fff;}
.container .header{float: left; width: 110px; background: #333; border-radius: 5px 5px 0 0;}
.container .body{clear: left; width: 660px; background: #333; border-radius: 0 0 5px 5px;}

Any ideas?

EDIT 1:

I did it like this: http://jsfiddle.net/a93Rb/16/

<div class="container">
    <div class="header"></div>
    <div class="headerspacer">
       <div class="headercorner"></div>
    </div>
    <div class="body"></div>
</div>

.container{width: 660px; background: #fff;}
.container .header{float: left; width: 110px; height: 30px; background: #333; border-radius: 10px 10px 0 0;}  
.container .headerspacer{float: left; position: relative; width: 550px; height: 30px; background: #333;} 
    .container .headerspacer .headercorner{ position: absolute; width: 550px; height: 30px; background: #fff; border-radius: 0 0 0 10px;} 
.container .body{clear: left; width: 660px; height: 100px; background: #333; border-radius: 0 0 10px 10px;}

EDIT 2:

I am going to use this method: http://jsfiddle.net/a93Rb/13/

I also found a method of using an image which will not appear if the browser does not support rounded corners. It is much more semantic, and the whole point of using border-radius is to negate unnecessary markup. I think I will actually use this method, but I won’t accept it as an answer as I stated that I do not want images.

<div class="container">
    <div class="header"></div>
    <div class="body"></div>
</div> 

.container{width: 660px; background: #fff;}
.container .header{float: left; width: 110px; height: 30px; background: #333; border-radius: 10px 10px 0 0;}                                  
.container .header:after{content: url(http://img24.imageshack.us/img24/1112/corner0.gif); display: block; margin: 20px 0 0 110px;}
.container .body{clear: left; width: 660px; height: 100px; background: #333; border-radius: 0 0 10px 10px;}

Answer by alxx

My idea was white rounded div with dark background under bottom left corner. (I can’t get it to work myself, so it would be great to see result 🙂 )

Answer by Starx

Here is your pure CSS solution http://jsfiddle.net/Starx/a93Rb/, only compatible with FF for now. You can make it compatible for the remaining browsers.

Read more

Change Unordered list item's color on focus?

Question by Damien Joe

How can we change an unordered list’s item color when it is focused with tab or clicked with mouse using CSS ?

Edit:
Check JSFiddle

Answer by benhowdle89

   .form li input:focus
{
    background-color:yellow;
}

Check that…

If your interested, this jQuery may work, please someone step in and correct it if its wrong!

$(".form li input").click(function(){
        $(this).closest("li").css("background-color","yellow");
});

$(".form li input").blur(function(){
        $(this).closest("li").css("background-color","white");
});

Answer by Starx

Here is your pure CSS solution http://jsfiddle.net/Starx/a93Rb/, only compatible with FF for now. You can make it compatible for the remaining browsers.

Read more

Correct HTML scrolling element of main viewport (+jQuery)

Question by tillda

When I want to scroll a website by javascript code what is the corrent element to set the .scrollTop property on?

Is it window, document, document.body or html element?

Does jQuery have some trick to detect the intention to scroll the main viewport and unify the calls? It seems to me that sometimes $(window).scroolTop(value) works, but sometimes it doesn’t.

Answer by bobince

Normally, the <html> element, aka document.documentElement. However if you are using Quirks Mode, <body> (document.body) represents the viewport instead. (HTML/CSS doesn’t explicitly state/require this, but it is how all modern browsers work.)

You don’t ever want to be in Quirks Mode, but if you’re writing a script for inclusion on other people’s pages you will have to deal with it:

var viewport= document.compatMode==='BackCompat'? document.body : document.documentElement;
// do something with viewport.scrollTop

jQuery uses window.scrollTo() instead of setting the scroll properties directly. Either way is pretty much the same in practice. You could argue that using scrollTo is a bit cleaner in that it avoids relying on the viewport being represented by a particular element, but then jQuery still has to use scrollTop/scrollLeft to read the current value of the ‘other’ property anyway, so no big win. You can use pageXOffset/pageYOffset to read the current scroll position without relying on a particular element, but it’s not supported everywhere so you still have to fallback to the scrollTop method.

Sadly none of these properties/methods have been standardised in the past. CSSOM Views finally address this.

Answer by Starx

Depends on what you are scrolling, document/Window or a division

Read more
...

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