Vivid Lust Posted October 21, 2010 Share Posted October 21, 2010 Here it is, comments please guys! THnkas <?php class buddyList { // PenpalA = current logged in member; // PenpalB = member to add to list; // name = name of member to add to list var $buddyList; //an array of buddies var $buddies; //an amount var $loggedUserID; function __construct(){ $this->loggedUserID = $_ENV['loggedMemberID']; } function add($penpalB) { //check if row already exists $sql = "SELECT id FROM buddy WHERE `PenpalA` = '$this->loggedUserID' AND `PenpalB` = '$penpalB' AND `status` = 1"; $mysql = new mysql(); $mysql->query($sql); if( mysql_num_rows( $mysql->result ) < 1 ){ //if row doesnt exist add new row $sql = "INSERT INTO `buddy` (`penpalA` ,`PenpalB`) VALUES ('$this->loggedUserID','$penpalB');"; $mysql = new mysql(); $mysql->query($sql); $tempMember = new member($penpalB); //display success message echo '<span class="pint2"><strong>'.PROFILEadded_buddy_before.$tempMember->name.'</strong> '.PROFILEadded_buddy_after.' <a style="color:#ffffff" href="http://localhost/penpalparade/buddy'.( in_array($lang,$_ENV['supportedLanguages']) ? "/".$lang : $nothing).'">'.PROFILEsee_buddy_list.'</a> </span>'; } } function remove($penpalB) { $sql = "UPDATE buddy SET `status` = 0 WHERE `PenpalA` = '$this->loggedUserID' AND `PenpalB` = '$penpalB'"; $mysql = new mysql(); $mysql->query($sql); $tempMember = new member($penpalB); // display success message echo '<span class="pint2"><strong>'.PROFILEremoved_buddy_before.$tempMember->name.'</strong> '.PROFILEremoved_buddy_after.' <a style="color:#ffffff" href="http://localhost/penpalparade/buddy'.( in_array($lang,$_ENV['supportedLanguages']) ? "/".$lang : $nothing).'">'.PROFILEsee_buddy_list.'</a></span>'; } function build() { $this->buddyList = array(); $sql = "SELECT `PenpalB` FROM buddy WHERE `PenpalA` = '$this->loggedUserID' AND `status` = 1"; $mysql = new mysql(); $mysql->query($sql); $i = 0; while($row = mysql_fetch_array($mysql->result)){ $this->buddyList[$i] = $row['PenpalB']; $i++; } $this->buddies = $i; } function display() { //build table echo '<table border="1" width="100%">'; echo '<tr><td align="center"><strong>'.MISCbuddy_name.'</strong></td>'; echo '<td align="center"><strong>'.MISCbuddy_last_online.'</strong></td>'; echo '<td></td><td></td></tr>'; //display buddies for ( $counter = 0; $counter <= $this->buddies-1; $counter += 1) { $tempMember = new member($this->buddyList[$counter]); echo '<tr>'; //start row echo '<td align="center">'; echo '<a href='.(in_array($lang,$_ENV['supportedLanguages']) ? $lang."/" : $nothing).HEADlanguage_exchange.'/id/'.$tempMember->id.'">'.$tempMember->name.'<br />'; $tempMember->displayPhoto(82); echo '</td>'; echo '<td align="center">'.MISCbuddy_ago_before.time_since($tempMember->lastOnline).MISCbuddy_ago_after.'</td>'; echo '<td align="center"><a href="http://localhost/penpalparade/email.php?id='.$tempMember->id.'&email=yes'.(in_array($lang,$_ENV['supportedLanguages']) ? '&lang='.$lang : $nothing).'">'.MISCbuddy_message.'</a></td>'; echo '<td align="center">'; ?> <form action="javascript:void(0);" method="post" onsubmit="if( confirm('<?=MISCbuddy_remove_confirm?>') ){ document.getElementById('removePenpal<?=$tempMember->id?>').innerHTML='<?=MISCbuddy_remove_change?>';document.getElementById('ajax-loader<?=$tempMember->id?>').style.display = 'block';window.location = 'buddy.php?remove=<?=$tempMember->id?><?=(in_array($lang,$_ENV['supportedLanguages']) ? '&lang='.$lang : $nothing)?>' }else{ }"> <div style="display:inline;" id="removePenpal<?=$tempMember->id?>"><input type="submit" value="<?=MISCbuddy_remove?>" /></div> <div style="display:none;" id="ajax-loader<?=$tempMember->id?>"><img class="ajaxLoader" src="mages/ajax-loader.gif" alt="removing..."/></div> </form></td> <?php echo '</tr>'; //end row } echo '</table>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/216471-first-class/ Share on other sites More sharing options...
trq Posted October 21, 2010 Share Posted October 21, 2010 The first thing I noticed is that this class has html mixed all through it making it tied to a particular site. Classes are meant to be reusable, this is far from it. Quote Link to comment https://forums.phpfreaks.com/topic/216471-first-class/#findComment-1124822 Share on other sites More sharing options...
Vivid Lust Posted October 21, 2010 Author Share Posted October 21, 2010 Thanks for the message. How would I use HTML in conjunctions with a class then if it's not actually in the class. How would I display an error message, success message, etc etc? Jake Quote Link to comment https://forums.phpfreaks.com/topic/216471-first-class/#findComment-1124879 Share on other sites More sharing options...
ignace Posted October 21, 2010 Share Posted October 21, 2010 Here's a basket filled with goodness class BuddyTableGateway { private $table = 'buddy'; private $database = null; function __construct(MySQLi $mysql) { .. } function insert($data) { .. } function update($data, $where) { .. } function delete($where) { .. } } class Buddy { private $id = 0; .. private $table = null; function __construct($data, TableGateway $table) { //$this->_init($data); $this->table = $table; } .. function save() { if($this->id == 0) { $this->id = $this->table->insert( array( .. ) ); } else { $this->table->update( array( .. ), 'id = ' . $this->id ); } } function equals(Buddy $buddy) { return $this->id == $buddy->id; // *MAGIC* } } class BuddyList implements Countable, IteratorAggregate { private $list = array(); private $count = 0; function add(Buddy $b) { $this->list[] = $b; ++$this->count; } function remove(Buddy $b) { for($i = 0, $found = false; $i < $this->count() && !$found; ++$i) { if($this->list[$i]->equals($b)) { unset($this->list[$i]); --$this->count; $found = true; } } return $found; } function count() { return $this->count; } function getIterator() { return new ArrayIterator($this->list); } } Quote Link to comment https://forums.phpfreaks.com/topic/216471-first-class/#findComment-1124951 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.