Thursday, November 22, 2007

Paging in CodeIgniter PHP

Paging in CodeIgniter PHP


CONTROLLER

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class City extends CI_Controller {
    public function __construct() {
        parent:: __construct();
        $this->load->helper("url");
        $this->load->model("Cities");
        $this->load->library("pagination");
    }
    public function index() {
        $this->load->view('welcome_message');
    }
    public function page() {
        $config = array();
        $config["base_url"] = base_url() . "city/page";
        $config["total_rows"] = $this->Cities->city_record_count();
        $config["per_page"] = 20;
        $config["uri_segment"] = 3;
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["results"] = $this->Cities->fetch_cities($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
        $this->load->view("city_view", $data);
    }
}


MODEL

<?php class Cities extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->database();
    }
    public function city_record_count() {
        $this->db->where('city_id >', 10);
        $this->db->from('cities');
        return $this->db->count_all_results();
        //return $this->db->count_all("cities");
    }

    public function fetch_cities($limit, $start) {
        $this->db->limit($limit, $start);
        $this->db->where('city_id >', 10);
        $this->db->order_by("city_name", "asc");
        $query = $this->db->get("cities");
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
}



VIEW

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>
</head>
<body>
 <div id="container">
  <h1>Cities</h1>
  <div id="body">
<?php
foreach($results as $data) {
    echo $data->city_id . " - " . $data->city_name . "<br>";
}
?>
   <p><?php echo $links; ?></p>
  </div>
  <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
 </div>
</body>
</html>

Tuesday, November 20, 2007

config.getServletContext().getRealPath() is not working

config.getServletContext().getRealPath() is not working

This is the very common problem in Servlet and JSP while you deploy WAR file in tomcat or any other server. to over come this problem just extract the .WAR file in the "tomcat/webapps" directory.
and problem will be solved.


Wednesday, November 14, 2007

Sort Alphanumeric Data in Real Number in Sql

Sort Alphanumeric Data in Real Number in Sql


Suppose you have a table with columns pID Autonumber, registrationNo varchar(50) ,pName varchar(100)
registrationNo value is the first character is alphabets and rest is the number listed below:


pID registrationNo
1,    A1315
2,    A1
3,    A132
4,    A2
5,    A1316
6,    A1317
7,    A1318
8,    A133
9,    A1330
10,   A1331
11,   A3
12,   A1450
13,   A4
14,   A5643
15,   A5
16,   A1390
17,   A6


My client want to see those records in ascending number order as listed below order:
pID registrationNo
2,    A1
4,    A2
11,   A3
13,   A4
15,   A5
17,   A6
3,    A132
8,    A133
9,    A1330
10,   A1331
1,    A1315
5,    A1316
6,    A1317
7,    A1318
16,   A1390
12,   A1450
14,   A5643


but when retrieve the order by registrationNo. records was coming in ascending order,
because registrationNo are not a integer data type , those are Alphanumeric data type.

So how to show the Alphanumeric data in real ascending number order?
I have solved this problem with using listed below Sql. listed below sql will show the records which start with character 'a' :

SELECT pID,CAST(SUBSTRING(registrationNo, 2, 10) AS INT) as registrationNoNew
FROM [patientdb].[dbo].[Patients] where registrationNo like '%a1%' order by registrationNoNew


Tuesday, November 13, 2007

Failed to create sessionFactory object.org.hibernate.MappingException Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister Exception in thread main java.lang.NullPointerException

You can face this problem during Java Hibernate application development:

Failed to create sessionFactory object.org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
Exception in thread "main" java.lang.NullPointerException



Solution:
The most common mistake is that you have you have
missed any one getXXXX() or setXXXX() methods.

So check again your code and make the missed getXXXX() or setXXXX() methods.