What I am trying to do is have user on the iPhone input a name, it should then query the DB and output the information in all columns related to that name.
1. I'd appreciate any assistance pulling back the data in normal format (rather than JSON string)
2. I'd appreciate any assistance editing the iPhone Controller file (to remove unnecessary code and insert an input box)
2. I'd appreciate any assistance editing the PHP file so the users entered name correlates to what the PHP file is querying on the DB (PHP file needs editing too?)
I have implemented the following;
iPhone
PHP Code:
//
// AuthenticationViewController.m
// Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details
//
// Created by Ben Copsey on 01/08/2009.
// Copyright 2009 All-Seeing Interactive. All rights reserved.
//
#import "AuthenticationViewController.h"
#import "ASIHTTPRequest.h"
#import "InfoCell.h"
#import "DetailCell.h"
#import "ToggleCell.h"
@interface UIAlertView (SPI)
- (void) addTextFieldWithValue:(NSString *) value label:(NSString *) label;
- (void) addTextFieldAtIndex:(NSUInteger) index;
- (UITextField *) textFieldAtIndex:(NSUInteger) index;
@end
// Private stuff
@interface AuthenticationViewController ()
- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest;
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest;
@end
@implementation AuthenticationViewController
- (IBAction)fetchTopSecretInformation:(id)sender
{
[self setRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8888/test/jsontest.php"]]];
[request setUseKeychainPersistence:[useKeychain isOn]];
[request setDelegate:self];
[request setShouldPresentAuthenticationDialog:[useBuiltInDialog isOn]];
[request setDidFinishSelector:@selector(topSecretFetchComplete:)];
[request setDidFailSelector:@selector(topSecretFetchFailed:)];
[request startAsynchronous];
}
- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest
{
[responseField setText:[[request error] localizedDescription]];
[responseField setFont:[UIFont boldSystemFontOfSize:12]];
}
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest
{
[responseField setText:[request responseString]];
[responseField setFont:[UIFont boldSystemFontOfSize:12]];
}
- (void)authenticationNeededForRequest:(ASIHTTPRequest *)theRequest
{
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Please Login" message:[request authenticationRealm] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil] autorelease];
// These are undocumented, use at your own risk!
// A better general approach would be to subclass UIAlertView, or just use ASIHTTPRequest's built-in dialog
[alertView addTextFieldWithValue:@"" label:@"Username"];
[alertView addTextFieldWithValue:@"" label:@"Password"];
[alertView show];
}
- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)theRequest
{
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Please Login to proxy" message:[request authenticationRealm] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil] autorelease];
[alertView addTextFieldWithValue:@"" label:@"Username"];
[alertView addTextFieldWithValue:@"" label:@"Password"];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
if ([[self request] authenticationNeeded] == ASIHTTPAuthenticationNeeded) {
[[self request] setUsername:[[alertView textFieldAtIndex:0] text]];
[[self request] setPassword:[[alertView textFieldAtIndex:1] text]];
[[self request] retryUsingSuppliedCredentials];
} else if ([[self request] authenticationNeeded] == ASIProxyAuthenticationNeeded) {
[[self request] setProxyUsername:[[alertView textFieldAtIndex:0] text]];
[[self request] setProxyPassword:[[alertView textFieldAtIndex:1] text]];
[[self request] retryUsingSuppliedCredentials];
}
} else {
[[self request] cancelAuthentication];
}
}
- (BOOL)respondsToSelector:(SEL)selector
{
if (selector == @selector(authenticationNeededForRequest:) || selector == @selector(proxyAuthenticationNeededForRequest:)) {
if ([useBuiltInDialog isOn]) {
return NO;
}
return YES;
}
return [super respondsToSelector:selector];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc
{
[request cancel];
[request release];
[responseField release];
[super dealloc];
}
/*
Most of the code below here relates to the table view, and isn't that interesting
*/
- (void)viewDidLoad
{
[super viewDidLoad];
[[[self navigationBar] topItem] setTitle:@"test"];
responseField = [[UITextView alloc] initWithFrame:CGRectZero];
[responseField setBackgroundColor:[UIColor clearColor]];
[responseField setEditable:NO];
[responseField setText:@"test information if successful"];
}
static NSString *intro = @"testing query";
- (UIView *)tableView:(UITableView *)theTableView viewForHeaderInSection:(NSInteger)section
{
if (section == 1) {
int tablePadding = 40;
int tableWidth = [tableView frame].size.width;
if (tableWidth > 480) { // iPad
tablePadding = 110;
}
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableWidth-(tablePadding/2),30)] autorelease];
UIButton *goButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[goButton setTitle:@"Go!" forState:UIControlStateNormal];
[goButton sizeToFit];
[goButton setFrame:CGRectMake([view frame].size.width-[goButton frame].size.width+10,7,[goButton frame].size.width,[goButton frame].size.height)];
[goButton addTarget:self action:@selector(fetchTopSecretInformation:) forControlEvents:UIControlEventTouchDown];
[view addSubview:goButton];
return view;
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int tablePadding = 40;
int tableWidth = [tableView frame].size.width;
if (tableWidth > 480) { // iPad
tablePadding = 110;
}
UITableViewCell *cell;
if ([indexPath section] == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"InfoCell"];
if (!cell) {
cell = [InfoCell cell];
}
[[cell textLabel] setText:intro];
[cell layoutSubviews];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"Response"];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Response"] autorelease];
[[cell contentView] addSubview:responseField];
}
[responseField setFrame:CGRectMake(5,5,tableWidth-tablePadding,150)];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (CGFloat)tableView:(UITableView *)theTableView heightForHeaderInSection:(NSInteger)section
{
return 34;
}
- (CGFloat)tableView:(UITableView *)theTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath section] == 0) {
return [InfoCell neededHeightForDescription:intro withTableWidth:[tableView frame].size.width]+20;
} else if ([indexPath section] == 2) {
return 160;
} else {
return 42;
}
}
- (NSString *)tableView:(UITableView *)theTableView titleForHeaderInSection:(NSInteger)section
{
return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
@synthesize request;
@end
PHP
PHP Code:
<?php
// Database credentials
$host = 'localhost';
$db = 'test';
$uid = 'root';
$pwd = 'password';
// Connect to the database server
$link = mysql_connect($host, $uid, $pwd) or die("Could not connect");
//select the json database
mysql_select_db($db) or die("Could not select database");
// Create an array to hold our results
$arr = array();
//Execute the query
$rs = mysql_query("SELECT name,description FROM test");
// Add the rows to the array
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
//return the json result. The string users is just a name for the container object. Can be set anything.
echo '{"name":'.json_encode($arr).'}';
?>
At the moment, the iPhone app just has a go button and outputs the following data;
Quote:
|
{"name":[{"name":"firstname-1","name":"bob","description":"bald"},{"name":"fir stname-2","name":"joe","description":"skinny"},{"name":"f irstname-3","name":"mike","description":"tough"}]}
|
I'd like, if a user enters the name "joe" and clicks "go" - it will then output:
Name: joe
Description: bald
Thank you so much in advance!