Jump to content

Lamez

Members
  • Posts

    1,686
  • Joined

  • Last visited

    Never

Everything posted by Lamez

  1. Okay I revised my function, just in case someone else needs it, here is what I have: <?php define("FILE", "config/review_setup.txt"); function fix($var){ $var = strtolower($var); return str_replace(" ", "_", $var); } /*function findValue($var, $file = FILE){ $var = fix($var); $val = NULL; $fh = fopen(FILE, "r"); while($string = trim(fgets($fh))){ $i = stripos($string, "="); $f_var = str_replace(" ", "", substr($string, 0, $i) ); if($f_var == $var){ $val = substr($string, $i+1, strlen($string)-1); $val = str_replace('"', "", $val); break; } } fclose($fh); return trim($val); }*/ function findValue($var, $file = FILE){ $val = parse_ini_file($file); return $val[fix($var)]; } echo findValue("host"); echo findValue("port"); echo findValue("username"); ?>
  2. Thank you guys so much. Once I get this working well. I will go back and use that function.
  3. Okay, I have been playing around with it, this is very strange to me. This seems like a logical error, but I cannot locate it. Just a reminder, here is the code: <?php define("FILE", "config/review_setup.txt"); function fix($var){ $var = strtolower($var); return str_replace(" ", "_", $var); } function findValue($var, $file = FILE){ $var = fix($var); $val = NULL; $fh = fopen(FILE, "r"); while($string = fgets($fh)){ $i = stripos($string, "="); $f_var = str_replace(" ", "", substr($string, 0, $i) ); if($f_var == $var){ $val = substr($string, $i+1, strlen($string)-1); $val = str_replace('"', "", $val); break; } } fclose($fh); return $val; } echo findValue("port"); echo "?"; echo findValue("host"); echo findValue("robot name"); ?> Notice the echo statements. Now here is the output of those echo statements: Why in the world is it doing this?
  4. I noticed in my OP that I forgot some code. Just a line or two, here it is amended. <?php define("FILE", "config/review_setup.txt"); function fix($var){ $var = strtolower($var); return str_replace(" ", "_", $var); } function findValue($var, $file = FILE){ $var = fix($var); $val = NULL; $fh = fopen(FILE, "r"); while(!feof($fh)){ $string = fgetss($fh); $i = stripos($string, "="); $f_var = str_replace(" ", "", substr($string, 0, $i) ); if($f_var == $var){ $val = str_replace("\"", "", substr($string, $i+1, strlen($string)) ); $val = str_replace(" ", "", $val); break; } } if(is_null($val)) $val = "DNE"; fclose($fh); return $val; } echo findValue("host"); echo findValue("port"); ?>
  5. My Code is suppose to parse a text file and return the value that is attached to the variable. However when I try calling two different variables, one after the next, it adds a space. Here is the code: <?php define("FILE", "config/review_setup.txt"); function fix($var){ $var = strtolower($var); return str_replace(" ", "_", $var); } function findValue($var, $file = FILE){ $var = fix($var); $val = NULL; $fh = fopen(FILE, "r"); while(!feof($fh)){ $string = fgetss($fh); $i = stripos($string, "="); $f_var = str_replace(" ", "", substr($string, 0, $i) ); if($f_var == $var){ $val = str_replace("\"", "", substr($string, $i+1, strlen($string)) ); $val = str_replace(" ", "", $val); break; } } if(is_null($val)) $val = "DNE"; fclose($fh); } echo findValue("host"); echo findValue("port"); ?> Output of above: if I just do host, I get this: Any thoughts?
  6. Looks like I was wrong, again.
  7. To private buyers. Posting it here would not do anything. I don't think there is any really good websites out there that have this functionality. Maybe Ebay? LOL!
  8. At first I was like, who is TLG, so then I was like: http://www.abbreviations.com/TLG But now I'm like, oh The Little Guy.
  9. Most SATA cables sit on the drive one way. I bet resetting the BIOS did the trick.
  10. So every e-mail you send (with Gmail, Hotmail, Outlook, ..) is a database? From what I understand. Yes (In this context of course). Class database is the parent, and email is the child. Every child is apart of their parent?
  11. Yes, email is-a database. My email class is also a database class, in a nutshell? So I now have this: <?php require_once("pear/Mail.php"); class Email extends Database{ var $from_name, $from_email, $to_name, $to_email, $subject, $body, $host, $port, $username, $password; private $db = NULL; //$db will is a object? function __construct(Database $db){ $this->db = $db; } function dbEmail($id, $name, $email, $subject, $body){ $q = $this->db->select(TBL_SMTP, "*", "id='".$id."'"); if($q->numRows > 0){//I don't think this is correct. $f = $q->fetchRow(); $this->from_name = $f['name']; $this->from_email = $f['email']; $this->username = $f['username']; $this->password = $f['password']; $this->host = $f['protocol']."://".$f['server']; $this->port = $f['port']; $this->to_name = $name; $this->to_email = $email; $this->subject = $subject; $this->body = $body; return sendEmail(); }else return false; } function sendEmail(){ $from = $this->from_name." <".$this->from_email.">"; $to = $this->to_name." <".$this->to_email.">"; $headers = array ('From' => $this->from, 'To' => $this->to, 'Subject' => $this->subject); $smtp = Mail::factory('smtp', array ( 'host' => $this->host, 'port' => $this->port, 'auth' => true, 'username' => $this->username, 'password' => $this->password)); $mail = $smtp->send($to, $headers, $this->body); if(PEAR::isError($mail)){ //echo($mail->getMessage()); //For debugging purposes only return false; }else return true; } function addAccount($name, $email, $username, $password, $protocol, $port, $server){ //nothing yet.. } } $db = new Database(DB_HOST, DB_USER, DB_PASS, DB_DB); $email = new Email($db); ?> I believe I am getting this down. Maybe?
  12. Very odd indeed. Sounds dumb, but make sure when you install the card, that you are not pulling on any data cables or things like that. Are you getting any POST (Power-On Self Test)? If you are getting one, then the hardware is working correctly. Install the graphics card again, but this time open up the BIOS and check that the boot settings have not changed for any reason. Have you updated the BIOS's firmware?
  13. Awesome. I have my database class working well. I even wrote a small HTML class for easier use. Now I am working on a Email class based on pear's SMTP classes. I have this: <?php require_once("pear/Mail.php"); class Email extends Database{ var $from_name, $from_email, $to_name, $to_email, $subject, $body, $host, $port, $username, $password; function dbEmail($id, $name, $email, $subject, $body){ $q = $this->select(TBL_SMTP, "*", "id = '".$id."'"); $n = $q->numRows(); if($n > 0){ $f = $q->fetchRow(); $this->from_name = $f['name']; $this->from_email = $f['email']; $this->username = $f['username']; $this->password = $f['password']; $this->host = $f['protocol']."://".$f['server']; $this->port = $f['port']; $this->to_name = $name; $this->to_email = $email; $this->subject = $subject; $this->body = $body; return sendEmail(); }else return false; } function sendEmail(){ $from = $this->from_name." <".$this->from_email.">"; $to = $this->to_name." <".$this->to_email.">"; $headers = array ('From' => $this->from, 'To' => $this->to, 'Subject' => $this->subject); $smtp = Mail::factory('smtp', array ( 'host' => $this->host, 'port' => $this->port, 'auth' => true, 'username' => $this->username, 'password' => $this->password)); $mail = $smtp->send($to, $headers, $this->body); if(PEAR::isError($mail)){ //echo($mail->getMessage()); //For debugging purposes only return false; }else return true; } function addAccount($name, $email, $username, $password, $protocol, $port, $server){ //nothing yet.. } } $email = new Email(); ?> I'm getting errors of such: Warning: Missing argument 1 for Database::__construct(), called in C:\Wamp\www\KrazyPickem 2.0\core\includes\Email.php on line 47 and defined in C:\Wamp\www\KrazyPickem 2.0\core\includes\Database.php on line 4 An so on.. I thought if it was a child class I did not have to create a new database: $db = new Database(...); How do I handle this situation? Oh ya, just so you know I took your advice, and fixed my logical error: //... function execute($query, $error = false, $mode = MYSQLI_STORE_RESULT){ $this->q = $query; if(!$error) $result = $this->mysqli->query($query, $mode); else $result = $this->mysqli->query($query, $mode) or die($this->mysqli->error); if(is_object($result) && $result instanceof MySQLi_Result){//if result is a object and is part of the mysqli class? $this->result = $result; $this->affectedRows = $this->result->num_rows; }else $this->affectedRows = $this->mysqli->affected_rows; return $this; } function numRows(){ return $this->affectedRows; } //...
  14. Wait, I completely understand now. When the query is not a select query, num rows will suffice, but when using a select query, num rows will not work, but using your method of affected rows will work. That is why your execute method has that if statement in it?
  15. This is a false statement in my case. I glanced at your posted code, but I did not copy it. It helped me understand OOP a little better. What really allowed me to fully understand how this should work is this: http://www.php.net/manual/en/mysqli.affected-rows.php Example #1. After I had my code created, I compared it to yours and went back and added the modes. I am very willing to learn, but I am not going to lie sometimes it's hard to understand you. Let me see if I understand you now; Instead of returning the database object, return the affected rows?
  16. I don't understand why affected rows is significant. Please explain why I need it? Oh, are you talking about when calling num rows?
  17. Glad to hear I am headed down the right path. What do you make of my additions? <?php class Database{ var $mysqli, $result, $q; function __construct($host, $user, $pass, $db){ $this->mysqli = new MySQLi($host, $user, $pass, $db); } function execute($query, $mode = MYSQLI_STORE_RESULT){ $this->q = $query; $this->result = $this->mysqli->query($query, $mode); return $this; } function fetchRow($mode = MYSQLI_ASSOC){ return $this->result->fetch_assoc($mode); } function fetchAll($mode = MYSQLI_ASSOC){ return $this->result->fetch_all($mode); } function numRows(){ return $this->result->num_rows; } function delete($table, $where){ return $this->execute("DELETE FROM ".$table." WHERE ".$where); } function deleteAll($table){ return $this->execute("TRUNCATE ".$table); } function update($table, $set, $where){ return $this->execute("UPDATE ".$table." SET ".$set." WHERE ".$where); } function insert($table, $values, $data){ $valString = $dataString = ""; $length = count($values); for($i = 0; $i<$length; $i++){ $valString .= $values[$i]; $dataString .= $values[$i]." = '".$data[$i]."'"; if($i < $length-1){ $valString .= ", "; $dataString .= ", "; } } return $this->execute("INSERT INTO ".$table." (".$valString.") VALUES (".$dataString.")"); } } ?>
  18. You guys are helping me out a lot! Here is what I now have. <?php class Database{ var $mysqli, $result, $q; function __construct($host, $user, $pass, $db){ $this->mysqli = new MySQLi($host, $user, $pass, $db); } function execute($query){ $this->q = $query; $this->result = $this->mysqli->query($query); return $this; } function fetchRow(){ return $this->result->fetch_assoc(); } function fetchAll(){ return $this->result->fetch_all(); } } ?> I'm not too sure if I am getting the corrected results. This is returned when using fetch_all(): Should I be getting an array within an array?
  19. Well I don't think my fetchAll function working. I am getting an error as such: "Maximum execution time of 60 seconds exceeded" Maybe I am doing something wrong? Here is my entire class: <?php class database{ var $mysqli; function __construct($host, $user, $pass, $db){ $this->mysqli = new MySQLi($host, $user, $pass, $db); } function query($query){ return mysqli_query($this->mysqli, $query); } function fetchAssoc($query){ return mysqli_fetch_assoc($this->query($query)); } function fetchAll($query){ $rows = array(); while($f = $this->fetchAssoc($query)) $rows[] = $f; return $rows; } function numRows($query){ return mysqli_num_rows($this->query($query)); } function delete($table, $where){ return $this->query("DELETE FROM $table WHERE ".$where); } } ?> Here is how I am using it: $db = new database(DB_HOST, DB_USER, DB_PASS, DB_DB); print_r($db->fetchAll("SELECT * FROM people WHERE id != '-1'")); Any thoughts?
  20. I want to apologize to you. I've been going through a lot of crap here lately. Taking it out, through the internet, is not acceptable. I can see where you are coming from, without the application involved this function looks ridiculous, however I am all about writing clean, efficient, DRY code. I am done arguing with why I am using the function. Once again, sorry.
  21. While I respond to you, I must keep in mind that I must stay classy. While talking about checking for a false evaluation why would you use a !== instead of a !=, false or FALSE is always 0. Furthermore, I am not trying to impress anyone. I am not trying to showoff, if I could. I was just showing him an example of simple email validation. I fail to understand YOUR explanation of a DRY method. First off, what is a DRY method? Just a procedural approach, maybe? I also like the fact that you truncate my quotes. However, from what I understand is, I should not wrap a function around another one, even though I call it several different locations, I should just use it as is, then go back and change it in several different locations? By doing so I am an idiot. Thanks. I, for one, did not know that filter_var return the email otherwise. So now I can just change my one function to say this: function validEmail($email){ if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false; else return true; }
  22. The reason I used a function is just in case I find a better way of checking for a valid email, I call this function in several different locations. I need to know if it is valid or not. If I need something more from the input, then I would have written the function to do so. You cannot judge abstract code without knowing the application. Also, just a suggestion: if(filter_var($email, FILTER_VALIDATE_EMAIL)!==false){ } Is syntactically correct, but doing this: !==, is the operator for not identical, != is for not equal, in php. I would have written it like this: if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ } For the expression is evaluated to true or not. So in order for it to be a non-valid email we would put the not operator (!) in the expression so it can be evaluated to being true to execute the if body.
  23. I would create $censoredwords to an array of strings. Then use a for loop or maybe a foreach loop to compare the strings. Here is some code on the spot: $title = "mean"; $censored = array("stupid", "mean", "gay"); for($i=0; $i<count($censored); $i++){ if($censored[$i] == $title){ echo "naughty word"; break; } } Not sure if that code is all correct, I kinda did it real quick.
×
×
  • 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.