Archive

Posts Tagged ‘function’

Twitter like loading (Load More)

December 3rd, 2009 Jigish Thakar No comments

twitter t 300x249 Twitter like loading (Load More)

basically this is the most simplest way to load data in twitter way (Load more). for this i have not used jquery, mootools or any other fancy javascript framework. to use this you need very basic knowledge of javascript along with little ajax & php that’s it, so here we start

step 1: you html base

again i have kept this very simple.. just one div where you want to show your twitts. now heart of this code will be your javascript functions here we will use 2 javascript functions one to send request (AJAX) and another one to load your resulting twitts… also i have used 1 javascript variables to trace my twitts (pages)


i.e.

var page = 1;

lets start with first function

function getData(){

	document.getElementById("div-Loading").className = "show";
	document.getElementById("div-button").className = "hide";
	remoteCall("php/ajax.php","page="+page,"loadData");
}

below two lines are just hide the load more button and show loading image.

	document.getElementById("div-Loading").className = "show";
	document.getElementById("div-button").className = "hide";

and this are simple Stylesheet classes to show and hide the content. Read more…

PHP: Generate Slug

November 4th, 2009 Jigish Thakar 4 comments
php code 300x193 PHP: Generate Slug

slug is used to represent URL’s in a friendly way. for example if we have URL

http://www.technoreaders.com/?p=1484

can be converted into

http://www.technoreaders.com/2009/11/04/php-generate-slug/

but for this you need to convert title of your page to to friendly URL. So you can use below function to generate slug of the title of your page.

function generate_slug($url)
{
$slug = strtolower($url);
$slug = preg_replace("/[^a-z0-9\s-]/", "", $slug);
$slug = trim(preg_replace("/\s+/", " ", $slug));
$slug = trim(substr($slug, 0, 45));
$slug = preg_replace("/\s/", "-", $slug);
return $slug;
}

and now to know how use this slug click here

Spring Source

March 4th, 2009 Jigish Thakar No comments

Spring is a layered Java/J2EE application platform, based on code published in Expert One-on-One J2EE Design and Development by Rod Johnson (Wrox, 2002).

Spring includes:

  • The most complete lightweight container, providing centralized, automated configuration and wiring of your application objects. The container is non-invasive, capable of assembling a complex system from a set of loosely-coupled components (POJOs) in a consistent and transparent fashion. The container brings agility and leverage, and improves application testability and scalability by allowing software components to be first developed and tested in isolation, then scaled up for deployment in any environment (J2SE or J2EE).
  • A common abstraction layer for transaction management, allowing for pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Generic strategies for JTA and a single JDBC DataSource are included. In contrast to plain JTA or EJB CMT, Spring’s transaction support is not tied to J2EE environments.
  • A JDBC abstraction layer that offers a meaningful exception hierarchy (no more pulling vendor codes out of SQLException), simplifies error handling, and greatly reduces the amount of code you’ll need to write. You’ll never need to write another finally block to use JDBC again. The JDBC-oriented exceptions comply to Spring’s generic DAO exception hierarchy.
  • Integration with Toplink, Hibernate, JDO, and iBATIS SQL Maps: in terms of resource holders, DAO implementation support, and transaction strategies. First-class Hibernate support with lots of IoC convenience features, addressing many typical Hibernate integration issues. All of these comply to Spring’s generic transaction and DAO exception hierarchies.
  • AOP functionality, fully integrated into Spring configuration management. You can AOP-enable any object managed by Spring, adding aspects such as declarative transaction management. With Spring, you can have declarative transaction management without EJB… even without JTA, if you’re using a single database in Tomcat or another web container without JTA support.
  • A flexible MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. Note that a Spring middle tier can easily be combined with a web tier based on any other web MVC framework, like Struts, WebWork, or Tapestry.

You can use all of Spring’s functionality in any J2EE server, and most of it also in non-managed environments. A central focus of Spring is to allow for reusable business and data access objects that are not tied to specific J2EE services. Such objects can be reused across J2EE environments (web or EJB), standalone applications, test environments, etc without any hassle.

Spring’s layered architecture gives you a lot of flexibility. All its functionality builds on lower levels. So you can e.g. use the JavaBeans configuration management without using the MVC framework or AOP support. But if you use the web MVC framework or AOP support, you’ll find they build on the core Spring configuration, so you can apply your knowledge about it immediately.

for more information click here

Dropdown with the file names

October 24th, 2008 sagar 3 comments

hii guys today i came across a problem wherein i had to fill in a dropdown menu with all the file names in a particular folder. and this is wat i hav got from a free source and believe me guys this is as simple as you can imagine.. Read more…

How to create bar graph with dynamic scaling

October 18th, 2008 jigish No comments

Hi everyone, this tutorial will help you in creating a bar graph from PHP with the ability to adjust the scale depending upon the values provided. The technique used is smart enough to handle the number and range of values. A preview of the graph is shown below

Graph

All you need to understand this tutorial is the knowledge of following PHP image functions

* imagecreate
* imagecolorallocate
* imagefilledrectangle
* imageline
* imagestring
* imagepng

in addition the following PHP functions are also used and offcourse some mathematics as well

* count
* intval
* list
* each
* header

If you have a good understanding of all these function, its good to go otherwise you should click on function which is new to you to consult the documentation and get back after learning all these functions

Lets get started

First of all declare an array of values for the graph. Declare these values in the form of associative array (i.e key and value pairs). Keys will be shown at the bottom as the graph legend and the values will be used to draw bars.

$values=array(
"Jan" => 110,
"Feb" => 130,
"Mar" => 215,
"Apr" => 81,
"May" => 310,
"Jun" => 110,
"Jul" => 190,
"Aug" => 175,
"Sep" => 390,
"Oct" => 286,
"Nov" => 150,
"Dec" => 196
);

Now define the size of image, i have used an image of size 600×400 for this tutorial.

$img_width=600;
$img_height=400;

The graph we are going to create has a border around it, i have declared a variable $margins to create that border around the four sides.

$margins=20;

Now find the size of graph by subtracting the size of borders.

$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2;

Create an image of the size defined above

$img=imagecreate($img_width,$img_height);

Define the width of bar. Gap between the bars will depend upon the width and number of bars and the gaps will be one more than the total number of bars as there is gap on the right and left of the graph. You can see in our example, we have 12 bars but 13 gaps, thats why you see ($total_bars+1) in the denominator.

$bar_width=20;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);

Define colors to be used in the graph

$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,220,220,220);

Create a border around the graph by filling in rectangle

imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);

Now the maximum value is required to adjust the scale. Ratio is calculated by dividing graph height by maximum graph value. Each value will be multiplied with ratio, so that no bar goes beyond the graph height.

$max_value=max($values);
$ratio= $graph_height/$max_value;

Drawing horizontal lines is optional, note that the margin variable is subtracted from image height so that first line is positioned inside the graph area (Discarding the margins). If you have trouble understanding this code, use a paper and pencil to manually find the values of variable at each repitition of the loop

$horizontal_lines=20;
$horizontal_gap=$graph_height/$horizontal_lines;
for($i=1;$i<=$horizontal_lines;$i++){
$y=$img_height - $margins - $horizontal_gap * $i ;
imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
$v=intval($horizontal_gap * $i /$ratio);
imagestring($img,0,5,$y-5,$v,$bar_color);
}

Here comes the most crucial part of our graph, drawing the bars. Each of the 8 lines in the for loop are individually explained below

1. Extract key and value pair from the current pointer position, each iteration of loop moves the internal pointer of array to the next entry
2. The x1 value (i.e. left) of each bar gets and increment by $gap+$bar_width with each iteration of loop
3. The x2 value (i.e. right) is calculated by adding bar width with x1
4. y1 is the top of each bar. ratio is multiplied with individual values to mare sure that bars remain inside the graph boundries.
5. y2 (i.e bottom) is fix for all bars. Can also be placed outside the loop
6. Draw the graph with calculated left, top, right and bottom positions
7. The numeric value of each bar is shown at the top. Some plus or minus will be required to center align the displayed value with the bar
8. Display the legend i.e. Month names

for($i=0;$i< $total_bars; $i++){
list($key,$value)=each($values);
$x1= $margins + $gap + $i * ($gap+$bar_width) ;
$x2= $x1 + $bar_width;
$y1=$margins +$graph_height- intval($value * $ratio) ;
$y2=$img_height-$margins;
imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);
}

Show the graph as a png image

header("Content-type:image/png");
imagepng($img);

The whole script is givenbelow, just copy and enjoy your own php graphs

 110,
"Feb" => 130,
"Mar" => 215,
"Apr" => 81,
"May" => 310,
"Jun" => 110,
"Jul" => 190,
"Aug" => 175,
"Sep" => 390,
"Oct" => 286,
"Nov" => 150,
"Dec" => 196
);

$img_width=450;
$img_height=300;
$margins=20;

# —- Find the size of graph by substracting the size of borders
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2;
$img=imagecreate($img_width,$img_height);

$bar_width=20;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);

# ——- Define Colors —————-
$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,220,220,220);

# —— Create the border around the graph ——

imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);

# ——- Max value is required to adjust the scale ——-
$max_value=max($values);
$ratio= $graph_height/$max_value;

# ——– Create scale and draw horizontal lines ——–
$horizontal_lines=20;
$horizontal_gap=$graph_height/$horizontal_lines;

for($i=1;$i<=$horizontal_lines;$i++){
$y=$img_height - $margins - $horizontal_gap * $i ;
imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
$v=intval($horizontal_gap * $i /$ratio);
imagestring($img,0,5,$y-5,$v,$bar_color);

}

# ———– Draw the bars here ——
for($i=0;$i< $total_bars; $i++){
# —— Extract key and value pair from the current pointer position
list($key,$value)=each($values);
$x1= $margins + $gap + $i * ($gap+$bar_width) ;
$x2= $x1 + $bar_width;
$y1=$margins +$graph_height- intval($value * $ratio) ;
$y2=$img_height-$margins;
imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);
imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}
header("Content-type:image/png");
imagepng($img);
$_REQUEST['asdfad']=234234;

?>