The wrong way to do string comparisons in the iPhone SDK
if(myTitle == @"HELLO")
The right way
if([myTilte isEqualToString:@"HELLO"])
The wrong way to do string comparisons in the iPhone SDK
if(myTitle == @"HELLO")
The right way
if([myTilte isEqualToString:@"HELLO"])
Dealing with trailing comas in lists is common problem for developers. I found the best way to handle this situation in PHP is with the following regular expression.
$string = eregi_replace(',$', '', $string);
Before: string = “2, 6, 9,”
After: string = “2, 6, 9”
Only the trailing coma will be stripped. If no coma is found, the string simply passes through. It’s a simple and effective solution.
WordPress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as meta-data. This meta-data can be used by theme authors to do some pretty cool things.
The theme I am currently using allows for a custom field named “thumb”. If you create the “thumb” custom field in a post and pass in the path to an image, for example “wp-content/uploads/2008/12/wordpress_logo.png”, a thumb nail version of the image will appear next the posts title on the home page of my blog. Awesome stuff.
When I discovered this theme and the “thumb” custom field, I already had 225 posts in my blog. I didn’t want to go back and manually create all the custom fields for the existing posts, so I wrote a PHP script to do it for me.
The PHP script cycles through your existing posts and looks for the first image in the post, and creates the thumb field based on it’s location. The script is configurable, your theme may want a different custom field name, like “thumbnail” or “image”.
This code is provided as is. If you aren’t a developer, you should be messing with stuff like this. Be sure to back up your wordpress database before running this.
Instruction:
Note: This is a one time deal, when you create new posts you will need to add the “thumb” field and image path manually
//your database configuration information
$username = "YOUR_USER_NAME";
$password = "YOUR_PASSWORD";
$database = "DATABASE";
$databaseLocation = "localhost";
//name of the Custom Field to use
$customFieldName = "thumb";
/******************* END CONFIGURATION *****************************/
//get the wordpress posts to work with
$query = "SELECT * FROM wp_posts";
if ($result = $mysqli->query("$query"))
{
//loop over all the posts
while ($row = $result->fetch_object())
{
//get all the info from the post that we need to work with
$cur_post_id = $row->ID;
$cur_post_content = $row->post_content;
//the pattern to find the html image tag
$pattern = '/]+src[\\s=\'"]';
$pattern .= '+([^"\'>\\s]+)/is';
//try to match the pattern against the post content
if(preg_match($pattern,$cur_post_content,$match))
{
//break up the image path
$pieces = explode("wp-content/", $match[1]);
//make sure the file name isnt empty
if($pieces[1] != "")
{
//build the file path
$imagePath = "wp-content/" . "$pieces[1]";
//insert the custom field for the post
$insertQuery = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES ($cur_post_id, '$customFieldName', '$imagePath')";
$mysqli->query("$insertQuery");
//debugging stuff
if ($mysqli->errno)
{
printf("failed: %s \n", $mysqli->error);
}
else
{
echo "..... inserting $imagePath \n";
}
flush();
}
}
}
}
echo "---completed---";
?>
I found an interesting issues with the iPhone SDK and the UIPickerView
The didSelectRow method gets called after a users selects a new value on a picker wheel. The method knows what wheel (component) was spun and the index of the newly selected value. This works great for a picker with a single wheel. My picker had 5 wheels in it.
If I spun two components of the picker at the same time. I would only get one call to the didSelectRow method. More specifically, if I spin one component and move another component no call was received until the first component stops spinning, and so the second component move is never registered.
Long story short, I didn’t catch this problem when I submitted my app, “Ka-Ching”, to the iTunes store. As a result of not catching this, Apple rejected my app. I could argue that the iPhone SDK should have so programmatic hooks that would only allow one component to be spun at a time, or even a method to return the state of all of components in the picker.
Trying to figure out a solution to the problem I expanded on my idea of recording the state of all the components in the picker when the didSelectRow method was called. Realistically, by the time that didSelectRow gets processed it is possible that all 5 of the components had changed.
Looking through the SDK documentation for the UIPickerView I came across this:
- (NSInteger)selectedRowInComponent:(NSInteger)component
With this method you can determine the selected row index for any of the components in your picker. you don’t have to rely on the values passed into the didSelectRow method.
- (void)pickerView:(UIPickerView *)thePickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
//get the value of the component that was changed
if(component == 0)
{
currency_value = [currency objectAtIndex:row];
}
else if(component == 1)
{
hundereds_value = [hundereds objectAtIndex:row];
}
else if(component == 2)
{
tens_value = [tens objectAtIndex:row];
}
else if(component == 3)
{
ones_value = [ones objectAtIndex:row];
}
else if(component == 4)
{
change_value = [cents objectAtIndex:row];
}
//some code here to save the values
}
- (void)pickerView:(UIPickerView *)thePickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
//get the values
NSInteger currency_NewRow = [thePickerView selectedRowInComponent:0];
NSInteger hundereds_NewRow = [thePickerView selectedRowInComponent:1];
NSInteger tens_NewRow = [thePickerView selectedRowInComponent:2];
NSInteger ones_NewRow = [thePickerView selectedRowInComponent:3];
NSInteger cents_NewRow = [thePickerView selectedRowInComponent:4];
//some code to save the values
}
Today I ran into a situation where I had forgotten the password to my development instance of phpBB 3. I was stuck in a situation were I needed to reset the password. I had full admin access to the database, so changing it there wouldn’t be the problem. The real problem is that phpBB uses its own password hashing, not MD5.
In a work around, I created a new user and used the password “123456” looking at the database , in the users table of the phpBB install. I saw the “user_password” field was “e10adc3949ba59abbe56e057f20f883e”.
I then changed my admin accounts user to the same string, “e10adc3949ba59abbe56e057f20f883e”.
I went to the phpBB login screen, fillled out my username, and entered the password “123456”… BINGO! it worked.
So to save you the work. You can follow what I did or just use these hashes to reset your own password:
Hash: e10adc3949ba59abbe56e057f20f883e
Password: 123456
Hash: $H$9Ae3Uk.ECdWW5ya13M4ErWhr4c.761/
Password: password
I hope this helps someone else out there.