...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
October 28, 2010

Find who calls a method in PHP's class

Question by balkon_smoke

Possible Duplicate:
Find out which class called a method in another class.

Hello everyone,

I have a class and I can’t find where his object creates.
I mean, can I somehow find out who calls his constructor?

Thanks a lot,
Andrey.

Answer by Starx

use

$trace = debug_backtrace();
echo "<pre>".print_r($trace[1])."</pre>"; 
//see all the displays '1' is the referrer '0' is self
echo $callingfunction = $trace[1]['function'];
echo $callingclass = $trace[1]['class'];
Read more

jQuery remove elements by condition

Question by plutov

I have some tr elements in table:

<table>
<tr id="tr_level_1">...</tr>
<tr id="tr_level_2">...</tr>
<tr id="tr_level_3">...</tr>
<tr id="tr_level_4">...</tr>
<tr id="tr_level_5">...</tr>
</table>

In Javascript I have the next variable:

var levels = 3;

I want to delete all tr’s where number in id is more than levels. And if levels is more than number of tr’s – adding tr’s after last.

Thanls a lot.

Answer by netadictos

Working demo

Try this:

var levels = 3;
$("table tr:gt("+(levels-1)+")").remove();

I substract one because this expression (“gt”: greater than) is 0-based index.

For the second part of your question try this:

http://www.jsfiddle.net/dactivo/fADHL/

if($("table tr").length<levels){
 //the code here for less than levels
}
else
{
 $("table tr:gt("+(levels-1)+")").remove();    
}

Answer by Starx

try this

var total = $("#table").find('tr').length;
var levels = 3;
if(levels<=total) {
   for(levels=levels;levels<=total;levels++) {
       $("#tr_level_"+levels).remove();
   }
}
else {
   $("#table").append("<tr id="tr_level_"+total+1+"">..</tr>");
   // this will add the element with tr_level_6 at the end 

}
Read more
October 27, 2010

passing variables from php file to anther

Question by OdO

How to pass variables from a php file to another while it is not html inputs ,just i have a link refer to the other file and i want to pass variables or values to it

Example:

File1.php

<?php

$name='OdO';
echo "<a href='File2.php'>Go To File2</a>";

?>

File2.php

<?php

echo $name;

?>

Answer by kijin

Use sessions to store any small value that needs to persist over several requests.

File1.php:

session_start();
$_SESSION['var'] = 'foo';

File2.php:

session_start();
$var = $_SESSION['var'];  // $var becomes 'foo'

Answer by Starx

You can use URLs to pass the value too.

like

index.php?id=1&value=certain

and access it later like

$id = $_GET['id'];
$value = $_GET['value'];

However, POST might be much reliable. Sessions/Cookies and database might be used to make the values globally available.

Read more
October 26, 2010

Javascript resize text as an image

Question by Andrew Jones

I was wondering how to resize a div containing some text by setting a width and height so it would stretch to fill that area like an tag? E.g. when you set the width and height attribute of an image, it resizes the image to fill that area. I need to take an arbitrary width and height value and stretch a div containing text to those dimensions. I do not wish to send a request to a php script to build an image containing the text since it will be slow and affect the scalability of the web app. Oh, and I would preferably not like to use html5 because it is a requisite that I need compatibility with ie7+ (and firefox/chrome).

Thanks.

Answer by Starx

Ok, Here is how to do that.

http://www.jsfiddle.net/Zp8AH/

However, some factors like Font Family, Division width have to be fixed, in order for this code to work.

Read more
October 24, 2010

php session management

Question by AJCE

I am developing a system in php which has different types of users like admin, A, B, C.

I need to allow log in just one user from the type at a time. Means
1) if there is already an admin user is logged in, no other admin user can log in.
2) And the logged in credentials cannot be used on other pc at the same time (just like yahoo messenger login system).

I have tried to store session with login logout time in database table when someone login and logout.
But it creates problem when someone close the browser without logging out, the logout entry time misses.

Answer by Starx

1) if there is already an admin user is logged in, no other admin user can log in.

If you want to stop multiple logins from same system then set a session flag like $_SESSION[‘loggedin’]=true; while a successful log in occurs, then before each login attempt, check this flag and only when the flag is falseproceed with the login process.

If you want to stop multiple logins from multiple system then create a temporary table in your database to hold the status of your logged-in users and before any login attempts occur check this table to find any logged in user, if you dont find any user then only proceed, or else prompt the user

2) And the logged in credentials cannot be used on other pc at the same time (just like yahoo messenger login system).

create a temporary table in your database, which will hold the status of your all logged in users, then when a user who is already logged-in, attempts to log in from another place, check the status of that userid in your temporary table, if he is already found logged-in then either prompt him or deny him or log him off from other computer before logging him in again.

Read more
October 11, 2010

want to change color at multiple places in one go !

Question by Rahul Utb

My problem is I have couple of divs in my page. All have header of similar color. Now if I have to change the color(for example background color) of all divs, I have to make changes as many divs I have. Is it not possible to just change or say write the color code at one place (like in a variable) and the then use that variable as color value in the embedded styles to all those divs. Something like javascript entities.

Answer by Yi Jiang

If you need variables in CSS, you might want to look at CSS pre-compilers (is this the correct term?), such as Sass, which does this Server-side and eases the pains for having many different color repeated across multiple rulesets.

Otherwise, when developing, try splitting your CSS files into individual components, such as typography.css, color.css etc. to help better organise them. You’ll still want to combine them after development is complete for better performance, but doing this does help keep things neater and tidier.

Lastly, you can always define large rules like this:

#header, #footer, #nav, #sidebar {
    color: orange; /* I like orange! */
}

Which would reduce redundancy somewhat. Using Javascript for styling and presentation should only be kept as a last resort; there are always tools available to keep your CSS tidy; you only need to use them.

Answer by Starx

Jquery solution

define all the divs with a specific class like

<div class="changeable"></div>

Then use jquery to change the background

$(".change").click(function() {
    $(".changeable").css("background","#000");
});
Read more
September 2, 2010

css – use attribute selector or set class name?

Question by StoneHeart

Should we use

css: input[type='submit']{...}

or set class name for input tag?

html: <input type="submit" class="submit">
css: .submit{...}

Answer by Sarfraz

You should use the class name instead of attribute selector if you want to support IE6.

This article is interesting:

Answer by Starx

Yes, As Sarfraz said if you go for browser compatibility you should go for class name, but attribute selector provides a vast range of control over any elements.

Read more

How to animate text using JavaScript

Question by Hema

I want to implement animated text i.e. moving the text from bottom (half of the page) to the top like marquee. I don’t get any good code. Does any one know how this is implemented in JavaScript or jQuery or DHTML?

Thanks in advance

Answer by Cipi

Same code as the answer below but, it sets the TEXT location to be at half the page’s height, and then animates it to top:

//Calculate where is HALF of the page (half the window height)
var start_pos = screen.height/2;

//Set starting TOP location
$(".text").css("top", start_pos+"px");    

//Animate to the END location, which is 0px
$(".text").animate({ top:0+"px" }, 5000, function() { alert("Animation Complete"); });

​

Answer by Starx

use jquery

  $('#mydiv').animate({
    top: 0
  }, 5000, function() {
    // Animation complete.
  });

Check the demo here

Read more
August 30, 2010

Prevent floated divs from wrapping to next line

Question by user433143

Here is my site, first of all:

www.kaiserroof.com/test/index2.html

So here is my problem. You’ll notice that underneath the divider bar in the middle of the page, there are three columns, one with a form, one with text, one with links. Now, resize the window to slightly smaller, and the right div will drop down to the next line. Is there anyway to just not display that? So, they divs will adjust (i have a liquid layout) up to the point where they won’t fit, then, instead of wrapping the div down to the next line, it just won’t be displayed?

I hope this makes sense. Any help would be greatly appreciated.

Answer by jfs

You can also achieve that with CSS only.

Just assign the following CSS attributes to #row4:

#row4 {
    min-width:1202px; /* the exact value depends on the sum of the width of your 3 column boxes */
    overflow:hidden;
}

This differs slightly from your intended solution, since the right box will stay partly visible when sizing down the window and will not immediately disappear completely.

Please be aware that min-width won’t work in IE6. However, there are several ways to emulate the min-width property, if you need to support old IEs:

http://www.thecssninja.com/xhtml/ie6-min-width-solutions

Answer by Starx

Ok here is what you should do

Wrap all three floated division on a parent div, something like this

<div id="parent">
    <div class="form">......</div>
    <div class="text">......</div>
    <div class="links">.....</div>
</div>

Now to solve your problem give a fixed height to the parent div like

#parent { height:400px;clear:both; }
Read more
...

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