Jump to content
Larry Ullman's Book Forums

Hermann

Members
  • Content Count

    29
  • Joined

  • Last visited

Community Reputation

3 Neutral

About Hermann

  • Rank
    Member
  1. I have found this exact version of what I need but I'm not sure how to convert it to my own extension of Yii. Please have a look http://www.marcofolio.net/webdesign/a_fancy_apple.com-style_search_suggestion.html Could anybody please help me?
  2. Hi there Larry. Yes...and for the life of me I can't seem to figure this one out on my own. Previous problems became easy after a while but not this one. Could you please give me some advice?? BTW thank you for all the wonderful work in light of the Yii framework. This is the first time that I'm using a framework and decided on Yii as I learned PHP from one of your books and decided if you like Yii then it must be worth my while and thus far it most certainly is!!
  3. I really don't know why this keeps happening. But here is the link to the code I used :http://www.yiiframework.com/wiki/361/simple-way-to-use-autocomplete-using-different-id-and-display-value/ I would like to achieve the following but I'm not sure how: 1)have a dropdown list with sizes (small, medium or large) which would be added to my SQL statement. I.e. if the dropdownlist is set to large then something like "....WHERE size='large'...." should be included in the SQL query. 2)If no records are found then I need to display a "Sorry, no results" message in the autocomplete list 3)If a record is found and selected by the user a button needs to be included to do a database search based upon the 'id' found in the above code.
  4. Hi everybody. I have used the following code from http://www.yiiframework.com/wiki/361/simple-way-to-use-autocomplete-using-different-id-and-display-value/ . My table name is dog and I'm interested in three attributes: id, name and size. So far I have gotten an autocomplete field working which is populated with the name-attribute values as typed in by the end user. The following code does that: Under extensions: <?php Yii::import("zii.widgets.jui.CJuiAutoComplete"); class myAutoComplete extends CJuiAutoComplete { /** * Run this widget. * This method registers necessary javascript and renders the needed HTML code. */ public function run() { list($name,$id)=$this->resolveNameID(); // Get ID Attribute of actual hidden field containing selected value $attr_id = get_class($this->model).'_'.$this->attribute; if(isset($this->htmlOptions['id'])) $id=$this->htmlOptions['id']; else $this->htmlOptions['id']=$id; if(isset($this->htmlOptions['name'])) $name=$this->htmlOptions['name']; if($this->hasModel()) { echo CHtml::textField($name,$this->value,$this->htmlOptions); echo CHtml::activeHiddenField($this->model, $this->attribute); }else { echo CHtml::textField($name,$this->value,$this->htmlOptions); CHtml::hiddenField($name,$this->value,$this->htmlOptions); } if($this->sourceUrl!==null) $this->options['source']=CHtml::normalizeUrl($this->sourceUrl); else $this->options['source']=$this->source; // Modify Focus Event to show label in text field instead of value if (!isset($this->options['focus'])) { $this->options['focus'] = 'js:function(event, ui) { $("#'.$id.'").val(ui.item.label); return false; }'; } if (!isset($this->options['select'])) { $this->options['select'] = 'js:function(event, ui) { $("#'.$id.'").val(ui.item.label); $("#'.$attr_id.'").val(ui.item.id); }'; } $options=CJavascript::encode($this->options); //$options = $this->options; $js = "jQuery('#{$id}').autocomplete($options);"; $cs = Yii::app()->getClientScript(); $cs->registerScript(__CLASS__.'#'.$id, $js); } } Under models: public static function usersAutoComplete($name='') { $sql= 'SELECT id ,name AS label FROM dog WHERE name LIKE :name'; $name = $name.'%'; return Yii::app()->db->createCommand($sql)->queryAll(true,array(':name'=>$name)); } Under Controllers: public function actionUsersAutocomplete() { $term = trim($_GET['term']) ; if($term !='') { $users = Users::usersAutoComplete($term); echo CJSON::encode($users); Yii::app()->end(); } } And finally under views: $this->widget('ext.myAutoComplete', array( 'model'=>$model, 'attribute'=>'user_id', 'name'=>'user_autocomplete', 'source'=>$this->createUrl('dog/usersAutoComplete'), 'options'=>array( 'minLength'=>'0', ), 'htmlOptions'=>array( 'style'=>'height:20px;', ), )); What I Want to achieve still is have a dropdown list with sizes (small, medium or large) which would be added to my SQL statement. I.e. if the dropdownlist is set to large then something like "....WHERE size='large'...." should be included in the SQL query. Im really not sure how to achieve this. If no records are found then I need to display a "Sorry, no results" message in the autocomplete list and finally if a record is found and selected by the user a button needs to be included to do a database search based upon the 'id' found in the above code. Thank you very much for your help!
  5. I also thought that it would be more useful to have a categories dropdown limiting these AJAX requests to only a certain category with the default value being all. Thus a search possible values will only come from one attribute but it will be limited by values indicated in another attribute. Ex. 1)Table (named dog) with Attributes: id, name, size and image (which is a path to image file) 2) model, and CRUD functionality generated via gii 3)User of site should be able to select between small, medium and large in the categories dropdown or all sizes will be scanned as a default 4)User of site will search via name and autocomplete will dropdown as in example in previous post (all the possible names with picture on the left) 5) User mouse-clicks on the item he wants and field gets filled with the name value. 6) User now mouse-clicks search and a table search is done with the key array value (this value should be the id attribute from the table represented by the specific name chosen) I hope this is a clear enough explanation. I have been able to achieve most of the other functionality of my practice site using Yii but this seems to be above my skill level. My next project would definetely be to maste jQuery and AJAX etc thus enhancing my Yii experience.
  6. Thank you very much for the reply. I have tried and failed the entire day (from South Africa so its nighttime over here already ). I have however found an extension that basically does what I want but I'm struggling with its implementation. The extension can be found here: http://www.yiiframework.com/extension/select2/#hh1 A demo can be found here: http://ivaynberg.github.com/select2/#documentation Please see the " Loading Remote Data" section. Now this is exactly what I want to do. With the images etc. I just want to add a search button that would allow users to navigate to a specific unit or category. According to Documentation this code needs to be entered into view file in order to use data from model: // Working with model <?php $this->widget('ext.select2.ESelect2',array( 'model'=>$model, 'attribute'=>'attrName', 'data'=>array( 0=>'Nol', 1=>'Satu', 2=>'Dua', ), ); The problem is (as mentioned earlier) My AJAX skills aren't on standard at all. I would thus really appreciate some help on executing an AJAX request whilst user is typing into the specific field i.e. returning key:value array to above data parameter via AJAX. I assume this will be done in the specific controller? Thank you for the help thus far!
  7. Hi there everybody. I've only recently started experimenting with the Yii framework and must say that I'm loving it. I have however run into a slight dilemma which I just can't seem to figure out. I must admit that my javascript (jQuery) knowledge probably isn't up to scratch but I would prefer to make that part of my Yii learning experience. The problem: I want to include a search function in my header. This search function should behave exactly the same as a normal autocomplete dropdown textbox and the values of autocomplete options should be retrieved from two or more MYSQL database attributes (In this case I want to have a table for products and this one autocomplete field should enable me to search for product matches via the product_name, product_serial and product_color attributes). I have gotten a database search to work from http://www.yiiframework.com/extension/database-live-search/ . This however does a render_partial to a part of the page whereas I want possible results to be represented as a dropdown list. I would like to be able to click on one of the dropdown options and then go to this option specific page content after clicking a search button or perhaps if clicking on the specific option. To illustrate, I want to build a search box similar to that of Amazon.com (now by no means am I aiming to build such a complex underlying system, but this is just the idea of what I want to do.) http://www.amazon.com/ Now like I said I want to search through multiple attributes within my table. Ex. if I search for the string 'red', product_name, product_serial and product_color attributes will be scanned for all of the entries containing this string and if none are found the a sorry message will be displayed on the dropdown. BTW. I'm mainly using the Bootstrap extension for my UI if it might be of any additional help in building a typeahead or search form. Please if anybody can give me some assistance or point me to some sort of tutorial that will help me master this problem as it is really important to me to be able to do it exactly in the way described above! Thank you very much in advance. Kind regards, Hermann.
  8. Hermann

    Login Error

    Oh and I would really like to keep the hash_hmac function as is seeing as my other site uses this exact function and I would prefer to not have to ask users to re-enter their passwords when I eventually use this Yii based site instead of the old procedural programmed site.
  9. Hermann

    Login Error

    Hi there you guys. I have recently started using Yii and I am trying to develop a couple of basic systems one of which is a simple registration and login system. I have succeeded in the registration form and entering data into the database(MYSQL). I used a hash_hmac function in a similar fashion used in Effortless e-commerce, also written by Larry. A encrypted form of the password does show up in my database so I'm guessing this is not the problem. I'm using the agile application development using Yii book as a learning aid and did the login system according to this. I have however named my database table columns differently and suspect that this might perhaps be the problem but don't know how to fix it. (I suspect that it's quite similar to Larry's login example where he assigns the email to username rather than email in order not to upset the rest of the framework). Or perhaps it's a problem with the encrypt method. I really don't know if anybody could please help me I would be really grateful! Here is my encryption function as located in my Users AR class: /** * perform one-way encryption on the user_password before we store it in the database */ protected function afterValidate() { parent::afterValidate(); $this->user_password = $this->encrypt($this->user_password); } public function encrypt($value) { return hash_hmac('sha256', $value, 'c#haRl891', true); } The UserIdentity class is as follows: <?php /** * UserIdentity represents the data needed to identity a user. * It contains the authentication method that checks if the provided * data can identify the user. */ class UserIdentity extends CUserIdentity { private $_id; /** * Authenticates a user using the User data model. *Reference to username instead in order not to upset framework * @return boolean whether authentication succeeds. */ public function authenticate() { $user=Users::model()->findByAttributes(array('user_email'=>$this->username)); if($user===null) { $this->errorCode=self::ERROR_USERNAME_INVALID; } else { if($user->user_password!==$user->encrypt($this->user_password)) { $this->errorCode=self::ERROR_PASSWORD_INVALID; } else { //Session variables set here accesed like this anywhere in app:Yii::app()->user->userEmail; $this->_id = $user->user_id; $user_email = ($user->user_email); $this->setState('userEmail', $user_email); $this->errorCode=self::ERROR_NONE; } } return !$this->errorCode; } public function getId() { return $this->_id; } } The login class is as follows: <?php /** * UserIdentity represents the data needed to identity a user. * It contains the authentication method that checks if the provided * data can identify the user. */ class UserIdentity extends CUserIdentity { private $_id; /** * Authenticates a user using the User data model. * Word username genoem om nie die res van die framework te affekteer nie * @return boolean whether authentication succeeds. */ public function authenticate() { $user=Users::model()->findByAttributes(array('user_email'=>$this->username)); if($user===null) { $this->errorCode=self::ERROR_USERNAME_INVALID; } else { if($user->user_password!==$user->encrypt($this->user_password)) { $this->errorCode=self::ERROR_PASSWORD_INVALID; } else { //Session variables set here accesed like this anywhere in app:Yii::app()->user->userEmail; $this->_id = $user->user_id; $user_email = ($user->user_email);$this->setState('userEmail', $user_email); $this->errorCode=self::ERROR_NONE; } } return !$this->errorCode; } public function getId() { return $this->_id; } } The LoginForm class is as follows: <?php /** * LoginForm class. * LoginForm is the data structure for keeping * user login form data. It is used by the 'login' action of 'SiteController'. */ class LoginForm extends CFormModel { public $user_email; public $user_password;private $_identity; /** * Declares the validation rules. * The rules state that username and user_password are required, * and user_password needs to be authenticated. */ public function rules() { return array( // username and user_password are required array('user_email, user_password', 'required'), /* // rememberMe needs to be a boolean array('rememberMe', 'boolean'), */ // user_password needs to be authenticated array('user_password', 'authenticate'), ); }/** * Declares attribute labels. */ public function attributeLabels() { return array( 'user_email'=>'Email', 'user_password'=>'Password', ); } /** * Authenticates the user_password. * This is the 'authenticate' validator as declared in rules(). */ public function authenticate($attribute,$params) { if(!$this->hasErrors()) { $this->_identity=new UserIdentity($this->user_email,$this->user_password); if(!$this->_identity->authenticate()) $this->addError('user_password','Incorrect username or Password.'); } }/** * Logs in the user using the given username and user_password in the model. * @return boolean whether login is successful */ public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->user_email,$this->user_password); $this->_identity->authenticate(); } } } Trying to log in results in the following error: Property "Users.user_password" is not defined. C:\wamp\www\framework\db\ar\CActiveRecord.php(144) 132 */ 133 public function __get($name) 134 { 135 if(isset($this->_attributes[$name])) 136 return $this->_attributes[$name]; 137 else if(isset($this->getMetaData()->columns[$name])) 138 return null; 139 else if(isset($this->_related[$name])) 140 return $this->_related[$name]; 141 else if(isset($this->getMetaData()->relations[$name])) 142 return $this->getRelated($name); 143 else 144 return parent::__get($name); 145 } 146 147 /** 148 * PHP setter magic method. 149 * This method is overridden so that AR attributes can be accessed like properties. 150 * @param string $name property name 151 * @param mixed $value property value 152 */ 153 public function __set($name,$value) 154 { 155 if($this->setAttribute($name,$value)===false) As You should be able to see, my column for passwords in the users table, is not named password but instead user_password. I would like to keep it this way. As pointed out earlier, I think that either this or the my encrypt function might be the problem. Any help would really be greatly appreciated!! Regards, Hermann (Yii Newbie!!!)
  10. Thank you very much Larry. I asked my web host to please enable output buffering and all problems solved!!! Really grateful.
  11. Another thing that might help, is that this problem arises on all of my pages. So the problem is not on my index page. I've really tried everything oncluding moving session_start to the beginning of different files, checking for whitespace in files etc.
  12. I have tried this but doesn't seem to me like I'm getting it to work. I have done this interesting experiment: I've placed an echo statement at the beginning of my config file as well as next to my session_start function. The "Here config starts" is above the error while the "here session starts"is echoed just beneath the error. when I moved the session to above the"Here config starts, both the session message and config start message moved to below the error message. Don't know if this makes more sense to somebody else than it does to me!
  13. I have placed session_start at the very start of my config file as well but no luck. On index.php line one there is no whitespace and the only thing there is my opening php tag. I have tried to remove session_start from my config file and place it at the very top of my page specific script but still no luck. Is there a way that.
  14. Hi there guys and gals. I'm busy uploading my site and during the development process I've relied quite heavily on Larry's book. I do seem to have a problem now that it is uploaded however. It gives me these errors: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /public_html/index.php:1) in /public_html/mvc/controller/system/config.php on line 54 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /public_html/index.php:1) in /public_html/mvc/controller/system/config.php on line 54 I have looked on Google but the only solution I'm able to find is that something is being sent to the browser before the session is started. I assume that this might be the solution but for the life of me I'm not able to determine where the problem is. session_start is in my config file just like Larry did it. I have however built my own page construct function to easily put together all of my pages consisting out of a couple of includes. The first thing that this function does is require my config file (so this is also according to my knowledge the first thing done on the page). My site worked perfectly on a local server and didn't have this problem but now that I'm going live I suddenly encounter this. None of the includes on the page incllude the config file on their own so it really is only included once. I would really appreciate it if somebody could just give me some help here or just maybe point me in the right direction? Thank you in advance, Hermann. 
  15. I've struggled with this as well in the beginning. I've decided to leave it out of my code. Remember this book is only a guide and you should use whatever you're comfortable with. I've found that I'm more comfortable with $_SERVER['DOCUMENT_ROOT'], which equals for example www.myweb.com/ . Another piece of advice that I can give from my little experience, is to never forget the 'echo' command. It can tell you a lot. for example, if you want to know why your constants aren't working, display the value of it in the browser using echo and you'll solve this problem really quickly or at least that's how I learned how to use constants similar to the ones mentioned.
×
×
  • Create New...