Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-24-2011, 10:45 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 10
justine80 is on a distinguished road
Unhappy Having trouble passing data to and from the iPhone?

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)
    - (
voidaddTextFieldWithValue:(NSString *) value label:(NSString *) label;
    - (
voidaddTextFieldAtIndex:(NSUIntegerindex;
    - (
UITextField *) textFieldAtIndex:(NSUIntegerindex;
    @
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 errorlocalizedDescription]];
        [
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 allocinitWithTitle:@"Please Login" message:[request authenticationRealmdelegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nilautorelease];
        
// 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 allocinitWithTitle:@"Please Login to proxy" message:[request authenticationRealmdelegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nilautorelease];
        [
alertView addTextFieldWithValue:@"" label:@"Username"];
        [
alertView addTextFieldWithValue:@"" label:@"Password"];
        [
alertView show];
    }
    
    
    
    - (
void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    
{
        if (
buttonIndex == 1) {
            if ([[
self requestauthenticationNeeded] == ASIHTTPAuthenticationNeeded) {
                [[
self requestsetUsername:[[alertView textFieldAtIndex:0text]];
                [[
self requestsetPassword:[[alertView textFieldAtIndex:1text]];
                [[
self requestretryUsingSuppliedCredentials];
            } else if ([[
self requestauthenticationNeeded] == ASIProxyAuthenticationNeeded) {
                [[
self requestsetProxyUsername:[[alertView textFieldAtIndex:0text]];
                [[
self requestsetProxyPassword:[[alertView textFieldAtIndex:1text]];
                [[
self requestretryUsingSuppliedCredentials];
            }
        } else {
            [[
self requestcancelAuthentication];
        }
    }
    
    - (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 navigationBartopItemsetTitle:@"test"];
        
responseField = [[UITextView allocinitWithFrame: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 allocinitWithFrame: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 textLabelsetText:intro];
            [
cell layoutSubviews];
            
    
        } else {
            
            
cell = [tableView dequeueReusableCellWithIdentifier:@"Response"];
            if (!
cell) {
                
cell = [[[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Response"autorelease];
                
    
                [[
cell contentViewaddSubview: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!
justine80 is offline   Reply With Quote
Reply

Bookmarks

Tags
asihttprequest, iphone, mysql, php

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 406
17 members and 389 guests
7twenty7, blasterbr, buggen, chiataytuday, Clouds, dre, fiftysixty, HemiMG, jeroenkeij, jimmyon122, jonathandeknudt, LEARN2MAKE, n00b, nyoe, pungs, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,902
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:58 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0