PHP Code
This script resize an Image into two 60px and 25px. Take a look at $newwidth you have to modify size values.
Tag: FAQs Help and Tutorials
Auto Load More Data On Page Scroll using Jquery and PHP
How to import large sql files into mysql using phpmyadmin wamp server
Stop all services in wamp.
Then make changes to php.ini
post_max_size = 750M
upload_max_filesize = 750M
max_execution_time = 5000
max_input_time = 5000
memory_limit = 1000M
max_allowed_packet = 200M (in mysql my.ini file)
Restart all services and it should be okay,
Now XXXMb file upload very quickly.
Generating Tag cloud with PHP and MySQL
The basic idea this time is to present a way to form a tag cloud from user input text and text entered in the past. A tag cloud is a visual depiction of user-generated tags, or simply the word content of a site, used typically to describe the content of web sites.
for this we will create an HTML form that will accept user text & also allow user to see tag cloud generated from mysql database which contains the text entered in the past.
<!--?php echo
' php” name=”gen_tag_db”>’; echo
' Input your text here:<textarea name=”tag_input” rows=”20″ cols=”80″>
echo '<input type="submit" name="submit">' ; echo '</form>' ; ?> <br /> <h3>OR</h3> <br /> <p>see the current tag cloud here</p> <!--?php echo
' php”>’; echo '<input type="submit" value="show current tag cloud" >' ; echo '</form>' ; ?> |
The entered text will be tokenized into single words with php function strtok(), each of which will have its frequency counted and the pair will go into an array. This array will then be stored into a mysql database, we can optionally keep a coloumn in the mysql database table to store links if any for future expansion of this project.
1) tag_id —- int,primary key,auto increament
2) keyword — varchar(20),unique
3) weight — int
4) link — varchar(256).
Next make a php file and name it tag_cloud_gen.php . The php code written in following lines just make an array ‘$words’ which has keyword in lower case & its frequency association from input text. The pairs from the array are then feed into the mysql database ‘tagcloud_db’ which has a table callet ‘tags’ whose columns are listed above. On encountering error the mysql _errrno() returns error number. While feeding the word array to the tags table duplication may occour, because of the previous entries, so we check whether this is the case by comparing the number returned to ’1062′ which indicates duplicate field present in the table (the keyword coloumn of table has unique constraint). On encountering this we simply update the mysql database to include the count of this input word/tag too.
<!--?php /////////////////////////////////////////////////////////////////////////////////////////////////////// /** * this function will update the mysql database table to reflect the new count of the keyword * i.e. the sum of current count in the mysql database &amp; current count in the input. */ function update_database_entry( $connection , $table , $keyword , $weight ){ $string = $_POST [ 'tag_input' ]; $connection = mysql_connect( "localhost" , "root" , "" ); /** * now comes the main part of generating the tag cloud * we would use a css styling for deciding the size of the tag according to its weight, * both of which would be fetched from mysql database. */ $query = "select * from `tagcloud_db`.`tags` where keyword like '%$keyword%'" ; $resultset =mysql_query( $query , $connection ); if (! $resultset ){ die ( 'Invalid query: ' . mysql_error()); } else { while ( $row =mysql_fetch_array( $resultset )){ $query = "UPDATE `tagcloud_db`.`tags` SET weight=" .( $row [2]+ $weight ). " where tag_id=" . $row [0]. ";" ; mysql_query( $query , $connection ); } } } ?> <?php /* * get the input string from the post and then tokenize it to get each word, save the words in an array * in case the word is repeated add '1' to the existing words counter */ $count =0; $tok = strtok ( $string , " \t,;.\'\"!&-`\n\r" ); //considering line-return,line-feed,white space,comma,ampersand,tab,etc... as word separator if ( strlen ( $tok )>0) $tok = strtolower ( $tok ); $words = array (); $words [ $tok ]=1; while ( $tok !== false) { echo "Word=$tok<br />" ; $tok = strtok ( " \t,;.\'\"!&-`\n\r" ); if ( strlen ( $tok )>0) { $tok = strtolower ( $tok ); if ( $words [ $tok ]>=1){ $words [ $tok ]= $words [ $tok ] + 1; } else { $words [ $tok ]=1; } } } print_r( $words ); echo '<br /><br />' ; /** * now enter the above array of word and corresponding count values into the database table * in case the keyword already exist in the table then update the database table using the function 'update_database_entry(...)' */ $table = "tagcloud_db" ; mysql_select_db( $table , $connection ); foreach ( $words as $keyword => $weight ){ $query = "INSERT INTO `tagcloud_db`.`tags` (keyword,weight,link) values ('" . $keyword . "'," . $weight . ",'NA')" ; if (!mysql_query( $query , $connection )){ if (mysql_errno( $connection )==1062){ update_database_entry( $connection , $table , $keyword , $weight ); } } } mysql_close( $connection ); ?> |
Make another file and name it style.css . Put the following code in it.
HTML, BODY { padding: 0; border: 0px none; font-family: Verdana; font-weight: none; } .tags_div { padding: 3px; border: 1px solid #A8A8C3; background-color: white; width: 500px; -moz-border-radius: 5px; } H1 { font-size: 16px; font-weight: none; } A:link { color: #676F9D; text-decoration: none; } A:hover { text-decoration: none; background-color: #4F5AA1; color: white; } |
This will make our tag cloud look pretty, save it as style.css .
again make a new php file and name it show_tag_cloud.php .
In the php code that follows we connect to mysql database, fetch back all the tags, its weight and link.
Then it calculates the size for each tag using its weight & minimum assumed size for tags, it also associates each tag the link retrieved from the database or with a google link if no link was there i.e. ‘NA’
<?php $connection = mysql_connect( "localhost" , "root" , "" ); $table = "tagcloud_db" ; $words = array (); $words_link = array (); mysql_select_db( $table , $connection ); $query = "SELECT keyword,weight,link FROM `tagcloud_db`.`tags`;" ; if ( $resultset =mysql_query( $query , $connection )){ while ( $row =mysql_fetch_row( $resultset )){ $words [ $row [0]]= $row [1]; $words_link [ $row [0]]= $row [2]; } } // Incresing this number will make the words bigger; Decreasing will do reverse $factor = 0.5; // Smallest font size possible $starting_font_size = 12; // Tag Separator $tag_separator = ' ' ; $max_count = array_sum ( $words ); ?> <!--DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <TITLE> Tag Cloud Generator </TITLE> <META NAME= "Keywords" CONTENT= "tag, cloud, php, mysql" > <META NAME= "Description" CONTENT= "A Tag Cloud using php and mysql" > <LINK REL= "stylesheet" HREF= "style.css" TYPE= "text/css" > </HEAD> <BODY> Tag Cloud using php
<?php foreach ( $words as $tag => $weight ) { $x = round (( $weight * 100) / $max_count ) * $factor ; $font_size = $starting_font_size + $x . 'px' ; if ( $words_link [ $tag ]== 'NA' ) echo "<span style='font-size: " . $font_size . "; color: #676F9D;'> . $tag . "&meta='>" . $tag . "</a></span>" . $tag_separator ; else echo "<span style='font-size: " . $font_size . "; color: #676F9D;'> . $words_link [ $tag ]. "/'>" . $tag . "</a></span>" . $tag_separator ; } ?> </div></center> </BODY> </HTML> |
now put them all in your webserver’s root directory and watch the results. Each query will give you new results over time as the database grows.
a sample output from my tag cloug looks like this:
This tag cloud was generated using the following data:
tag_id;keyword;weight;link
“1″;”vimal”;”7″;”www.zeeshanakhter.com”
“2″;”scet”;”5″;”NA”
“3″;”engg”;”2″;”NA”
“4″;”0″;”1″;”NA”
“7″;”google”;”5″;”NA”
“8″;”cool”;”2″;”NA”
“9″;”orkut”;”3″;”NA”
Convert String into java.sql.Date and java.util.Date
Last nite i am struck in putting date in the DB and because the date is in string and in DB its Sql date type then now how i convert the string into java.sql.date formate look below……………..
Date dateVar;
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy/MM/dd”);
String DOB=”2012/07/18″;
java.util.Date dt = sdf.parse(DOB);
dateVar = new java.sql.Date(dt.getTime());
Solved How do I get data from Excel file in java?
Hi last evening i am worry about the thing that i delete data from Cell of the Excel file but when i retrieving data and displaying it was showing me that cell from which i delete data as “null” and that is the Problem for Me …….. Now for that I solve it like that ………
In this example we try to obtain the Excel’s cell data type so that we can read the value using the right method. The data to be read is contained in a file named celltype.xls
. The below matrix depict how to file is look like.
| COL ROW | 0 1 2 3 4 ----|------------------------- 0 | 1 2 A B TRUE 1 | FALSE X Y Z 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package org.kodejava.example.poi; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Iterator; public class ObtainingCellType { public static void main(String[] args) throws Exception { String filename = "..\\celltype.xls" ; FileInputStream fis = null ; try { fis = new FileInputStream(filename); HSSFWorkbook workbook = new HSSFWorkbook(fis); HSSFSheet sheet = workbook.getSheetAt( 0 ); Iterator rows = sheet.rowIterator(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); Iterator cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); int type = cell.getCellType(); if (type == HSSFCell.CELL_TYPE_STRING) { System.out.println( "[" + cell.getRowIndex() + ", " + cell.getColumnIndex() + "] = STRING; Value = " + cell.getRichStringCellValue().toString()); } else if (type == HSSFCell.CELL_TYPE_NUMERIC) { System.out.println( "[" + cell.getRowIndex() + ", " + cell.getColumnIndex() + "] = NUMERIC; Value = " + cell.getNumericCellValue()); } else if (type == HSSFCell.CELL_TYPE_BOOLEAN) { System.out.println( "[" + cell.getRowIndex() + ", " + cell.getColumnIndex() + "] = BOOLEAN; Value = " + cell.getBooleanCellValue()); } else if (type == HSSFCell.CELL_TYPE_BLANK) { System.out.println( "[" + cell.getRowIndex() + ", " + cell.getColumnIndex() + "] = BLANK CELL" ); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fis != null ) { fis.close(); } } } } |
Our program iterates the Excel file rows and cells and produce the following output:
[0, 0] = NUMERIC; Value = 1.0 [0, 1] = NUMERIC; Value = 2.0 [0, 2] = STRING; Value = A [0, 3] = STRING; Value = B [0, 4] = BOOLEAN; Value = true [1, 0] = BOOLEAN; Value = false [1, 1] = STRING; Value = X [1, 2] = STRING; Value = Y [1, 3] = STRING; Value = Z [1, 4] = NUMERIC; Value = 10.0
Related articles
- How to Import Data in Excel (tech-faq.com)
- C Programming – Boolean type (persoack.wordpress.com)
- Liquibase column data types mess (stackoverflow.com)
Gson Streaming to read and write JSON
Since Gson version 1.6, two new classes – JsonReader
and JsonWriter
, are introduce to provide streaming processing on JSON data. Read this Gson streaming documentation to understand what are the benefits of using it.
Here we show you two full examples of using following Gson streaming APIs to read and write JSON data.
JsonWriter
– Streaming write to JSON.JsonReader
– Streaming read from JSON.
Gson streaming processing is fast, but difficult to code, because you need to handle each and every detail of processing JSON data.
1. JsonWriter
In this example, you use “JsonWriter
” to write JSON data into a file name “file.json“. See comments for self-expalantory.
import java.io.FileWriter; import java.io.IOException; import com.google.gson.stream.JsonWriter; public class GsonStreamExample { public static void main(String[] args) { JsonWriter writer; try { writer = new JsonWriter(new FileWriter("c:\\user.json")); writer.beginObject(); // { writer.name("name").value("mkyong"); // "name" : "mkyong" writer.name("age").value(29); // "age" : 29 writer.name("messages"); // "messages" : writer.beginArray(); // [ writer.value("msg 1"); // "msg 1" writer.value("msg 2"); // "msg 2" writer.value("msg 3"); // "msg 3" writer.endArray(); // ] writer.endObject(); // } writer.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } }
As result, following new file named “file.json” is created :
{ "name":"mkyong", "age":29, "messages":["msg 1","msg 2","msg 3"] }
2. JsonReader
Example to use “JsonReader
” to parse or read above file “file.json”.
In streaming mode, every JSON “data” is consider as an individual token. When you use
JsonReader
to process it, each tokens will be processed sequential.For example,
{ "url":"www.mkyong.com" }
Token 1 = “{”
Token 2 = “url”
Token 3 = “www.mkyong.com”
Token 4 = “}”
As result, you need to keep calling “next****” method to move to next token manually.
See full example.
package com.mkyong.core; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import com.google.gson.stream.JsonReader; public class GsonStreamExample { public static void main(String[] args) { try { JsonReader reader = new JsonReader(new FileReader("c:\\user.json")); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("name")) { System.out.println(reader.nextString()); } else if (name.equals("age")) { System.out.println(reader.nextInt()); } else if (name.equals("message")) { // read array reader.beginArray(); while (reader.hasNext()) { System.out.println(reader.nextString()); } reader.endArray(); } else { reader.skipValue(); //avoid some unhandle events } } reader.endObject(); reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Output
mkyong 29 msg 1 msg 2 msg 3
Related articles
- Gson deserialize Collections (stackoverflow.com)
- How do I configure a Jackson ObjectMapper to show only whitelist properties? (stackoverflow.com)
- Building RESTful Web Services Using ATG (ecomwriter.com)
- Spain decides (joyeurovision.com)
- Android native app linked to a webservice, login (stackoverflow.com)
- Spain: Song Selection Tonight (eurovisiontimes.wordpress.com)
Java 7 awesome Features
Whenever, I am crawl through the web i notice the word java 7.Then,I really found out java 7 is today’s hot topic. So i googled it for know something about java 7.At that time i found out some thing interested and useful features in java 7.Thats push me here to share something to you all folks.Lets get into the topic.
The project name of java 7 development is Project Coin.I have listed some of the features below.
Null-safe Method invocation:
This is the greatest feature which is going to added in Java 7.NullPoniterException is one of the most common exception encountered in Java programming.
When I searched “NullPointerException” in Google, it gave about 5,570,000 results! This proves how pervasive the exception is and how much effort a developer has to put in writing java code free from null pointer exception.
Suppose if java has a feature to keep us in safe zone from null pointer exception, how is it? It happens.
Suppose we have a method to fetch postal code of a person’s address:
Now check the above syntax. We have done lots of if (null! = object) checks to avoid NullPointerException.
1
public String getPostcode(Person person)
2
{
3
return
person?.getAddress()?.getPostcode();
4
}
Null-ignore invocation is concerned with dealing with possible null values in calling one or especially a chain of methods. Check the syntax?. while calling method on an object. This is Null-safe operator in Java 7. Thus, you can avoid lots of if (null!= object) checks in Java 7.
Strings in Switch Statements:
Whenever i am working with switch-case i felt, if the case allowed the string as a case variable we feel so comfort on that. Because the switch case allows only the primitive data types as variable such as integer, char etc.
Whenever i working with the methods that returns status of the operations like succesful, failed like that. At that time i need to convert that in to constants. Then only i can move on switch statements.
But java 7 offers the Strings as case variables in Switch Statements, So we are free from the conversion process.
Multi-Exception Catch:
Another awesome update is Multi-Exception Catch, that means a single catch can handle multiple exceptions. The syntax is
1
try
2
{
3
block of statments
4
}
5
catch
(Exception1|Exception2|Exception3...)
6
{
7
block of statements.
8
}
It save lot of spaces in the code.
Bracket Notation for Collections:
This feature refers to the ability to reference a particular item in a Collection with square brackets similar to how Arrays are accessed. Collection is one of the key concepts in java.
But when i was a student, I feel so discomfort with collections because of its strutcture.Thats some thing different from basic things in java.
But now its also be like a simple things like array etc. For ex, a Collection class we might consider something similar to arrays.
Instead of:
1
Collection<String> c =
new
ArrayList();
2
c.add(“one”);
3
c.add(“two”);
4
c.add(“three”);
5
Use:
6
Collection<String> c =
new
ArrayList {“one”, “two”, “three” };
Thats all folks.If any important updates are missing don’t hesitate to add that as comments
Related articles
- Coder’s Delight- Java 7 (clean-clouds.com)
- Project Coin: “Small” Java 7 Tweaks, Big Dev Gains (devx.com)
- Java Version Market Share: January 2012 (jelastic.com)
- Garbage collection with Automatic Resource Management in Java 7 (javawithswaranga.blogspot.com)
- Null Pointer Exception occuring during graph generation (stackoverflow.com)
- Java 7 penetration gets to 10% of new server apps (jelastic.com)
- Eclipse Announces Full Support for Java 7 (prweb.com)
- NullPointerException when calling a private final variable in a method (stackoverflow.com)
- fatal exception: main caused by java.lang.NullPointerException menu button (stackoverflow.com)
- Null pointer when deploying WAR(s) (stackoverflow.com)
- BlackBerry Java SDK v7.0 – Now in Beta! (blogs.blackberry.com)
- The Java EE 7 Platform: Developing for the Cloud at Cloud Expo New York (java.sys-con.com)
Whenever, I am crawl through the web i notice the word java 7.Then,I really found out java 7 is today’s hot topic. So i googled it for know something about java 7.At that time i found out some thing interested and useful features in java 7.Thats push me here to share something to you all folks.Lets get into the topic.
The project name of java 7 development is Project Coin.I have listed some of the features below.
Null-safe Method invocation:
This is the greatest feature which is going to added in Java 7.NullPoniterException is one of the most common exception encountered in Java programming.
When I searched “NullPointerException” in Google, it gave about 5,570,000 results! This proves how pervasive the exception is and how much effort a developer has to put in writing java code free from null pointer exception.
Suppose if java has a feature to keep us in…
View original post 320 more words
How do I pass JavaScript values to Scriptlet in JSP?
I can provide two ways, a.jsp:
2)b.jsp:
|
Multiple Small Useful Scripts Enjoy……….
Copy this in Notepad and save as html ,,,,,,,,,,,,,,,,
- http://www.facebook.com/plugins/like.php?href=http://www.smartpassiveincome.com/facebook-landing-page-html/&layout=box_count&show_faces=false&width=50&action=like&colorscheme=light&height=65
- http://platform.twitter.com/widgets/tweet_button.html#_=1322337260939&count=vertical&id=twitter_tweet_button_0&lang=en&original_referer=http%3A%2F%2Fwww.smartpassiveincome.com%2Ffacebook-landing-page-html%2F&text=How%20to%20Create%20a%20Facebook%20Landing%20Page%20%28HTML%20%2F%20iFrame%20Edition%29%20%7C%20The%20Smart%20Passive%20Income%20Blog&url=http%3A%2F%2Fwww.smartpassiveincome.com%2Ffacebook-landing-page-html%2F&via=patflynn
- http://bufferapp.com/button/?url=http%3A%2F%2Fwww.smartpassiveincome.com%2Ffacebook-landing-page-html%2F&text=How%20to%20Create%20a%20Facebook%20Landing%20Page%20%28HTML%20%2F%20iFrame%20Edition%29%20%7C%20The%20Smart%20Passive%20Income%20Blog&via=patflynn&count=vertical&source=button
Please, do not remove or modify this code free counter
Please do not change this code for a perfect fonctionality of your counter
promotion and affiliation
http://www.xatech.com/web_gear/chat/chat.swf
Get your own Chat Box! Go Large!