...

Hi! I’m Starx

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

I want to define variable in CodeIgnitor so that it can be accessed in application

Question by Musaddiq Khan

e.g array of constants so that it can be accessed in controllers, views, models.
Thanks

Answer by Starx

If you are looking for globally accessible datas, then session is more than just a storage feature.

Set the session values like this

$this->session->set_userdata('item', 'value');

And read the values like this

$this->session->userdata('item');
Read more

Understanding "javascript : … ;" inside <a href=" ">

Question by pierr

What is the meanign of javascript:; that is kept inside the href attribute in an link?

Like

<a href="javascript: ... ;">Link</a>

Answer by Starx

IF you need to pass a javascript snippet which needs to run instead of the default behavior of an element then you use this javascript: ; syntax.

For example

<a href="javascript:alert('');">Test</a> <!-- Runs on the click of the link -->

Similarly, you can combine these on other events also, like onclick, onchange etc but this is really not necessary, since you can execute the snippet, directly.

The uses of this, i have seen in years are:

  1. <a href="javascript:void(0);">Test</a>
  2. <form action="javascript:void(0);">..</form>
Read more

How to add vertical spacing between block elements, but not top and bottom

Question by rob

Say I have a bunch of P, LI, or DIV elements, with nothing between them. I want to control the vertical spacing between them, so they don’t fit so tightly. But I don’t want to add any space top and bottom, since that is handled by the parent element and I don’t need more. Is there a simple way to do this that works for all block elements?

Say I’ve got something like this :

p {
  margin: 5px 0;
  }

and then

 <div>
   <p>1</p>
   <p>2</p>
   <p>3</p>
   <p>4</p>
 </div>

But I don’t want 5px above p 1, or below p 4, since the div already has padding and I don’t want to go messing with that. I just want the 10px between p 1 and p 2, p 2 and p 3, etc.

I’m sure I could do something kludgy (and I have many times), but am looking for something cleaner that I don’t have to do a lot of special-casing for this common situation.

Answer by Starx

Use adjacent selectors

p + p { margin-top: 10px; }

Basically the concept is that, if a p comes after another p give 10px margin in between.

You usage is something similar to

p + p, li + li, div + div { 
    margin-top: 10px;
}

Answer by Starx

This can also be done using :last-child or :first-child

Here is an example:

p, li, div {
    margin-bottom: 10px;
}

p:last-child, li:last-child, div:last-child {
    margin-bottom: none;
}
Read more

Notice: Undefined index: in C:wampwwwsomygmsa.php on line 142

Question by admin

I got an error whenever I run this php code on my local web server:

<?php
@ require_once ('C:wampwwwConnectionskoneksi.php');
session_start();
if (!isset($_SESSION['id']))
{
    header("Location:index.php");
}
$sql = "SELECT * FROM pasien WHERE noreg = 1122312131";
$query = mysql_query($sql) or die(mysql_error());
$data2 = mysql_fetch_array($query);

?>

            <form id="form1" name="form1" method="post" action="#">
          <table width="804" border="0" id="inputdata" style="border-collapse:collapse">
            <tr>
              <th width="8" rowspan="3" bgcolor="#CCCCCC" scope="row">&nbsp;</th>
              <th width="104" height="50" bgcolor="#CCCCCC" scope="row"><div align="right">Kode Pasien</div></th>
              <td width="250" bgcolor="#CCCCCC"><label for="nama"></label>
                <label for="noreg"></label>
                <div align="left">
                <input name="noreg" type="text" id="noreg" size="15" maxlength="13">
                <input type="submit" name="view" id="view"  value="View">       <?php
                @ include_once ('database.php');
                $view = $_POST['view'];
                $noreg = $_POST['noreg'];
                if($view){
                    $_POST[$noreg];
                }
                ?>

This is the error:

( ! ) Notice: Undefined index: view in C:wampwwwsomygmsa.php on line 139

and

( ! ) Notice: Undefined index: noreg in C:wampwwwsomygmsa.php on line 140

Thanks for any help, will be appreciated.

-admin

Answer by Starx

You are initializing those two variables with the array item does not exists.

$view = $_POST['view'];
$noreg = $_POST['noreg'];

Clearly shows that the view and noreg are not coming from the POST method. You should check to confirm this. Use var_dump($_POST); to see the posted data.

When you are coding, you should make sure that the code you are writing will execute on all cases as much as possible. In your case, initialize the variables before or use isset to check and initialize again.

$view = isset($_POST['view']) ? $_POST['view'] : '';
$noreg = isset($_POST['noreg']) ? $_POST['noreg'] : '';

Now that it is being pointed out…. The page by default will have *no POST variables *, so you have filter such cases out

if(isset($_POST) && count($_POST)) {
    @ include_once ('database.php');
    $view = $_POST['view'];
    $noreg = $_POST['noreg'];
    if($view){
        $_POST[$noreg];
    }
}
Read more
April 24, 2012

Cut off effect with overflow:hidden

Question by Team-JoKi

I’d like to create the following in css:

I’ve a long text which should be displayed on a single line, and a hyperlink next to this text (this will later be placed in the header of the website, that’s why it needs to be a single line).

When the there is little room on the screen, I want the string to be cut off (“testtest..” – hyperlink). When the window is resized and there is more room, I want the text to be shown (as much as possible (e.g. “testtesttesttesttesttesttest.. – hyperlink”).

The problem that I’m having is that the string doesn’t want to be cut off. It always remains in its full size.

Here is a link to the JSFiddle: http://jsfiddle.net/PNGDw/1/

note: here I’m trying it with a table, I’ve also tried it with floats, but that didn’t work either.

Thanks for the help

Answer by Mark Schultheiss

You have a 5em width on the table so it will not resize. Use something like:

<style>
    *
    {
        margin: 0;
        padding: 0;
    }

    body
    {
        background: #5e8834;
    }

    div.wrapper        {
        background: #456326;
        border: 3px solid #547a2f;
        margin: 2em;
        width: 100%;
    }
    div.contents{    display:inline-block;}
    .should-cut-off 
    {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 70%;
    }
</style>
<div id='wrapper'>
    <div class="should-cut-off contents">Test - test - test - Test - test - test - Test - test - test - Test - test - test
            - Test - test - test
    </div>
    <div class='contents' style="width:15%;">
        <a href="#">[this is a link]</a>
    </div>
</div>

Answer by Starx

Configure it to display as inline-block

.should-cut-off
{
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 70%;
    display: inline-block;
}

Demo

Read more

Framework CSS "windows 95" like

Question by Tristan

We have a client that build his mockups under visual studio, and he wants that the web application looks like the same as his windows application.

My eyes are constantly bleeding when I see those mockups but, any chance someone crazy enough created a Css framework that look like windows form & buttons ?

Thanks

Beware your eyes may bleed

Answer by Starx

Here is an emulated Win95 Desktop made with CSS. That might help.

Read more

php create objects from list of class files

Question by ing. Michal Hudak

I have directory CLASSES with files in my project.

../classes/class.system.php
../classes/class.database.php
...

I pull out every class and include it to main index.php with this code:

// load classes
foreach (glob("classes/class.*.php") as $filename) {
    require_once $filename;
}

and then I create (manually write) objects for example:

$system = new System();
$database = new Database();
...

Q: How can I automatically generate object for each class from list of files in directory CLASSES without writing them?

Thank you for your answers and code.

EDIT:

My working solution:

// load classes
foreach (glob("classes/class.*.php") as $filename) {
    require_once $filename;
    $t = explode(".",$filename);
    $obj = strtolower($t[1]);
    $class = ucfirst($t[1]);
    ${$obj} = new $class();
}

Answer by ing. Michal Hudak

// load classes and create objects
foreach (glob("classes/class.*.php") as $filename) {
    require_once $filename;
    $t = explode(".",$filename);
    $obj = strtolower($t[1]);
    $class = ucfirst($t[1]);
    // create object for every class
    ${$obj} = new $class();
}

Answer by Starx

IF you follow a typical pattern, while creating those files like

class.<classname>.php

Then

foreach (glob("classes/class.*.php") as $filename) {
    require_once $filename;
    $t = explode(".",$filename);
    ${strtolower($t[1])}= new ucfirst($t[1])(); // automatically create the object        

}
Read more

html comment box cutting text without going onto new line

Question by lukey

enter image description here

when i insert a comment more than the set width (600px) the text will just cut off (see image link above) instead of going onto a new line. I am using html comment box.
the style for the comment box is:

 #HCB_comment_box div.comment {
  border-top:1px solid #fff;
  width: 600px;

  }

Answer by Starx

Use the word-wrap property

#HCB_comment_box div.comment {
   word-wrap: break-word;
}

Demo

Read more

Javascript: get children by tag type

Question by 4herpsand7derpsago

I have a situation where the server-side is producing resultant HTML of the form:

<div id="myDiv">
    <ul>
        <li class="imageElem">
            <img src="..."/>
            <div id="imgInfo">
                <p class="imgDetail">
                    Image Title #1
                </p>
            </div>
        </li>

        <!-- Etc. for many images (1 <li> per <img>) -->

    </ul>
</div>

I am trying to loop through this list and perform on-the-fly DOM manipulation with jQuery. I’m running into three bottlenecks:

  • How to obtain myDiv‘s single <ul> child that has neither an id or class (and to change the server-side code to produce one of these will be a nightmare); and
  • How to obtain each <li>‘s single <img> child; same as above for getting the server to generates id/class attributes; and
  • How to obtain each <li>‘s single <p> child’s text (e.g. “Image Title #1”)

I am trying the following (for each of those three issues respectively) to no avail:

var myDivUL = document.getElementById("myDiv").ul;
for(i = 0; i < myDivUL.length; i++)
{
    var currImageElem = myDivUL[i].img;
    var currPText = myDiv[i].div.p.text;

    // ... rest of function omitted for brevity
}

What am I doing wrong here? What doe I need to change to get the three elements without having access to id/class attributes? Thanks in advance!

Answer by Chinmaya003

You can get all things like this:

$("#myDiv").find("ul:not([id],[class])").each(function()
{
    $(this).find("li").each(function(){
       var IMG = $(this).find("img:not([id],[class])");
       var PText = $(this).find("p.imgDetail").text();
    });
})

Answer by Starx

You can do this like

$("ul:not([id], [class])").each(function() {
      var img = $(this).find("img");
      var p = $(this).find("p.imgDetail");
});
Read more

val() on <option> returns text instead of value in ie7

Question by Johan

$(this).find('select option:selected').each(function (i, val) {

    var selectedValue = $(val).val(); //this returns the text from the option instead of the value
});

Is this a known issue in ie7? I didnt find any info about it? Possible workarounds? Thanks

Answer by SiGanteng

According to the docs at http://api.jquery.com/val/

.val() is only applicable to

The .val() method is primarily used to get the values of form elements
such as input, select and textarea. In the case of <select
multiple="multiple">
elements, the .val() method returns an array
containing each selected option; if no option is selected, it returns
null.

To achieve what you want, you could just iterate over the select and call .val() on it, your original code effectively is calling val() on option, not the actual select element, which is why it doesn’t really work.

$(this).find('select').each(function (i, val) {

    var selectedValue = $(this).val(); 
});

val() has an added bonus of having the capability to return an array of values in case of multiple select: (emphasize mine)

The .val() method is primarily used to get the values of form elements
such as input, select and textarea. In the case of <select
multiple="multiple">
elements, the .val() method returns an array
containing each selected option
; if no option is selected, it returns
null.

Answer by Starx

It is an expected behavior, since you asking for the value of the option and not the element itself.

Do not overcomplicate things.

$(this).find('select').each(function() {
   var val = $(this).val();
});
Read more
...

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