Thursday, October 21, 2010

how to create static html page from a database table using php

In this post we are creating a html file or pages which is static page in specific loacation of folder from a databse table using php. In this program we are also creating folders in specific location maintain url.
This table contains all the data for the "static" pages of the site.

Note: Please remove all "\" from bellow code, I have use it because blogger does not show html tags.

Step 1. Create a table as bellow.

CREATE TABLE `template` (
  `id` bigint(200) NOT NULL auto_increment,
  `name` varchar(200) NOT NULL,
  `address` text NOT NULL,
  `city` varchar(200) NOT NULL,
  `country` varchar(200) NOT NULL,
  `companyname` varchar(200) NOT NULL,
  `empid` varchar(200) NOT NULL,
  PRIMARY KEY  (`id`)
);

Step 2. Copy bellow code and create a php file name refresh.html.

<\?php
ob_start();

/**************** DataBase Conectivity ************/
$connect = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test");

/************** Create Directory(Folder)*************/
$query_dir = "select * from template";
$ressel_dir = mysql_query($query_dir);
while($data_dir = mysql_fetch_array($ressel_dir))
{
            $datadir = $data_dir['name'];
            $datapage = $data_dir['id'];
           
            /**************Include Pages**************/
            include ("template.php");
            $content = ob_get_contents();
            ob_end_clean();

            $dir_name = strtolower(str_replace(' ', '-', $datadir));
            $dir_page = strtolower(str_replace(' ', '-', $datapage));

            @mkdir($dir_name, 0777, true);

            /*************** Create File Path *****************/
            $filename = $dir_name."/".$dir_page.".html";

            /************** Write contents on file ****************/
            $fp = fopen($filename, "w") or die("Could't open $filename");

            fwrite($fp, $content);
            fclose($fp);
}

echo "All File written Success Full<\br />";
echo "Congratulation HTML File has been created";

?>

Step 3. Copy bellow code and create a php file name template.php

$query= "select * from template where id='".$datapage."'";
$res = mysql_query($query) or die(mysql_error());
$data = mysql_fetch_assoc($res);
?>
<\html>
<\head>
            <\title>creating static html page from a database table using php<\/title>
<\/head>
<\body>
<\table border="0" cellpadding="0" cellspacing="0" width="100%">
            <\tr>
                        <\td><\b>Name<\/b><\/td>
                        <\td><\b>Address<\/b><\/td>
                        <\td><\b>City<\/b><\/td>
                        <\td><\b>Country<\/b><\/td>
                        <\td><\b>Company Name<\/b><\/td>
                        <\td><\b>Employee ID<\/b><\/td>
            <\/tr>
            <\tr>
                        <\td><\?=$data['name']?><\/td>
                        <\td><\?=$data['address']?><\/td>
                        <\td><\?=$data['city']?><\/td>
                        <\td><\?=$data['country']?><\/td>
                        <\td><\?=$data['companyname']?><\/td>
                        <\td><\?=$data['empid']?><\/td>
            <\/tr>
<\/table>
<\/body>
<\/html>

Monday, August 23, 2010

how to select limited characters from a table column in mysql

In MySQL you can draw on the LEFT() thread function to choose a fix digit of qualities as of the left of a specific data column which permits me to obtain an appearance at the primary little utterances in comment to estimate. In this situation I am fetching 100 characters.

<\?php
      $query = "select id, left(content, 100), title from tablename";
      $res = mysql_query($query);
      while($data = mysql_fetch_array($res))
      {
             echo $data['id'];
             echo $data['left(content, 100)'];
             echo $data['title'];
             echo "<\br>";
      }
?>

Saturday, July 10, 2010

how to search string from text file in php

search string from text file in php or find string from text file in php
Note: In this file we store ( write ) all string ( value ) in text ( txt ) file. And searching string start from "name:", "address" and mend with ";".

Note: Save bellow string for search in "test.txt" file.

Hi my name:Kamran; and address:Delhi;. Working as a Software Developer and my area of expertise in PHP and Java/J2EE and I would like to work in software company providing cutting edge PHP and Java base web application.
In this file I am searching string in php name:ahmad; and address:delhi; with php program bellow. searching string from text file in php.

Note: Save bellow file "searchstring.php".
Note: Remove "\" every place from program

<\?php
    $filename = "test.txt";
    $fp = fopen($filename, "r") or die("couldn't open $filename");
    while(!feof($fp))
    {
        $char = fgets($fp);
        $name = strstr($char,'name:');
        $name1 = substr($name, 5);
        $name2 = strpos($name1, ';');

        echo $name3 = substr($name1, 0, $name2);
        echo "<\br>";

        $address = strstr($char,'address:');
        $address1 = substr($address, 8);
        $address2 = strpos($address1, ';');

        echo $address3 = substr($address1, 0, $address2);
        echo "<\br>";
    }
?>

Tuesday, June 15, 2010

JSP Connectivity with SQL Server

Note :-
For the connectivity with JSP to SQL Server first Download Microsoft SQL Server JDBC 2.0 Driver for java on the bellow URL:

http://www.microsoft.com/downloads/details.aspx?FamilyID=99b21b65-e98f-4a61-b811-19912601fdc9&displaylang=en

or

http://go.microsoft.com/fwlink/?LinkId=144633&clcid=0x409

After download it gives sqljdbc_2.0.1803.100_enu.exe. Run it, then it will gives a zip file, extract  Zip file. Copy and paste sqljdbc4.jar in  Paste java->jdk->jre->->lib->ext Folder after that restart the Tomcat-Apache webserver.



<%@ page language="java" import="java.sql.*" %>
<%
    Connection con = null;
    Statement stmt =null;
    ResultSet rs = null;
    try{ 
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String url = "jdbc:sqlserver://localhost:1433;username=mysqlusername;password=mysqlpassword;databasename=dbname";
        conn = DriverManager.getConnection(url);
        stmt = con.createStatement();
        out.println("Database connected");
        String queryCountry = "Select * from table";
        ResultSet countResultSet = stmt.executeQuery(queryCountry);
            while(countResultSet.next()){
                    out.println(countResultSet.getString("name"));
                    out.println("\n");
            }
   
      }catch(Exception e){
            out.println(e);
      }

 %>

Friday, April 2, 2010

jsp connectivity with mysql

Download connector/j  Jar file Copy and Paste java->jdk->jre->ext->lib Folder folder.

create a mysqlconnection.jsp file

<%@ page language="java" import="java.sql.*" %>
<%
    Connection con = null;
    Statement stmt =null;
    ResultSet rs = null;
    try
   {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection("jdbc:mysql:mysql_databsename","mysql_username", "mysql_password");
        stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT * FROM tab");
        while (rs.next())
       {
             out.println(rs.getString("field"));
       }
       rs.close();
   }
  catch (SQLException ex)
  {
            //Catch Errors
            out.println("SQLException: " + ex.getMessage());
            out.println("SQLState: " + ex.getSQLState());
            out.println("VendorError: " + ex.getErrorCode());
   }
  %>

Thursday, February 4, 2010

JSP Connectivity with Oracle 8i and 9i

Step - 1 :- Copy Oracle JDBC folder's *.jar files and classes111.zip or classes12.zip
Step - 2 :- Paste java->jdk->jre->ext->lib Folder

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@page import="java.net.*"%>
<%@ page import="oracle.jdbc.driver.*" %>
<%@ page import="oracle.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.math.*" %>
<%@ page import="java.*" %>
<%
    Connection con = null;
    Statement stat = null;
    ResultSet rs = null;
    String sql=null;
    try
    {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
           Class.forName("oracle.jdbc.driver.OracleDriver");
                con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:databasename","scott","tiger");
                stat = con.createStatement();
    }
        catch(SQLException e)
    {
        out.print("Error = " + e );
    }
%>

Friday, January 22, 2010

How To Create A Pagination In PHP

Use Pagination to display results of your MySQL query over multiple pages in PHP.

Note : Remove "\" with a href tag

pagination.php

 ////// Connection with mysql Database////////

$connect = mysql_connect("yourhostname", "username", "password", "true") or die(mysql_error());
mysql_select_db("databaseName", $connect) or die(mysql_error());

////// pagination limit 10 records in a page //////

$limit =10;
if(!isset($_GET['page']))
{
     $page=1;
}
else
{
    $page=$_GET['page'];
}
$limitstart = ($page-1)*$limit;
$strgry = "select * from tableName";
$num =mysql_num_rows(mysql_query($strgry));
$pages = ceil($num/$limit);
$strgry = "select * from tableName Limit $limitstart, $limit";
$query = mysql_query($strgry) or die (mysql_error());
while($row=mysql_fetch_array($query))
{
    echo $row[0];
    echo $row[1];
    echo "\n";
}
if($page!=1)
{
    $pr=$page-1;
    echo "<\a href='pagination.php?page=$pr'>Prev<\/a>";
    echo " ";
}
for($i;$i<=$pages;$i++)
{
    if($i!=$page)
   {
        echo "<\a href='pagination.php?page=$i'>";
        echo $i;
        echo "<\/a>";
   }
   else
  {
        echo $i;
        echo " ";
   }
}
if($page!=$pages)
{
    $nxt=$page+1;
    echo "<\a href='pagination.php?page=$nxt'>Next<\/a>";
    echo " ";
}

Tuesday, January 19, 2010

How to store multiple values in a session variable with in a form submit in php

Note : Save this page session.php
Note : Remove "\" from all HTML tag and php start tag

<\?php
session_start();

$sessValue = "";
    if(isset($_REQUEST['submit']))
    {
        $sessValue = $_REQUEST['sessValue'];
        if(!isset($_SESSION['sessValue1']))
        {
            echo $_SESSION['sessValue1']=$sessValue;
        }
        else
        {
            echo $_SESSION['sessValue1']=$_SESSION['sessValue1'].$sessValue;
        }
    }
?>
<\html>
<\body>
<\form name="frm" action="session.php" method="post">
    <\input type="text" name="sessValue">
    <\input type="submit" name="submit" value="submit">
<\/form>
<\/body>
<\/html>

How to upload file in php

Step 1 - Save Bellow code name "upload.php"
Step 2 - Create a folder name upload for file upload
Note : Remove "\" from all HTML tag and php start tag

<\?php
        if(isset($_REQUEST['submit']))
        {
            $tmp_file = $_FILES['upload']['tmp_name'];
            $filename = $_FILES['upload']['name'];
           
            if(move_uploaded_file("$tmp_file", "upload/$filename"))
            {
                echo "File Success Fully Uploaded";
            }
            else
            {
                echo "File Upload Failed";
            }
        }
?>
<\html>
<\body>
<\form name="frm" method="post" action="upload.php" enctype="multipart/form-data">
    <\input type="file" name="upload">
    <\input type="submit" name="submit" value="Upload">
<\/form>
<\/body>
<\/html>