Page 2 of 2

Posted: Fri Aug 05, 2005 4:08 pm
by Luke
Ya, I guess it really didnt make a whole lot of sense, buy you get what I mean...

right?

:D

Posted: Sat Aug 06, 2005 6:15 am
by robster
theda wrote:Have you learned how to speak proper English first? That's pretty much a prerequisite to programming.
It isn't actually. What an ignorant comment. A lot of folk would even call that a racist comment. C'mon, these forums are full of intelligent people, regardless of if their language originated from the United Kingdom or not.

help me with this class file

Posted: Sat Aug 06, 2005 11:46 am
by vsaggu
Guide me how to start and implement this class file

Code: Select all

<?php
class admin {
	var $img_upload;
	
	// Constructor
	//function admin($host, $db, $user, $pass, $img_fname='',$img_path='',$img_url='') {
	function admin() {
		global $img_upload;
		if ($img_upload == 1) {
			global $img_fname;
			global $img_path;
			global $img_url;
			
			// Set all of the image vars locally in this class
			$this->img_fname = $img_fname;
			$this->img_path = $img_path;
			$this->img_url = $img_url;
			$this->img_upload = 1;
		}
		else {
			echo "Img upload not enabled<br>\n";
			$this->img_upload = 0;
		}
		
		db_connect();
	}

	///////////////////////////////////////////////
	// ADD Function				     //
	///////////////////////////////////////////////
	// $table : table to edit data in.	     //
	// $form_fields : field values from the form //
	///////////////////////////////////////////////
	function add($table, $form_fields) {
		// Enter IF img_up is true
		if ($this->img_upload == 1) {
			// First Upload file, If applicable
			if ($form_fields[$this->img_fname] != 'none') {
				// Uploads image and returns unique filename
				$form_fields[$this->img_fname] = $this->img_upload();
				if ($form_fields[$this->img_fname] == '') {
					echo $form_fields[$this->img_fname];
					echo "Invalid File format!!<br>\n";
					exit;
				}
			}
			else {
				// otherwise, set img to nothing
				$form_fields[$this->img_fname] = '';
			}
		}
		
		// Build INSERT Query
		$count = 1;
		$fcount = sizeof($form_fields);
		foreach ($form_fields as $key => $value) {
			if ($count == $fcount) {
				// END Field Names
				$field_names .= "$key";

				// END Data
				$fields .= "'$value'";
			}
			else {
				// Field Names
				$field_names .= "$key, ";
			
				// Data
				$fields .= "'$value', ";
			}
			$count++;
		}	
		$query = "INSERT INTO $table ($field_names) VALUES($fields)";
		//$this->query($query);
		db_query($query);
		return true;
	}

	///////////////////////////////////////////////
	// Edit Function			     //
	///////////////////////////////////////////////
	// $table : table to edit data in.	     //
	// $form_fields : field values from the form //
	// $id : id of item to edit		     //
	///////////////////////////////////////////////
	function edit($table,$form_fields,$id) {
	
		// Enter IF img_upload is enabled
		if ($this->img_upload == 1) {
		
			// If they uploaded an image, we need to remove the old one if applicable
			if ($form_fields[$this->img_fname] != 'none') {
				// Get old info from db	
				$query = "SELECT $this->img_fname FROM $table WHERE id = '$id'";
				$result = db_query($query);
				$result = db_fetch_array($result);
		
				if ($result[$this->img_fname] != '') {
					// Delete large image
					$full_path = $this->img_path . $result[$this->img_fname];
					unlink($full_path);
			
					// Delete thumbnail
					$tmb_path = $this->img_path . "sm" . $result[$this->img_fname];
					unlink($tmb_path);
				}
			
				// Do image functions to new image
				$form_fields[$this->img_fname] = $this->img_upload();
			}
			if ($form_fields[$this->img_fname] == 'none') {
				unset ($form_fields[$this->img_fname]);
			}
		}
		
		// Make UPDATE query
		$count = 1;
		$fcount = sizeof($form_fields);
		foreach ($form_fields as $name => $value) {
			if ($count == $fcount) {
				// End Field Names
				$fields .= "$name='$value'";
			}
			else {
				$fields .= "$name='$value', ";
			}
			$count++;
		}	
		$query = "UPDATE $table SET $fields WHERE id = '$id'";
		
		db_query($query);
		return true;
	}


	///////////////////////////////////////////////
	// Delete Function			     //
	///////////////////////////////////////////////
	// $table : table name to delete entry from  //
	// $id : id of entry to delete		     //
	///////////////////////////////////////////////
	function delete($table,$id) {
	
		// Enter IF img_up is true
		if ($this->img_upload == 1) {
			// Get old image name from DB
			$query = "SELECT $this->img_fname FROM $table WHERE id = '$id'";
			$result = db_query($query);
			$result = db_fetch_array($result);
	
			// Delete large image and thumbnail
			if ($result[$this->img_fname] != '') {
				// Delete large image
				$full_path = $this->img_path . $result[$this->img_fname];
				unlink($full_path);
				
				// Delete thumbnail
				$tmb_path = $this->img_path . "sm" . $result[$this->img_fname];
				unlink($tmb_path);
			}
		}
	
		// Delete item from database
		$query = "DELETE FROM $table WHERE id='$id'";
		db_query($query);
		return true;
	}
	
	///////////////////////////////////////////////
	// IMAGE UPLOAD Function		     //
	///////////////////////////////////////////////
	// No Arguments				     //
	///////////////////////////////////////////////
	function img_upload() {
		global $HTTP_POST_FILES;

		$filename = $HTTP_POST_FILES['form_fields']['name'][$this->img_fname];
		$filename_tmp = $HTTP_POST_FILES['form_fields']['tmp_name'][$this->img_fname];

		$size = GetImageSize($filename_tmp);
		$img_type = $size[2];
		if ($img_type != 2) {
			return false;
		}

		if ($img_unique == 1) {
			// Name Images unqiuely with UNIX time
			$filename_new = time() . ".jpg";
		}
		else {
			// Keep original filename
			$filename_new = $filename;
		}

		if (is_uploaded_file($filename_tmp)) {
			$save_loc = $this->img_path . $filename_new;

			// Move the Uploaded file to the correct Directory
			move_uploaded_file($filename_tmp, $save_loc);
			chmod($save_loc, 0755);

			// Make and Save Thumbnail image
			$thumb_save_loc = $this->img_path . "sm" . $filename_new;
			$size = GetImageSize($save_loc);
			$width = $size[0];
			$height = $size[1];
			$new_w = $width / 1.40;
			$new_h = $height / 1.40;
			$new_w = ceil($new_w);
			$new_h = ceil($new_h);
			$src_img = imagecreatefromjpeg($save_loc);
			$dst_img = imagecreate($new_w,$new_h);
			imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
			imagejpeg($dst_img, "$thumb_save_loc");
			return $filename_new;
		}
	}
}
?>

Posted: Sat Aug 06, 2005 12:34 pm
by BDKR
Hope you don't mind that I put your code inside the BB php tags. It's a lot easier to look at now.

I don't really have time to look at it just yet as I got to get to work on my car. However, a casual glance shows that there is a lack of knowledge or design skills. Overall, it's mechanically sound, but what you've created is close to a God Class.

And for everybody else, this is the biggest problem with teaching OOP without teaching OOD or RDD on the internet. What he's done is put all of the various functionaliies he needs into one class as opposed to seperating them into their own distinctive areas of responsibility.

Anyway, before going any further, do some digging into the topics of Responsibility Driven Design and Object Oriented Design. You can't write good OO otherwise.

Posted: Sat Aug 06, 2005 12:46 pm
by josh
I think you should split your class up into multiple classes and have them work together, maybe a database class and an image class. If you're going to be using the database and admin functions together that is, if not you should probably give admin it's own class.
Just my advice from a few weeks of doing this stuff, certainly not saying you have to do this but I'd recommend it.

scrotaye wrote:I've never tried to use a class, or even understood why I would need to. I guess I should get on the ball, eh.


You don't need to, but you'll never know the real joys of programming in OOP, I just started programming in object based code a few weeks ago, I now consider all of my old code unreadable. It really helps speed up the upgrade process too. It can be a bit intimidating to switch to OOP at first but it's actually not that much harder then writing a custom function. It's kind of a pain to re-code existing code into OOP too, I think it's easier to learn OOP by starting a new project entirely at first.

Posted: Sun Aug 07, 2005 8:22 am
by BDKR
Hey vsaggu!

You still there? If you want to learn this stuff, sound off. I don't think anybody here will mind throwing some input in.

how ?

Posted: Tue Aug 09, 2005 12:15 pm
by vsaggu
okay i'm here now, can we talk