我有一个包含此信息的表的MySQL数据库
ID - 商店名称 - 购物商品 - shoplogo - 类别
我想要的是从每个类别中选择一个随机的商店名称,这就是我所做的和它的工作,但我认为如果我有这个将不会有效让我们在10个不同类别的数据库中说10000条目。
- (IBAction)listShops:(id)sender {
arrayShopName = [[NSMutableArray alloc] init];
arrayShopDescription = [[NSMutableArray alloc] init];
arrayShopCategory = [[NSMutableArray alloc] init];
arrayShopLogoUrl = [[NSMutableArray alloc] init];
NSString *hostStr = [NSString stringWithFormat:@"%@Load.php", siteHost];
NSData *dataURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[hostStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]]];
NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:dataURL options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dictResponse: %@", dictResponse);
NSMutableArray *TempArrayShopName = [[NSMutableArray alloc] init];
NSMutableArray *TempArrayShopDescription = [[NSMutableArray alloc] init];
NSMutableArray *TempArrayShopCategory = [[NSMutableArray alloc] init];
NSMutableArray *TempArrayShopLogoUrl = [[NSMutableArray alloc] init];
for (NSDictionary *theCourse in dictResponse) {
[TempArrayShopName addObject:theCourse[@"shopname"]];
}
for (NSDictionary *theCourse in dictResponse) {
[TempArrayShopDescription addObject:theCourse[@"shopdescription"]];
}
for (NSDictionary *theCourse in dictResponse) {
[TempArrayShopCategory addObject:theCourse[@"category"]];
}
for (NSDictionary *theCourse in dictResponse) {
[TempArrayShopLogoUrl addObject:theCourse[@"shoplogo"]];
}
//================Adding all information for shop in Phones category=====================
NSMutableArray *PhonesTempArrayShopName = [[NSMutableArray alloc] init];
NSMutableArray *PhonesTempArrayShopDescription = [[NSMutableArray alloc] init];
NSMutableArray *PhonesTempArrayShopCategory = [[NSMutableArray alloc] init];
NSMutableArray *PhonesTempArrayShopLogoUrl = [[NSMutableArray alloc] init];
NSString *PhonesTargetString = @"Phones";
for (int i=0; i<TempArrayShopCategory.count; i++) {
NSString *name = [TempArrayShopCategory objectAtIndex:i];
if ([name isEqualToString:PhonesTargetString]) {
[PhonesTempArrayShopName addObject:[TempArrayShopName objectAtIndex:i]];
[PhonesTempArrayShopDescription addObject:[TempArrayShopDescription objectAtIndex:i]];
[PhonesTempArrayShopCategory addObject:[TempArrayShopCategory objectAtIndex:i]];
[PhonesTempArrayShopLogoUrl addObject:[TempArrayShopLogoUrl objectAtIndex:i]];
}
}
//================Adding all information for shop in Phones category=====================
//================Adding all information for shop in Electronics category=====================
NSMutableArray *ElectronicsTempArrayShopName = [[NSMutableArray alloc] init];
NSMutableArray *ElectronicsTempArrayShopDescription = [[NSMutableArray alloc] init];
NSMutableArray *ElectronicsTempArrayShopCategory = [[NSMutableArray alloc] init];
NSMutableArray *ElectronicsTempArrayShopLogoUrl = [[NSMutableArray alloc] init];
NSString *ElectronicsTargetString = @"Electronics";
for (int i=0; i<TempArrayShopCategory.count; i++) {
NSString *name = [TempArrayShopCategory objectAtIndex:i];
if ([name isEqualToString:ElectronicsTargetString]) {
[ElectronicsTempArrayShopName addObject:[TempArrayShopName objectAtIndex:i]];
[ElectronicsTempArrayShopDescription addObject:[TempArrayShopDescription objectAtIndex:i]];
[ElectronicsTempArrayShopCategory addObject:[TempArrayShopCategory objectAtIndex:i]];
[ElectronicsTempArrayShopLogoUrl addObject:[TempArrayShopLogoUrl objectAtIndex:i]];
}
}
//================Adding all information for shop in Electronics category=====================
//================Adding all information for shop in Toys category=====================
NSMutableArray *ToysTempArrayShopName = [[NSMutableArray alloc] init];
NSMutableArray *ToysTempArrayShopDescription = [[NSMutableArray alloc] init];
NSMutableArray *ToysTempArrayShopCategory = [[NSMutableArray alloc] init];
NSMutableArray *ToysTempArrayShopLogoUrl = [[NSMutableArray alloc] init];
NSString *ToysTargetString = @"Toys";
for (int i=0; i<TempArrayShopCategory.count; i++) {
NSString *name = [TempArrayShopCategory objectAtIndex:i];
if ([name isEqualToString:ToysTargetString]) {
[ToysTempArrayShopName addObject:[TempArrayShopName objectAtIndex:i]];
[ToysTempArrayShopDescription addObject:[TempArrayShopDescription objectAtIndex:i]];
[ToysTempArrayShopCategory addObject:[TempArrayShopCategory objectAtIndex:i]];
[ToysTempArrayShopLogoUrl addObject:[TempArrayShopLogoUrl objectAtIndex:i]];
}
}
//================Adding all information for shop in Toys category=====================
//Selecting random number from Phones category then adding the details to the array that will show in tableview
int ptasn = arc4random() % [PhonesTempArrayShopName count];
[arrayShopName addObject:[PhonesTempArrayShopName objectAtIndex:ptasn]];
[arrayShopDescription addObject:[PhonesTempArrayShopDescription objectAtIndex:ptasn]];
[arrayShopCategory addObject:[PhonesTempArrayShopCategory objectAtIndex:ptasn]];
[arrayShopLogoUrl addObject:[PhonesTempArrayShopLogoUrl objectAtIndex:ptasn]];
//Selecting random number from Electronics category then adding the details to the array that will show in tableview
int etasn = arc4random() % [ElectronicsTempArrayShopName count];
[arrayShopName addObject:[ElectronicsTempArrayShopName objectAtIndex:etasn]];
[arrayShopDescription addObject:[ElectronicsTempArrayShopDescription objectAtIndex:etasn]];
[arrayShopCategory addObject:[ElectronicsTempArrayShopCategory objectAtIndex:etasn]];
[arrayShopLogoUrl addObject:[ElectronicsTempArrayShopLogoUrl objectAtIndex:etasn]];
//Selecting random number from Toys category then adding the details to the array that will show in tableview
int ttasn = arc4random() % [ToysTempArrayShopName count];
[arrayShopName addObject:[ToysTempArrayShopName objectAtIndex:ttasn]];
[arrayShopDescription addObject:[ToysTempArrayShopDescription objectAtIndex:ttasn]];
[arrayShopCategory addObject:[ToysTempArrayShopCategory objectAtIndex:ttasn]];
[arrayShopLogoUrl addObject:[ToysTempArrayShopLogoUrl objectAtIndex:ttasn]];
[self.tableView reloadData];
}
那么从xCode端还是服务器端有更好的方法吗?
这是我的PHP代码
<?php
$DBhost="";
$DBusername="";
$DBpassword="";
$db_name="";
$tbl_name= "";
$conn = new mysqli($DBhost, $DBusername, $DBpassword, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM $tbl_name";
$result = $conn->query($sql);
$emparray = array();
while($row =mysqli_fetch_assoc($result)) {
$emparray[] = $row;
}
echo json_encode($emparray);
$conn->close();
?>