Naez Posted February 16, 2008 Share Posted February 16, 2008 Okay I'm trying to get into OOP so I am trying to re-write this code On display at http://naez.myftp.org/sqltests.php <?php // Testing some basic database functions include ('mysqlconnect.php'); } // selecting information from the forum_id that == our site news $sql1 = "SELECT * FROM phpbb_topics WHERE forum_id = '8' ORDER BY topic_id DESC LIMIT 5"; $result1 = mysql_query($sql1); // now we will display the 5 that we chose. while ($row1 = mysql_fetch_array($result1)) { echo "<b>" . $row1['topic_title'] . "</b> - "; $sql2 = "SELECT * FROM phpbb_users WHERE user_id = '" . $row1['topic_poster'] . "'"; $result2 = mysql_query($sql2); $row2 = mysql_fetch_array($result2); echo "by: " . $row2['username'] . "<br>"; /* Grabbing all the info regarding posts where our topic_id's match, whats good is it automatically picks the first post in the topic so it's real easy */ $sql3 = "SELECT * FROM phpbb_posts WHERE topic_id = '" . $row1['topic_id'] . "'"; $result3 = mysql_query($sql3); $row3 = mysql_fetch_array($result3); echo nl2br($row3['post_text']) . "<br /><br />"; } ?> So I'm thinking that especially within the main loop some Objects would make this easier so im trying to write that but im not very good, this is wha i have so far. On display @ http://naez.myftp.org/tester.php <?php include 'mysqlconnect.php'; include 'dbstuffclass.php'; $where = "forum_id = '8' ORDER BY topic_id DESC LIMIT 5"; $table = "phpbb_topics"; $dbobject = new dbstuff($table, $where); echo $dbobject->showData(); ?> dbstuffclass.php <?php class dbstuff { var $table_name; // table we'll pull data from var $where_statement; // our WHERE sql statement var $data_array; // data from the database var $errors; // array of error messages function dbstuff($table, $where) { $this->table_name = $table; $this->where_statement = $where; if (empty($this->where_statement)){ $where_str = NULL; } else { $where_str = "WHERE " . $this->where_statement; } $query = "SELECT * FROM " . $this->table_name . " " . $where_str; $result = mysql_query($query); $this->data_array = $result; } function showData() { while ($showdata = mysql_fetch_array($this->data_array)) { foreach ($showdata as $key => $value) { echo "Key: ". $key . " Value: " . $value . "<br />\n"; } } } } class db2stuff extends dbstuff { // omg i need halp } ?> I hope I made it clear what i'm trying to do here... I've never really dabbled into OOP so I'm in over my head <0>.. if someone could provide some examples on how to go about doing this that would be super awesome pawsum Link to comment https://forums.phpfreaks.com/topic/91352-new-to-oop-need-help/ Share on other sites More sharing options...
able Posted February 17, 2008 Share Posted February 17, 2008 It vaguelly looks like you are trying to invent the active record pattern. A quick google for php active record should give you all sorts of help :-) Link to comment https://forums.phpfreaks.com/topic/91352-new-to-oop-need-help/#findComment-468652 Share on other sites More sharing options...
Naez Posted February 17, 2008 Author Share Posted February 17, 2008 Yea now that I'm a little bit more acquainted with OOP im not sure what I was trying to do there >.> Anyways here's my first "real" OOP PHP class! [code=php:0]<?php class mailingList { var $email; var $errors; var $code; var $customMsg; var $customSubj; var $action; // if we have the email adress already, we'll do stuff with it, otherwise we'll display the form. function __construct() { // if we get the email in the .php?email= we will send it along to our validator/mailer or if an action is specified perform it if (isset($_GET['email'])){ $this->email = addslashes($_GET['email']); $act = $_GET['action']; switch ($act){ case "validate": // we have gotten an email coming in and we'll validate it now. $this->code = $_GET['rancode']; $sql = "SELECT * FROM mailinglist WHERE randomcode='" . $this->code . "'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); // woops! there was no randomcode that matched our $_GET so we'll show the form again, so the person can type their if (!$row){ $this->errors = "That was not a valid code."; $this->showform(); } else { // success! the codes match and thus the account will be activated $query = "UPDATE mailinglist SET active=1 WHERE randomcode='" . $this->code . "'"; mysql_query($query); echo "Your email <b>" . $this->email . "</b> has been validated and is now in our mailing list."; } break; case "resend": // this is if the person needs to get the confirmation email resent. $sql = "SELECT * FROM mailinglist WHERE email='" . $this->email . "'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); // ruh roh! there is no email by that name!! show the form! if (!$row){ $this->errors = "An error occured when searching for your email."; $this->showform(); } else { // okay everything looks good so we'll send a custom email with the link to validate their email. $this->code = $row['randomcode']; $this->customMsg = "This email was sent to activate your email to recieve our newsletter."; $this->customSubj = "Confirmation Resent regarding mailing list."; $this->action = "validate"; $this->confirmationEmail(); } break; case "remove"; // this is if someone wants to remove their email from our mailing list. $this->code = $_GET['rancode']; $sql = "SELECT * FROM mailinglist WHERE email='" . $this->email . "'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); // is there a code in the $_GET? If not we'll see whatsup. if ($this->code){ if (!$row){ // okay that email doesnt even exist... someone must be trying to fool the system! $this->errors = "An error occured when searching for your email."; $this->showform(); } else { // okay since theres an email we'll do a check if the codes match if ($this->code == $row['randomcode']){ $query = "UPDATE mailinglist SET active=0 WHERE randomcode='" . $this->code . "'"; mysql_query($query); echo "Your email <b>" . $this->email . "</b> has been removed from our mailing list."; } else { $this->errors = "Your code did not match ours."; $this->showform(); } } } else { // since no code has been entered yet, we must believe they want a confirmation to delete! $this->code = $row['randomcode']; $this->customMsg = "This email was sent because you wish to be removed from our mailing list."; $this->customSubj = "Remove from our Mailing list."; $this->action = "remove"; $this->confirmationEmail(); } break; default: // no action specified or an invalid one results in basically our first validation email being sent if the email passes. $this->firstmail($this->email); } } else { // An email has not been supplied so we'll show our form. $this->showform(); } } public function showform() { // just our form, it gets shown alot and is nothing special!! echo "Sign up for the " . $_SERVER['HTTP_HOST'] . " mailing list?<br />"; echo $this->errors; echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="get">'; echo '<input type="hidden" name="mode" value="mailinglist" />'; echo '<input type="text" name="email" />'; echo '<input type="submit" name="submit" value="submit" />'; } public function firstmail($email) { // the function that happens when a new email is entered into our form. Check if the email is valid if ($this->validateEmail($this->email)){ if (!$this->errors){ // double check for errors! if not we'll go ahead and enter it into our database and send an email // with a link to confirm the address! $randomcode = new randomGen(10); $this->code = $randomcode->key; $sql = "INSERT INTO mailinglist (`email`, `randomcode`) VALUES ('" . $this->email . "', '" . $this->code . "')"; $result = mysql_query($sql); $this->customMsg = "Our records show that " . $this->email . " has signed up for our newsletter."; $this->customSubj = "Confirm Signup to the Newsletter"; $this->action = "validate"; $this->confirmationEmail(); } } else { // failed inspection $this->showform(); } } public function confirmationEmail() { // a generic email that thanks to OOP we can make it very custom! echo "An email has been dispatched to <b>" . $this->email . "</b>. <br><br>"; echo "Please follow the link within it to confirm your action."; $link = "http://" .$_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?mode=mailinglist&email=" . $this->email . "&action=" . $this->action . "&rancode=" . $this->code; $to = $this->email; $subject = $this->customSubj; $message = $this->customMsg . "\n\n" . ' Follow this link: ' . $link; $headers = 'From: ' . $_SERVER['HTTP_HOST'] . "\r\n" . 'Reply-To: noreply@' . $_SERVER['HTTP_HOST']; mail($to, $subject, $message, $headers); } public function validateEmail($email) { /* * This function checks if an email is valid.. Syntax: * validateEmail($_GET['email]); * if(!validateEmail($_GET['email']) { include('whatever.php'); exit(); } */ if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $email)){ // if it passes this stage (aka being a real email or at least looking like one, we'll check if its in our db yet $this->errors = "Email is not valid."; return false; } else { // check if its in our db yet $sql = "SELECT * FROM mailinglist WHERE email='" . $email . "'"; $result = mysql_query($sql); $num_rows = mysql_num_rows($result); if ($num_rows == 0){ // its not in our db and looks like a valid email so we'll let it pass return true; } else { // ruh roh its already in our DB! both of these will return false but we want a custom error msg $row = mysql_fetch_array($result); if ($row['active'] == 0){ // 0 specifies that this email is not active... so we'll see whatsup. $this->errors = "Email is already in our database, but inactive. Click <a href=\"" . $_SERVER['PHP_SELF'] . "?mode=mailinglist&action=resend&email=" . $this->email . "\">here</a> to resend the validation email."; } else { // obviously its an active email... perhaps the person wants to remove themselve $this->errors = "Email is in our database, and active. Click <a href=\"" . $_SERVER['PHP_SELF'] . "?mode=mailinglist&action=remove&email=" . $this->email . "\">here</a> to remove yourself from the mailing list."; } // by returning false the form will get shown wit our custom error msg imo! return false; } } } // end of function } // end of class class randomGen { /* * randomGen creates a random number/letter combination * $mybox = new randomGen(20); * echo $mybox->key; */ public $key; function __construct($length) { $this->key=$this->keygen($length); } private function keygen($length){ $letters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; for($i=0; $i<$length; $i++){ $out.=$letters{rand(0,61)}; } return $out; } } ?> [/code] Little messy but works great! in case anyone is wondering my database is structed like this [pre] id active email randomcode 1 1 [email protected] S3dS23fsads 2 0 [email protected] asd2Dwasd2 3 1 [email protected] c4w5fds345[/pre] Link to comment https://forums.phpfreaks.com/topic/91352-new-to-oop-need-help/#findComment-468696 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.