Jump to content

dgruetter

Members
  • Posts

    16
  • Joined

  • Last visited

    Never

Everything posted by dgruetter

  1. I was looking into using dependency injections as I read a great deal of issues with using global. I will most likely be using this method. Thanks for the replies!
  2. Hi, I am trying to find the best location to instantiate a new object in my code. My old method was to instantiate a new object at the bottom of the class declaration. class Messenger () { //class structure }//Close class $messenger = new Messenger(); I just built an initialize script to simplify all the requires. It also has an autoload function for classes like messenger that might not need to be required and instantiated on every page function __autoload($class_name) { global $MODELS_DIR; require ($MODELS_DIR . $class_name . '.php'); } The problem is that if I instantiate a new class Messenger() at the end of my class declaration, it will never autoload... because I am not calling for a new Class() in my code. So should I continue to instantiate at the end of the class declaration of always instantiate it in my code? Not sure if it's worth mentioning but I plan on moving my code to a MVC framework in the future. Oh yeah, I am also using PHP 5.29 ... I think
  3. Thanks for the reply. I was actually going to include a type column, I forgot to add that. It makes sense. My question still remains on whether I should store the attributes unique to vendor in another table using the user id as a foreign key. Currently I have a foreign key in the vendor table called vendor_id (to be used for joins later in the game). But... I also have a field simply called id. This id field is the vendor tables primary id field. I was wonder if this was even needed or if I should just use the foreign key for indexing and sorting. I guess it doesn't hurt to have that extra primary key. I really need to brush up on my MYSQL skills.
  4. Hi. I am trying to restructure my PHP code into oop structure and now have developed an extended class called "vendor_user" from a base class "user." The basic attributes in "User" will be id, email, password. The extended class "vendor_user" will have vendor_id, info, phone_num, tagline, etc.. I was going to create two tables, one called "user" and one called "user_vendor." Eventually I plan on adding another extended class "guest_user" with different attributes... The way I was planning on entering vendors would be to first enter the vendor as a user and the take the id of the user table and do another insert into vendor_user using the "id" from user as a foreign key. I have a few questions. First, Would this be the best way to do this? My logic is all vendor_users would be users but not all users would be vendor_users. Second, should I create a primary key on user_vendors (another id attribute perhaps) or should I just index the forign key vendor_id? Any insights would be greatly appreciated.
  5. I am trying to write a more elegant solution to be able to check an object, find only the properties in an object that have values, and update those values in a database with a matching ID. I am extending a class from a generic user class here is an example of my code <?php require_once('User.php'); require_once('Database.php'); class Vendor extends User { protected static $vendor_attributes = array('id', 'nothing' , 'tagline' , 'bio'); protected static $table_name = "user_vendors"; public $tagline; }//close class ?>
  6. You could also take out the space on line 13 too header("location:login.php"); 11 } 12 ?> 13<?php 14
  7. Hi, I have been trying to improve my PHP programming skills by following the Lynda.com Beyond the basics tutorial and they came to a section where they were building common database methods inside a database class. Since they did not have PHP 5.3 (Where late static bindings is introduced) they had to place code referring to self:: inside of each class that needs this functionality. It seems that not having late static binding was binding self to the database class and not to other classes that call it statically. I will insert some code to explain this better. Long story short, I was really interesting in getting an attributes function working and as I do not have late static bindg either, I cannot figure out how to call it from inside a class to make it function correctly. Here is some code. Below is the attribute class they wrote. Each class declared an array of attributes for each of the classes that want to call this method like protected static $db_fields = array('id', 'email', 'password', 'etc'); protected function attributes() { // return an array of attribute names and their values $attributes = array(); foreach(self::$db_fields as $field) { if(property_exists($this, $field)) { $attributes[$field] = $this->$field; } } return $attributes; } They planned on moving this code to a DatabaseObject class after they get LSB but until then, have to include it in every class that wants use this. Is there any way to write this as a function, include it in a class, and call the method without late static binding.. or even place this inside a database class and call it from another class without LSB? Forgive me if I am not explaining this correctly, again, I am still learning. My version of PHP is 5.2.6. 17392_.php 17393_.php
  8. These are some great ideas. I think the concept of having the vendor and vendorsearch as two different classes. Maybe I can even expand on the vendorsearch class and make it abstract too. Since people can search for more than just vendors (for other brides for example). Maybe I can have just a search class and extend it for vendors, brides, articles, etc. Or.. maybe I don't need the vendor class at all. You say that OOP might be overkill for my needs, perhaps this is true. I have the foundation of the site already written in mostly procedural code. The problem is that moving forward has been very challenging. Since it contains a lot of if/then/else code it makes even the simplest changes affects the entire site. This is where I hoped to benefit from OOP. I was hoping to cut some of the spaghetti code and make it more elegant. Lots to think about. Thanks for the reply. These ideas will be extremely valuable when weighing out options for my re-write.
  9. Hi, I was wondering if anyone would be able to give me some ideas on how I should structure the classes for a website I am re writing (this time in OOP). The website is a bridal website where brides can register an account and search for vendors by category, regional area and various other ways. They can interact with the vendors by bookmarking them, writing reviews, etc. The vendors can register and also have actions they can perform. I was planning on writing an abstract class "user" and extending two classes from it, "vendor" and "bride". The commonalities would be things like "name" "email" etc. My question is how to present the search functionality. Should I have a class called vendor search that lists the vendors found by the MySQL query? Would this make logical sense? If so should the vendor class be responsible for displaying it's information (example vendor->DisplayListing()) or should the search object do this (vendorsearch->displayVendors)? I have a good understanding of classes but thinking through the logic of who does what always trips me up. There are more classes I was considering like "admin" and "geography" assuming it makes sense to do this. Any ideas would be much appreciated. P.S. the website is here: http://www.thebridalvine.com/vendors.php I plan on to set up
  10. Thanks! I substituted HAVING instead of WHERE and it also worked. Does this save on resources? I am still using the great circle formula but I am trying to find a better one. Any suggestions? $sql = "SELECT Latitude, Longitude, City, (((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) AS distance FROM `Cities` HAVING distance <= 5";
  11. I did some research and found "You can't use aliases in WHERE conditions, because WHERE clause evaluation precedes the aliases evaluation. " This makes sense. So I changed the query (took out join because I realized I didn't even need it) to: Got a bunch of results. Seems to be working.
  12. Hey, I was wondering if someone can help me with a query I am trying to build. I want to list all nearby cities within a 100 mile radius. I am using a set of tables that are joined and I am trying to calculate the distance inside the query. I am having trouble with the syntax and I am not sure how to reference the fields being calculated. I have something like this: $sql = "SELECT C.Latitude, C.Longitude, C.City, Co.Country, (((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) as distance FROM Cities C JOIN Countries Co ON Co.CountryId = C.CountryId WHERE distance <= 100" Do I reference the fields by their aliases in the calculation? A thousand bows if someone can help me.
  13. Thank you very much for the information. IT was very helpful.
  14. I must admit, I do not know what this is used for. I just started oop with php and know what a -> does, but I see => being used all the time and I cannot find out it's exact meaning. Can someone shed a little light on this? Thanks.
  15. Hi, I am new to this board. I have a problem when inserting text into a mysql database from a text box. When the test gets inserted into the table It is turning any Microsoft quotes into junk characters. I have created a few helper functions: clean_string, mysql_prep to prepare the clean and prepare the code to be inserted but I am afraid It might be hurting rather than fixing it. Here is a snippit: <?php print "<input name=\"vendor_info\" type=\"hidden\" value=\"".htmlspecialchars($vendor_info)."\">"; ?> function mysql_prep( $value ) { $magic_quotes_active = get_magic_quotes_gpc(); $new_enough_php = function_exists( "mysql_real_escape_string" ); if ( $new_enough_php ) { //undo effects of magic quotes $value = mysql_real_escape_string( $value ); } else { //If magic quotesw are not active add slashes manually if (!$magic_quotes_active) { $value = addslashes( $value ); } } return $value; } function clean_string ($str) { $str = str_replace(chr(130), ',', $str); // baseline single quote $str = str_replace(chr(131), 'NLG', $str); // florin $str = str_replace(chr(132), '"', $str); // baseline double quote $str = str_replace(chr(133), '...', $str); // ellipsis $str = str_replace(chr(134), '**', $str); // dagger (a second footnote) $str = str_replace(chr(135), '***', $str); // double dagger (a third footnote) $str = str_replace(chr(136), '^', $str); // circumflex accent $str = str_replace(chr(137), 'o/oo', $str); // permile $str = str_replace(chr(138), 'Sh', $str); // S Hacek $str = str_replace(chr(139), '<', $str); // left single guillemet $str = str_replace(chr(140), 'OE', $str); // OE ligature $str = str_replace(chr(145), "'", $str); // left single quote $str = str_replace(chr(146), "'", $str); // right single quote $str = str_replace(chr(147), '"', $str); // left double quote $str = str_replace(chr(148), '"', $str); // right double quote $str = str_replace(chr(149), '-', $str); // bullet $str = str_replace(chr(150), '-', $str); // endash $str = str_replace(chr(151), '--', $str); // emdash $str = str_replace(chr(152), '~', $str); // tilde accent $str = str_replace(chr(153), '(TM)', $str); // trademark ligature $str = str_replace(chr(154), 'sh', $str); // s Hacek $str = str_replace(chr(155), '>', $str); // right single guillemet $str = str_replace(chr(156), 'oe', $str); // oe ligature $str = str_replace(chr(159), 'Y', $str); // Y Dieresis return $str; } $vendor_info = clean_string($_POST['vendor_info']); $vendor_info = mysql_prep($_POST['vendor_info']); $sql = "INSERT INTO $table_name ( vendor_id..... Please be gentle, I am still learning PHP and I am far from an expert. Thanks for any help!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.