...

Hi! I’m Starx

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

Boolean assignment in java

Question by siik

Can you explain this assignment? What does it mean?

boolean activityExists = testIntent.resolveActivity(pm) != null;

Answer by Starx

It means to assign true if testIntent.resolveActivity(pm) does not return null otherwise assigns false.

Understandable long form of this would be

boolean activityExists;
if(testIntent.resolveActivity(pm) != null) {
    activityExists = true;
} else {
    activityExists = false;
}
Read more

using multiple image on background

Question by Diwash Regmi

I am getting problems on using multiple images in the background of my web pages. I used the following codes for it:

body{
background-image: url(images/img1.png), url(images/img2.png); }

The code I used gives me two images on background but I want to keep one of the image exactly on the center. How can it do so using CSS?

Answer by Starx

Yeah, you can do it like this

body {
    background-image: url(images/img1.png), url(images/img2.png);
    background-repeat: no-repeat, repeat-x;
    background-position: center, top left;
}

Check a demo here.

Read more

Zend Framework: Fatal error on the server

Question by Guilhem Soulas

I’m trying to put a ZF website on the Internet which works well on my local machine (WAMP).

But on the Linux server, only the main page can be properly displayed. For the other pages, I’ve got a fatal error:

Fatal error: Uncaught exception
‘Zend_Controller_Dispatcher_Exception’ with message ‘Invalid
controller specified (error)’ in
/var/www/staging/library/Zend/Controller/Dispatcher/Standard.php:248
Stack trace: #0
/var/www/staging/library/Zend/Controller/Front.php(954):
Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http),
Object(Zend_Controller_Response_Http)) #1
/var/www/staging/library/Zend/Application/Bootstrap/Bootstrap.php(97):
Zend_Controller_Front->dispatch() #2
/var/www/staging/library/Zend/Application.php(366):
Zend_Application_Bootstrap_Bootstrap->run() #3
/var/www/staging/public/index.php(26): Zend_Application->run() #4
{main} Next exception ‘Zend_Controller_Exception’ with message
‘Invalid controller specified (error)#0
/var/www/staging/library/Zend/Controller/Front.php(954):
Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http),
Object(Zend_Contr in
/var/www/staging/library/Zend/Controller/Plugin/Broker.php on line
336.

I activated the URL rewriting. I’m using modules. The index.php and application.ini are very basic, we didn’t custumize it.

I suppose that there is something wrong with the configuration… Thanks.

Answer by Starx

When deploying application from windows platform to Linux, most typical type of error that can be encountered is due to the filename cases. Linux system are very strict about file name and cases.

The error you are encountering is also probably one of these cases. Check the name of ErrorContainer.php and try to match the name you specify in your route and file system.

Read more

Ensuring Div is 100%

Question by Michael

For some reason I can’t get an image to span 100% width across the browser.

You can find my site here.

My div reads:

    <div class="home" style="background-image: url(http://payload51.cargocollective.com/1/7/237315/3336908/HomeImage_o.jpg); 
background-attachment: fixed; height: 560px; width: 100%; opacity: 1; background-position-y: 0px; background-position-x: center; background-repeat: no-repeat no-repeat; "> 
    </div>

Any help would be appreciated.

Answer by Starx

I don’t like the idea of stretching a image to fit the browser’s window. As this depends on the client’s machine on how the site will appear.

So, I will not suggest searching for such solutions. Rather:

  • Use repeating image as a background
  • Fix the dimensions of the image container. The effect you are trying to create will still look pretty cool
  • Detect the screen size and change the background as per the resolution.
Read more
August 4, 2012

Parsing JSON array of objects with jQuery

Question by Christopher Buono

What I am trying to do is parse this JSON: http://www.theunjust.com/test/container.php

And just get it to show at this point…

Here is my current code(its all jacked up)

$(document).ready(function(){
    var jsonurl = "container.php";
  $.getJSON(jsonurl, function(data) {         
    $.each(data.encompass,function(i,valu){
    var content,
    cont = '',
    tainers = valu.containers;

  $.each(tainers, function (i) {
    var tid = tainers[i].TID,
    top = tainers[i].Top,
    cat = tainers[i].Category,
    date = tainers[i].Date,
    icon = tainers[i].Icon,
    clink = tainers[i].ContainerLink,
    uid = tainers[i].UniqueID;

        cont += '<a href="' + clink + '">' + uid + '</a> ';
        $('ul').append('<li>' + tid + ' - ' + top + ' - ' + cat + ' - ' + date + ' - ' + icon + ' - ' + clink + ' - ' + uid + ' (' + cont + ')</li>');
    }); 
  }); 
}); 
});

I know that the code doesn’t make sense, this is more of a learning experience for me rather than a functioning tool. The issue may be in the JSON data itself, if that is the case, what stackoverflow web service post would you recommend?

What I eventually aim to do is run multiple web services and combine them… So that container.php loads and has all of the information that is required for a wrapping div, then entries.php loads and fills the divs that was generated by containers..

I’m currently learning jQuery and Honestly, I’m not to this level yet, but I am getting there pretty quick.

I really appreciate the help, and would like if you could point me in the direction to become a JSON parsing pro!

Answer by Starx

containers contain single node. So simply use

$.each(tainers, function (i, v) {
    var tid = v.TID,
    top = v.Top,
    cat = v.Category,
    date = v.Date,
    icon = v.Icon,
    clink = v.ContainerLink,
    uid = v.UniqueID;

    cont += '<a href="' + clink + '">' + uid + '</a> ';
    $('ul').append('<li>' + tid + ' - ' + top + ' - ' + cat + ' - ' + date + ' - ' + icon + ' - ' + clink + ' - ' + uid + ' (' + cont + ')</li>');
});
Read more
August 1, 2012

CSS: fixed position on x-axis but not y?

Question by kylex

Is there a way to fix a position on the x-axis only? So when a user scrolls up, the div tag will scroll up with it, but not side to side?

Answer by Starx

Its a simple technique using the script also. You can check a demo here too.

JQuery

$(window).scroll(function(){
    $('#header').css({
        'left': $(this).scrollLeft() + 15 
         //Why this 15, because in the CSS, we have set left 15, so as we scroll, we would want this to remain at 15px left
    });
});

CSS

#header {
    top: 15px;
    left: 15px;
    position: absolute;
}

Update Credit: @PierredeLESPINAY

As commented, to make the script support the changes in the css without having to recode them in the script. You can use the following.

var leftOffset = parseInt($("#header").css('left')); //Grab the left position left first
$(window).scroll(function(){
    $('#header').css({
        'left': $(this).scrollLeft() + leftOffset //Use it later
    });
});

Demo 🙂

Read more
July 22, 2012

php include (cannot get correct path)

Question by Sandro Dzneladze

I have a file that resides in:

/Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/sidebar-left-big.php

I have another file in sub directory:

/Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php

And in _gallerypage.php I have php include:

<?php include('../sidebar-left-big.php'); //left sidebar, category navigation and ads ?>

Error I get:

Warning: include(../sidebar-left-big.php) [function.include]: failed to open stream: No such file or directory in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php on line 9

Warning: include() [function.include]: Failed opening '../sidebar-left-big.php' for inclusion (include_path='.:') in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php on line 9

It seems to me I’m doing everything correctly.

I thought that maybe problem is that _gallerypage.php is loaded via include in another file, so ../ relative to that leads to error. But error doesn’t say anything as to where it thinks path to sidebar-left-big.php is.

Answer by Jerzy Zawadzki

use include dirname(__FILE__).'/../sidebar-left-big.php';

Answer by Starx

Yes, You are right.

When you include the _gallerypage.php from another file, it does take the path relative to itself. So, you should fix this.

The best way, might be avoid such difficulties. There are number of ways to do this. Like, One would be define a global root in a constant and include every thing, everywhere as per it.

For example:

define("BASE" , "/wordpress"); //Define a base directory

//now whenever you are including and requirng files, include them on the basis of BASE

require(BASE."/admin/.../some.php");
Read more

how to navigate from one tab to other by clicking on a hyperlink using jquery ui tabs

Question by Hardworker

Could any one help me on how to navigate from first tab to second tab by clicking a hyperlink in first tab using JQUERY UI tabs?

Answer by Shant

You can refer the jQuery UI Tabs documentation for your problem, its very well mentioned

var $tabs = $('#example').tabs(); // first tab selected

$('#my-text-link').click(function() { // bind click event to link
    $tabs.tabs('select', 2); // switch to third tab
    return false;
});

Answer by Starx

You can switch between tabs freely by following the indexes using select method.
An example:

$("#tabs").tabs('select', 1);

Attach this snippet to the click handler of the link on the content of first tabs.

Example:

For a link like this:

<a href="some.html" id="nextTab">Go to Next Tab</a>

jQuery:

$("#nextTab").click(function() {
     $("#tabs").tabs('select', 1);
});
Read more
July 20, 2012

detect cross domain redirects on ajax request

Question by Sush

We have our authentication delegated to another domain (Window Identify framework, federated authentication setup). Now, if the the session timed out before an ajax request , server redirects the request to authentication server. Since it becomes a cross domain call, ajax request is cancelled by the browser. Is there a way i can detect this in jquery/javascript ?

I inspected the status property of the xhr object which set to 0 in such case, but is it a good indicator for cancelled requests? (I am using jquery $.ajax to make ajax requests)

Answer by Starx

Actually, there isn’t any definite way to detect this, unless you define it manually.

For example: Store you domain name in a var

var domain = "http://www.domain.com";

Next, whenever you have a URL you need to check, if it belongs to same domain, you can check like this:

var url = "http://www.domain.com/page.html";
if(url.indexOf(domain) >0) {
   //Yes it belongs to same domain
}

Note: This is rather a very simple example to give you an idea

Read more
July 18, 2012

PHP echo part of a string from right to left

Question by shwebdev

I am trying to get the ID from the end of this string the 4305 after the -. The code below works from left to right and shows 150. How can i make it work from right to left to show the 4305 after the -?

  $mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
  $mystring = substr($mystring, 0, strpos($mystring, "-"));
  echo $mystring;

Updated: This does what i need but i’m sure there is a better way to write it:

  $mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
  $mystring = substr(strrev($mystring), 0, strpos(strrev($mystring), "-"));
  echo strrev($mystring);

Answer by Michael Mior

You can use strrpos to get the last hyphen in the string, and then take the rest of the string after this character.

$mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
$mystring = substr($mystring, strrpos($mystring, "-") + 1);
echo $mystring;

Answer by Starx

Most easiest way is definitely using explode.

By using explode, you can split the string into an array with each parts accessible as individual identifiers using the indexes.

Usage Example:

  $mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
  $mystring = explode("-", $mystring);
  echo $mystring[count($mystring)-1]; //Extract the last item
Read more
...

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