-
w1sh posted...
Thanks for these Shawn, they are really helpful.
-
tsai posted...
nice tutorials, thanks you , Shwan! :)
-
slingle posted...
I have a question.
How would you go about handling conditional where statements with the structure you have in your models.
For Example: if you added a 'joinDate' column to your user table.
Would you have to make your function look like this?
if(isset($options['joindate >']))
$this->db->where('joindate >',$options['joindate >');
if(isset($options['joindate db->where('joindate <',$options['joindate =']))
$this->db->where('joindate >=',$options['joindate >=');
if(isset($options['joindate db->where('joindate >',$options['joindate <=');
That seems like a bad way. I love the way you are constructing your methods so i thought i would ask your opinion on going about this.
Thanks
-
samcasbon posted...
Thanks for the great tutorials. Would you mind also posting the code from this video. I am just beginning to learn PHP and Codeigniter and am fairly certain that there is something wrong with my code.
Thanks,
Sam
-
samcasbon posted...
@ Shawn
I didn't notice that KRD already asked for the code. didn't mean to be a pain thanks for the response
-
jyoseph posted...
Really really really appreciate the work you've put into this. Very easy to follow instructions. The CodeIgniter community should be thanking you. I am just now getting into learning a framework and chose CodeIgniter over similar frameworks because of your clear tutorials.
-
jerryLee posted...
I have to tell you, I started teaching myself html about 10 years ago, then on to php, etc. The problem with self teaching is you miss a lot, and waste a lot of time. I learned more here in one night about what I really want to be doing then I have in years. I know that sounds crazy, but thank you. I will follow all of your tutorials, your teaching mode is above and beyond. Do you have any other recommendations for someone like me? I know some php, and a lot of html/css, but I love programming. I just want more of what you are doing here. Most of the zend and other tutorials are just way beyond my understanding at this point.
Anyway, thank you, and kudos!
-
darkllangle posted...
wow man ^^, a great and informative tutorial. You've made me think differently towards writing PHP codes and functions.
Keep it up (**********)
-
ali oygur posted...
why you don't use CI form validation class on the validations proccess ?
-
ali oygur posted...
ok. Im sorry :)
-
Zumry posted...
Since i am a self starter....i just wanted to know how to do code writing part....its really amazing....u maxaaa
-
Xero posted...
Hey shawn, forgive the noob question but what IDE are you using in this video?
-
Vemula posted...
Very good tutorial and presentation style.... Thanks Shawn.
Looking forward for your next screencast
-
Tom Haws posted...
If you'll forgive my quibbling, I have a couple of questions:
1. Wouldn't it be good to omit the closing php tag ?> as specified in the CodeIgniter guide?
2. Isn't it best to use the base (some might say "uninflected" or "unconjugated") noun form for database tables?
-
Steve posted...
This is a great tutorial. Much more professional code than any of the other CI membership tutorials I've found yet. I like your programming style, very similar to my own.
I'm looking forward to the next episode in this series.
-
Stas posted...
I use free PHP IDE - Codelobster PHP Edition with special CodeIgniter plug-in.
-
Shawn McCool posted...
Yes, Camtasia Studio.
-
Shawn McCool posted...
I'm asking about the code that actually gives you the error. The table library line 269. Can you post the method of which line 269 is a part?
-
Shawn McCool posted...
I use Zend Studio 7. In the videos I am using Zend Studio 5.5.
-
Shawn McCool posted...
I really should provide the code, shouldn't I? I'll upload the code for part 3, and then upload the code for every subsequent part. Thanks for the feedback. If you can think of anything else that'd help me make these screencasts better please let me know!
-
Shawn McCool posted...
@tom
1) yes
2) I imagine that someone could make a case for any convention, as long as there indeed was a convention used. However, most conventions use the plural form of the datatype for the simple reason that it's what it's called. You wouldn't say, "add them to the user table." You say, "add them to the users table."
I've also since switched to using Luke Baker's ORM Active Record implementation for PHP instead of CodeIgniter's and it uses Ruby on Rails naming conventions which I'm very happy with.
-
Shawn McCool posted...
@slingle
I generally use option parameters like "joinDateStartRange" and "joinDateEngRange".
-
Shawn McCool posted...
@Xero
I'm using Zend Studio 7 in these videos. I've recently switched to NuSphere PHPed.
-
Shawn McCool posted...
@Sam hey man, no problem. Comments on blogs are hard to follow. I'm thinking about ditching WordPress in favor of maybe Expression Engine 2.0 so that I can really make the site more fit my vision. Unfortunately, that would mean that I'd need to put in quite a bit of work that would result in fewer new tutorials. So, instead I'll make do with what I have.
-
Shawn McCool posted...
@Sam
Yes, I should have been doing this from the beginning. I'm going to include the code in the "part 3" post soon, and continue to include it for every post after that.
-
Shawn McCool posted...
@Jason
Perhaps you can show the "new" GetUsers() method source?
-
Shawn McCool posted...
@Jason
Oh, ok right... Ok.. so what's happening here is GetUsers is returning an array of Objects. You need to format this into an array or create a custom option for GetUsers to return $result_array().
Basically, get back the data from GetUsers, then loop through it and format it into a new array so that you have more control over the actual layout.
Alternatively you can create a parameter $options['returnFormat'] = 'array';
if(isset($options['returnFormat']) && $options['returnFormat'] == 'array') return $query->result_array();
You have a lot of options based on what your needs are.
Do a print_r() on the results from GetUsers. It shows that it is returning objects. You're directly passing them to the table library, this is your problem.
-
Shawn McCool posted...
@Gerry
Actually, I just started using Expression Engine. My first experience with it is the EE 2.0 public beta. I'm still learning and am a good bit away from having best practice worthy experience with it.
I can definitely see that people are interested in learning about EE / CI integration. I myself am interested in it, as well.
-
Shawn McCool posted...
@Adrian
Thanks Adrian.
I'm going to go ahead and paste the contents of a common helper file that I bring from project to project with me. It has been updated many times since I last posted this code.
function notEmptyArray($array)
{
if(isset($array) && is_array($array) && count($array) > 0)
return true;
else
return false;
}
/**
* required method accepts an array of keys and returns false if any of those keys are not present in the data array
*
* @param array $required
* @param array $data
* @return bool
*/
function RequiredValues($required, $data)
{
foreach($required as $field) if(!isset($data[$field])) return false;
return true;
}
/**
* default method accepts an associated array of key => default value pairs then returns a new array containing the default values should they have not been declared in the $options array.
*
* @param array $defaults
* @param array $options
* @return array
*/
function DefaultValues($defaults, $options)
{
return array_merge($defaults, $options);
}
/**
* parseKeys method accepts a list of array keys and an array then returns a new array containing only matching keys
*
* @param array $values
* @param array $data
* @return array
*/
function ParseKeys($values, $data)
{
if(!is_array($values)) return false;
foreach($values as $index => $value)
{
$values[$index] = strtolower($value);
}
$newArray = array();
foreach($data as $key => $value)
{
if(in_array(strtolower($key), $values)) $newArray[$key] = $value;
}
return $newArray;
}
/**
* generateSlug creates a delimited slug
*
* @param string $original
* @return string
*/
function GenerateSlug($original, $delimiter = '-')
{
$slug = strtolower($original);
$slug = trim($slug);
$slug = preg_replace("/\s/", $delimiter, $slug);
$slug = preg_replace('/\W/', $delimiter, $slug);
$slug = str_replace('_', $delimiter, $slug);
$slug = preg_replace("/($delimiter)+/", $delimiter, $slug);
return $slug;
}
/**
* MysqlDate formats any date string to a mysql datetime format
*
* @param string $dateString
* @param bool $timestamp Pass true if datestring is a unix timestamp
* @return string
*/
function MysqlDate($dateString = false, $timestamp = false)
{
if($dateString && $timestamp) return date("Y-m-d H:i:s", $dateString);
else if($dateString) return date("Y-m-d H:i:s", strtotime($dateString));
return date("Y-m-d H:i:s");
}
/**
* DropDownOptions function generates an associative array from the passed array of objects.
* Using the key and value names specified in the 2nd and 3rd parameters.
*
* This method could use some polish.
*
* @param array $array
* @param string $key
* @param string $value
* @param string $emptyOptionText
*/
function DropDownOptions($array, $key, $value = false, $emptyOptionText = "-- Choose One --", $excludeArray = false, $excludeKey = false)
{
$retval = array();
// check if empty option text is set, then use it
if(!empty($emptyOptionText)) $retval[''] = $emptyOptionText;
// check to see if we're using an exclude array
if($excludeArray && $excludeKey)
{
$excludeArray = DropDownOptions($excludeArray, $excludeKey, '', '');
print_r($excludeArray);
die();
}
if(is_array($array)) foreach($array as $element)
{
if(!empty($value))
$retval[$element->$key] = $element->$value;
else $retval[] = $element->$key;
}
return $retval;
}
/**
* CurlRequest method queries a URI and returns the response
* CurlRequest(array('uri' => $uri));
*
* Option: Values
* --------------
* uri uri to query
*
* @param array $options
* @return string
*/
function CurlRequest($options = array())
{
$CI =& get_instance();
// required values
if(!RequiredValues(array('uri'), $options)) return false;
$ch = curl_init($options['uri']);
curl_setopt_array($ch, array(
CURLOPT_HEADER=>FALSE,
CURLOPT_RETURNTRANSFER=>TRUE,
CURLOPT_USERAGENT => 'BS'
));
$response = curl_exec($ch);
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
return $response;
}
function GetUniqueFilename($path, $filename)
{
$testPath = $path . $filename;
list($name, $ext) = explode('.', $filename);
$i=0;
while(file_exists($testPath))
{
$testPath = $path . $name . '_' . $i . '.' . $ext;
$i++;
}
return $name . '_' . $i . '.' . $ext;
}
function linkify($text)
{
$ret = ' ' . $text;
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1\\2'", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1\\2'", $ret);
$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1\\2@\\3", $ret);
$ret = substr($ret, 1);
return($ret);
}
-
Shawn McCool posted...
It should be in a base_model class that itself extends codeigniter's model. Please not that this tutorial was made in 2009. =)
-
Santos posted...
Great tutorial. I must agree with Steve and Drazen, this is the most professional code I have ever seen in a PHP tutorial.
I learned a whole lot about OOP and MVC from this clip. Many thanks!
-
Panos posted...
Hi Shawn,
Thanks alot for the CI introduction screencasts they're really helpful :)
(I think that the getUsers function should be looking for the md5'd userPassword (?) maybe you can include that in the errata list).
P.
-
Neil posted...
Hey Shawn,
Great tutorial. There aren't many decent CodeIgniter screencasts around, so I was pleased to find yours.
Perhaps an idea for a future tutorial/screencast... i'm interested to know how a simple form such as a newsletter signup/site login could be included on multiple pages without needing to go to separate pages to display error or success messages.
Thanks,
Neil
-
Michael posted...
Hi Shawn,
First, really high quality stuff.
Second, have kinda conceptual design question...
The _required() method... as U mention it's a utility method, but even thought, shouldn't this call be the part of the Login controller? It already does some of validation stuff... so why not consolidate whole validations steps on one single place?
Thnx.
-
Martin posted...
I don't want to nit-pick, but shouldn't the setting of the userPassword entry be to md5($options['userPassword]')? You left it as md5($options['userEmail]') in the screencast.
-
Karter posted...
shouldn't this line:
$this->_default(array('userStatus', 'active'),$options);
be:
$options = $this->_default(array('userStatus'=>'active'),$options);
given that $options is an associative array?
-
KRD posted...
Shawn,
Thanks much for the tutorial, I'm just getting into PHP and using Code Igniter so it's great that people like you take the time to create these screencasts. I am curious, however, do you provide the code from the screencast anywhere or should I just take the video really slow and copy it?
Thanks
Kris
-
JonyR posted...
Hey Shawn, excellent tutorial.
What's php ide do you use in this tutorial?
Thanks in advance,
Jony
-
Jason posted...
well... great resource here, but I just typed out a lengthy response/question and it told me I was spammy. I hit the back button and my response is not here.
Quick version: I can't get your GetUsers method to work.
http://60dayflip.com/main/listing2/
http://pastie.org/796499
-
Jason posted...
That's the CodeIgniter method. System/libraries/Table.php
-
Jason posted...
Thanks! I'm new to OOP PHP, and really only know enough PHP to be dangerous, lol. But I'm developing a new membership site with unique accounts and I didn't want to do another procedural app like I've done in the past.
I'll work on your suggestions once the Apple presentation is over. :D
-
Jason posted...
Shawn, it is the same as listing2, but swapped out User_Model for MUser.
I've updated the pastie link with both.
http://pastie.org/796499
-
Jason posted...
It works here: http://60dayflip.com/main/listing/
...using the following GetUsers method.
function GetUsers()
{
return $this->db->get('users');
}
-
Gerry posted...
Hi Shawn,
Thanks for the screencast, It's great to hear that you also work with ExpressionEngine. If you have some time I would like to see some introduction on how integrating CodeIgniter to ExpressionEngine like maybe making some small application, like read and write to database.
Thanks
Gery
-
Foxdith posted...
Great Tutorial Thank You
-
Drazen Mokic posted...
Really good job. This is one of the most professional CI tutorials out in the web (i guess i watched them all :) ).
Keep going, please! ;)
-
Dex Barrett posted...
Which software do you use to record your tutorials? Isn't it Camtasia Studio?
-
Adrian posted...
Hi Shawn,
Thanks for the great tutorials. I've change my programming style to use $options array where is possible and it really help me.
P.S You can change _required method like this:
function _required($required, $data) {
foreach($required as $key)
if(!array_key_exists($key, $data)) return false;
return true;
}