Tuesday 2 April 2013

53)JSON (Read Records)


APILinks.txt


Read:
            http://.../JSON/student.php?action=list

Add:
            http://…/JSON/student.php?action=add&Name=Alex&Age=16&Perc=90

Edit:
            http://.../JSON/student.php?action=edit&Name=Alex&Age=16&Perc=90.86&StudentId=3

Delete:
            http://.../JSON/student.php?action=delete&StudentId=3


student.php (Json File)

<?php
            $DB_Host = "localhost";
            $DB_Name = "test_ios_trainees";
            $DB_Username = "root";
            $DB_Password = "root123";
           
            $WEBSITE_URL = "http://.../JSON/";
           
            $con = mysql_connect($DB_Host, $DB_Username, $DB_Password) or die(mysql_errno());
            $db = mysql_select_db($DB_Name, $con) or die(mysql_error());
           
            $action = ( isset($_REQUEST["action"]) && trim($_REQUEST["action"]) != "" ) ? trim($_REQUEST["action"]) : "";
           
            if($action == "list")
            {
                        $jsonRecArr = array();
                       
                        $sqry = "SELECT * FROM students ORDER BY Name ASC";
                        $sres = mysql_query($sqry) or die("can not select form students - ".mysql_error());
                        while($srow = mysql_fetch_array($sres))
                        {
                                    $recStr = '{"StudentId":"'.trim($srow["StudentId"]).'", "Name":"'.trim($srow["Name"]).'", "Age":"'.trim($srow["Age"]).'", "Perc":"'.trim($srow["Perc"]).'"}';
                                   
                                    array_push($jsonRecArr, $recStr);
                        }
                        mysql_free_result($sres);
           
                        $json = '{"students":['.implode(",", $jsonRecArr).']}';
                       
                        echo $json;
                        exit;
            }
            else if($action == "add")
            {
                        if(        isset($_REQUEST["Name"]) && trim($_REQUEST["Name"]) != "" &&
                                    isset($_REQUEST["Age"]) && trim($_REQUEST["Age"]) != "" &&
                                    isset($_REQUEST["Perc"]) && trim($_REQUEST["Perc"]) != "")
                        {
                                    $iqry = "INSERT INTO students SET
                                                                                                                                    Name = '".trim($_REQUEST["Name"])."',
                                                                                                                                    Age = '".(int)trim($_REQUEST["Age"])."',
                                                                                                                                    Perc = '".(float)trim($_REQUEST["Perc"])."'";
                                                                                                                                                           
                                    mysql_query($iqry) or die("can not insert into students - ".mysql_error());              
                                   
                                    echo "OK";
                                    exit;                                                    
                        }
                        else
                        {
                                    echo "INVALID PARAMETER";
                                    exit;
                        }
            }
            else if($action == "edit")
            {
                        if(        isset($_REQUEST["StudentId"]) && trim($_REQUEST["StudentId"]) != "" &&
                                    isset($_REQUEST["Name"]) && trim($_REQUEST["Name"]) != "" &&
                                    isset($_REQUEST["Age"]) && trim($_REQUEST["Age"]) != "" &&
                                    isset($_REQUEST["Perc"]) && trim($_REQUEST["Perc"]) != "")
                        {
                                    $iqry = "UPDATE students SET
                                                                                                                        Name = '".trim($_REQUEST["Name"])."',
                                                                                                                        Age = '".(int)trim($_REQUEST["Age"])."',
                                                                                                                        Perc = '".(float)trim($_REQUEST["Perc"])."'
                                                                                                            WHERE StudentId = '".trim($_REQUEST["StudentId"])."'";
                                                                                                                                                           
                                    mysql_query($iqry) or die("can not update students - ".mysql_error());                                                                               
                                   
                                    echo "OK";
                                    exit;
                        }
                        else
                        {
                                    echo "INVALID PARAMETER";
                                    exit;
                        }
            }
            else if($action == "delete")
            {
                        if(        isset($_REQUEST["StudentId"]) && trim($_REQUEST["StudentId"]) != "")
                        {
                                    $iqry = "DELETE FROM students WHERE StudentId = '".trim($_REQUEST["StudentId"])."'";                                                                                                                                                         
                                    mysql_query($iqry) or die("can not delete from students - ".mysql_error());            
                                   
                                    echo "OK";
                                    exit;                                                    
                        }                     
                        else
                        {
                                    echo "INVALID PARAMETER";
                                    exit;
                        }
            }
            else
            {
                        echo "Nothing is here for you!";
                        exit;
            }
?>       


Import JSON Whole file for Framework


AppDelegate.h

@property (nonatomic, retain) NSString *apiURL;

AppDelegate.m

@synthesize apiURL;

In didfinisj=h method..
    apiURL = @"http://.../JSON/";

ViewController.h


#import <UIKit/UIKit.h>

@class AppDelegate;

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    AppDelegate *appDel;
   
    NSMutableArray *recordsArr;
}

@property (nonatomic, retain) IBOutlet UITableView *recordTblView;

@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *processActView;

@end

ViewController.m


#import "ViewController.h"
#import "AppDelegate.h"
#import "Student.h"
#import "MyFunctions.h"
#import "StudentCellViewController.h"

@implementation ViewController

@synthesize recordTblView,
            processActView;

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    appDel = (ip24AppDelegate *)[[UIApplication sharedApplication] delegate];
   
    processActView.hidden = NO;
   
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(recordsRequest) object:nil];
    [queue addOperation:operation];
    [operation release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];   
}

- (void)recordsRequest
{
    NSString *post =[NSString stringWithFormat:@""];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
   
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@student.php?action=list", appDel.apiURL]]];
   
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
   
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    NSString *data= [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
   
    [self performSelectorOnMainThread:@selector(recordResponse:) withObject:data waitUntilDone:NO];
}

- (void)recordResponse:(NSString *)str
{  
    recordsArr = [Student readRecords:str];
   
    [recordTblView reloadData];
   
    processActView.hidden = YES;
}

#pragma mark - Table Methods


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return recordsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   
    Student *sObj = [recordsArr objectAtIndex:indexPath.row];
   
    StudentCellViewController *studentCellVC = [[StudentCellViewController alloc] init];
    studentCellVC.studentObj = sObj;
   
    [cell.contentView addSubview:studentCellVC.view];
   
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Student *sObj = [recordsArr objectAtIndex:indexPath.row];
   
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(deleteRecordRequest:) object:sObj];
    [queue addOperation:operation];
   
    [recordsArr removeObjectAtIndex:indexPath.row];
   
    [recordTblView beginUpdates];
    [recordTblView deleteRowsAtIndexPaths:[[NSArray alloc] initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationMiddle];
    [recordTblView endUpdates];
}

- (void)dealloc
{
    [recordTblView release];
    [processActView release];
   
    [super dealloc];
}

@end

MyFunctions.h


#import <Foundation/Foundation.h>

@interface MyFunctions : NSObject

+ (NSString *)trim:(NSString *)stringToTrim;

@end

MyFunctions.m


#import "MyFunctions.h"
@implementation MyFunctions

+ (NSString *)trim:(NSString *)stringToTrim
{
            return [stringToTrim stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

@end

Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject

@property (nonatomic, retain) NSString  *StudentId,
                                        *Name,
                                        *Age,
                                        *Perc;

+ (NSMutableArray *)readRecords:(NSString *)dataStr;

@end


Student.m

 

#import "Student.h"
#import "SBJson.h"
#import "NSObject+SBJson.h"
#import "MyFunctions.h"

@implementation Student

@synthesize StudentId,
            Name,
            Age,
            Perc;

+ (NSMutableArray *)readRecords:(NSString *)dataStr
{
    NSMutableArray *recArr = [[NSMutableArray alloc] init];
   
    NSDictionary *json = [dataStr JSONValue];
   
    NSArray *items = [json valueForKey:@"students"];
    NSEnumerator *enumerator = [items objectEnumerator];
    NSDictionary *item;
   
    while((item = (NSDictionary *)[enumerator nextObject]))
    {
        Student *sObj = [[Student alloc] init];       
        sObj.StudentId = [MyFunctions trim:[item objectForKey:@"StudentId"]];
        sObj.Name = [MyFunctions trim:[item objectForKey:@"Name"]];
        sObj.Age = [MyFunctions trim:[item objectForKey:@"Age"]];
        sObj.Perc = [MyFunctions trim:[item objectForKey:@"Perc"]];
       
        [recArr addObject:sObj];
    }
   
    return recArr;
}

@end

StudentCellViewController.h


#import <UIKit/UIKit.h>

@class Student;

@interface StudentCellViewController : UIViewController

@property (nonatomic, retain) IBOutlet UILabel  *NameLbl,
                                                *AgeLbl,
                                                *PercLbl;

@property (nonatomic, retain) Student *studentObj;

@end

StudentCellViewController.m


#import "StudentCellViewController.h"
#import "Student.h"
#import "MyFunctions.h"

@implementation StudentCellViewController

@synthesize NameLbl,
            AgeLbl,
            PercLbl;

@synthesize studentObj;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    NameLbl.text = [MyFunctions trim:studentObj.Name];
    AgeLbl.text = [NSString stringWithFormat:@"%@yr", [MyFunctions trim:studentObj.Age]];
    PercLbl.text = [NSString stringWithFormat:@"%@%%", [MyFunctions trim:studentObj.Perc]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

52)xml parsing retrieve data from the MYSQL{Add/Delete/Update}



AppDelegate.h

@property (strong, nonatomic) IBOutlet UIWindow *window;

@property (strong, nonatomic) IBOutlet UINavigationController *nav;

@property (nonatomic, retain) NSString *apiURL;

AppDelegate.m

@synthesize apiURL;
@synthesize nav;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    apiURL = @"http://FolderPath/XML/";
   
    self.window.rootViewController = nav;
   
    [self.window makeKeyAndVisible];
    return YES;
}

ViewController.h

@class  ip23AppDelegate,
        Student;

@interface ip23ViewController : UIViewController    <NSXMLParserDelegate,
                                                    UITableViewDataSource,
                                                    UITableViewDelegate>
{
    ip23AppDelegate *appDel;
   
    Student *aRecord;

    NSMutableString *recordsStr;
   
    NSMutableArray *recordsArr;
}

@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *processActView;

@property (nonatomic, retain) IBOutlet UITableView *recordsTblView;

@property (nonatomic, retain) IBOutlet UIBarButtonItem  *refreshBarBtn,
                                                        *addBarBtn;

- (IBAction)refreshBtnPressed:(id)sender;

- (IBAction)addBtnPressed:(id)sender;

@end

ViewController.m

#import "ViewController.h"
#import "AppDelegate.h"
#import "Student.h"
#import "MyFunctions.h"
#import "StudentCellViewController.h"

@implementation ip23ViewController

@synthesize processActView,
            recordsTblView;

@synthesize refreshBarBtn,
            addBarBtn;

- (IBAction)refreshBtnPressed:(id)sender
{
    [self initTableAndView];
}

- (IBAction)addBtnPressed:(id)sender
{
    Student *sObj = [[Student alloc] init];
    sObj.Name = @"Alex";
    sObj.Age = @"16";
    sObj.Perc = @"90.16";
   
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(addRecordRequest:) object:sObj];
    [queue addOperation:operation];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.navigationItem.leftBarButtonItem = refreshBarBtn;
    self.navigationItem.rightBarButtonItem = addBarBtn;   

    appDel = (ip23AppDelegate *)[[UIApplication sharedApplication] delegate];
   
    [self initTableAndView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Add

- (void)addRecordResponse:(NSString *)response
{   
    if([response isEqualToString:@"OK"])
    {
        [[[UIAlertView alloc] initWithTitle:@"Success!"
                                    message:@"Record added successfully."
                                   delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
       
        [self initTableAndView];
    }
    else if([response isEqualToString:@"INVALID PARAMETER"])
    {
        [[[UIAlertView alloc] initWithTitle:@"Warning!"
                                    message:@"Record is not added.\nPlease varify your data."
                                   delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
    }
    else
    {       
        [[[UIAlertView alloc] initWithTitle:@"Warning!"
                                    message:@"Something went wrong.\nPlease try again later."
                                   delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
    }
}

- (void)addRecordRequest:(Student *)sObj
{
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    NSMutableData *body = [NSMutableData data];
   
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[sObj.Name dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
       
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Age\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[sObj.Age dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
   
   
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Perc\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[sObj.Perc dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
   
   
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"action\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"add" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
   
    // close form
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
   
    NSString *urlString = [NSString stringWithFormat:@"%@/student.php", appDel.apiURL];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:body];
   
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
   
    [self performSelectorOnMainThread:@selector(addRecordResponse:) withObject:returnString waitUntilDone:NO];
}

#pragma mark - Delete

- (void)deleteRecordResponse:(NSString *)response
{
    if([response isEqualToString:@"OK"])
    {
        //Action
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"Warning!"
                                    message:@"Something went wrong.\nPlease try again later."
                                   delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
        
        [self initTableAndView];
    }
   
    //NSLog(@"%@", response);
}

- (void)deleteRecordRequest:(Student *)sObj
{
    NSString *post =[NSString stringWithFormat:@"action=delete&StudentId=%@", sObj.StudentId];
   
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
   
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/student.php", appDel.apiURL]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
   
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    NSString *resp = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
   
    [self performSelectorOnMainThread:@selector(deleteRecordResponse:) withObject:resp waitUntilDone:NO];
}

#pragma mark - XML Parse

- (void)initTableAndView
{
    recordsTblView.userInteractionEnabled = NO;
   
    processActView.hidden = NO;
   
    recordsArr = [[NSMutableArray alloc] init];
   
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getApiData) object:nil];
    [queue addOperation:operation];
}

- (void)loadDataInTable
{
    recordsTblView.userInteractionEnabled = YES;   
   
    processActView.hidden = YES;
   
    [recordsTblView reloadData];
}

- (void)getApiData
{  
    NSString *myApiLink = [NSString stringWithFormat:@"%@student.php?action=list",
                           appDel.apiURL];
   
    //NSLog(@"%@", myApiLink);
    NSURL *url = [[NSURL alloc] initWithString:myApiLink];
   
    NSXMLParser *xParse = [[NSXMLParser alloc] initWithContentsOfURL:url];
           
            [xParse setDelegate:self];
   
    [xParse parse];
   
   
    [self performSelectorOnMainThread:@selector(loadDataInTable) withObject:nil waitUntilDone:NO];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{  
    if([elementName isEqualToString:@"Student"])
    {
        aRecord = [[Student alloc] init];
            }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(!recordsStr)
            recordsStr = [[NSMutableString alloc] initWithString:string];
            else
            [recordsStr appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"Students"])
    {
                        return;
            }
            else if([elementName isEqualToString:@"Student"])
    {
                        [recordsArr addObject:aRecord];
       
                        aRecord = nil;
    }
            else
    {
        if([elementName isEqualToString:@"StudentId"] ||
           [elementName isEqualToString:@"Name"] ||
           [elementName isEqualToString:@"Age"] ||
           [elementName isEqualToString:@"Perc"])
        {
            [aRecord setValue:recordsStr forKey:elementName];
        }
            }
   
    recordsStr = nil;
}

#pragma mark - Table Methods


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return recordsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   
    Student *sObj = [recordsArr objectAtIndex:indexPath.row];   
   
    StudentCellViewController *studentCellVC = [[StudentCellViewController alloc] init];
    studentCellVC.studentObj = sObj;
   
    [cell.contentView addSubview:studentCellVC.view];
   
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Student *sObj = [recordsArr objectAtIndex:indexPath.row];
   
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(deleteRecordRequest:) object:sObj];
    [queue addOperation:operation];
   
    [recordsArr removeObjectAtIndex:indexPath.row];
   
    [recordsTblView beginUpdates];
    [recordsTblView deleteRowsAtIndexPaths:[[NSArray alloc] initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationMiddle];
    [recordsTblView endUpdates];
}

@end

MyFunctions.h

+ (NSString *)trim:(NSString *)stringToTrim;

MyFunctions.m

+ (NSString *)trim:(NSString *)stringToTrim
{
            return [stringToTrim stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

Student.h


@property (nonatomic, retain) NSString  *StudentId,
                                        *Name,
                                        *Age,
                                        *Perc;

Student.m

@synthesize StudentId,
            Name,
            Age,
            Perc;

StudentCellViewController.h

@property (nonatomic, retain) IBOutlet UILabel  *NameLbl,
                                                *AgeLbl,
                                                *PercLbl;

@property (nonatomic, retain) Student *studentObj;
@class Student;

StudentCellViewController.m

#import "StudentCellViewController.h"
#import "Student.h"
#import "MyFunctions.h"

@implementation StudentCellViewController

@synthesize NameLbl,
            AgeLbl,
            PercLbl;

@synthesize studentObj;
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    NameLbl.text = [MyFunctions trim:studentObj.Name];
    AgeLbl.text = [NSString stringWithFormat:@"%@yr", [MyFunctions trim:studentObj.Age]];
    PercLbl.text = [NSString stringWithFormat:@"%@%%", [MyFunctions trim:studentObj.Perc]];
}

API

API Links

Read:
            http://Folderpath/XML/student.php?action=list

Add:
            http:// Folderpath /XML/student.php?action=add&Name=Alex&Age=16&Perc=90

Edit:
            http:// Folderpath /XML/student.php?action=edit&Name=Alex&Age=16&Perc=90.86&StudentId=3

Delete:
http:// Folderpath /XML/student.php?action=delete&StudentId=3

student-list.php

<?php
            header ("content-type: text/xml");
           
            sleep(3);
           
            echo '<?xml version="1.0" encoding="UTF-8"?>
            <Students>
                        <Student>
                                    <Name>Alan</Name>
                                    <Age>18</Age>
                                    <Perc>88.90</Perc>
                        </Student>
                        <Student>
                                    <Name>Bob</Name>
                                    <Age>20</Age>
                                    <Perc>85.60</Perc>
                        </Student>
                        <Student>
                                    <Name>Alex</Name>
                                    <Age>16</Age>
                                    <Perc>90.16</Perc>
                        </Student>     
            </Students>';
?>

student-list.xml

<?xml version="1.0" encoding="UTF-8"?>
<Students>
            <Student>
                        <Name>Alan</Name>
                        <Age>18</Age>
                        <Perc>88.90</Perc>
            </Student>
            <Student>
                        <Name>Bob</Name>
                        <Age>20</Age>
                        <Perc>85.60</Perc>
            </Student>
            <Student>
                        <Name>Alex</Name>
                        <Age>16</Age>
                        <Perc>90.16</Perc>
            </Student>     
</Students>

Student.php


<?php
            $DB_Host = "localhost";
            $DB_Name = "test_ios_trainees";
            $DB_Username = "root";
            $DB_Password = "root123";
           
            $WEBSITE_URL = "http://FolderPath/XML/";
           
            $con = mysql_connect($DB_Host, $DB_Username, $DB_Password) or die(mysql_errno());
            $db = mysql_select_db($DB_Name, $con) or die(mysql_error());
           
            $action = ( isset($_REQUEST["action"]) && trim($_REQUEST["action"]) != "" ) ? trim($_REQUEST["action"]) : "";
           
            $xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>';
           
            if($action == "list")
            {
                        header ("content-type: text/xml");
                       
                        $xml = $xmlHeader;
                        $xml .= '<Students>';
           
                        $sqry = "SELECT * FROM students ORDER BY Name ASC";
                        $sres = mysql_query($sqry) or die("can not select form students - ".mysql_error());
                        while($srow = mysql_fetch_array($sres))
                        {
                                    $xml .= '<Student>';                          
                                                $xml .= '<StudentId>'.trim($srow["StudentId"]).'</StudentId>';
                                                $xml .= '<Name>'.trim($srow["Name"]).'</Name>';
                                                $xml .= '<Age>'.trim($srow["Age"]).'</Age>';
                                                $xml .= '<Perc>'.trim($srow["Perc"]).'</Perc>';
                                    $xml .= '</Student>';
                        }
                        mysql_free_result($sres);
                       
                        $xml .= '</Students>';
           
                        echo $xml;
                        exit;
            }
            else if($action == "add")
            {
                        if(        isset($_REQUEST["Name"]) && trim($_REQUEST["Name"]) != "" &&
                                    isset($_REQUEST["Age"]) && trim($_REQUEST["Age"]) != "" &&
                                    isset($_REQUEST["Perc"]) && trim($_REQUEST["Perc"]) != "")
                        {
                                    $iqry = "INSERT INTO students SET
                                                                                                                                    Name = '".trim($_REQUEST["Name"])."',
                                                                                                                                    Age = '".(int)trim($_REQUEST["Age"])."',
                                                                                                                                    Perc = '".(float)trim($_REQUEST["Perc"])."'";
                                                                                                                                                           
                                    mysql_query($iqry) or die("can not insert into students - ".mysql_error());              
                                   
                                    echo "OK";
                                    exit;                                                    
                        }
                        else
                        {
                                    echo "INVALID PARAMETER";
                                    exit;
                        }
            }
            else if($action == "edit")
            {
                        if(        isset($_REQUEST["StudentId"]) && trim($_REQUEST["StudentId"]) != "" &&
                                    isset($_REQUEST["Name"]) && trim($_REQUEST["Name"]) != "" &&
                                    isset($_REQUEST["Age"]) && trim($_REQUEST["Age"]) != "" &&
                                    isset($_REQUEST["Perc"]) && trim($_REQUEST["Perc"]) != "")
                        {
                                    $iqry = "UPDATE students SET
                                                                                                                        Name = '".trim($_REQUEST["Name"])."',
                                                                                                                        Age = '".(int)trim($_REQUEST["Age"])."',
                                                                                                                        Perc = '".(float)trim($_REQUEST["Perc"])."'
                                                                                                            WHERE StudentId = '".trim($_REQUEST["StudentId"])."'";
                                                                                                                                                           
                                    mysql_query($iqry) or die("can not update students - ".mysql_error());                                                                               
                                   
                                    echo "OK";
                                    exit;
                        }
                        else
                        {
                                    echo "INVALID PARAMETER";
                                    exit;
                        }
            }
            else if($action == "delete")
            {
                        if(        isset($_REQUEST["StudentId"]) && trim($_REQUEST["StudentId"]) != "")
                        {
                                    $iqry = "DELETE FROM students WHERE StudentId = '".trim($_REQUEST["StudentId"])."'";                                                                                                                                                         
                                    mysql_query($iqry) or die("can not delete from students - ".mysql_error());            
                                   
                                    echo "OK";
                                    exit;                                                    
                        }                     
                        else
                        {
                                    echo "INVALID PARAMETER";
                                    exit;
                        }
            }
            else
            {
                        echo "Nothing is here for you!";
                        exit;
            }
?>