Jump to content

first class


Vivid Lust

Recommended Posts

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>';
}
}
?>

 

Link to comment
Share on other sites

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);
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.