티스토리 뷰

(2021.08.05 본인 네이버 블로그에서 작성한 글 옮겨옴)

 

실습을 하면 할수록 바닥치는 자존감 .........

오늘 느낀 점 5가지

1. 나 4년동안 뭐했나

2. 나 앞으로 공부 왕열심히 해야겠다

3. 나 취업 가능한가?

4. 나 개발자 가능한가?

5. 개발자 존경스럽다

뭐 그렇다고요 ....


1. 테이블 생성

create table notice1 (no int not null auto_increment primary key, path varchar(100) not null, name varchar(200) not null, title varchar(60) not null, cont varchar(1000) not null, time datetime not null default current_timestamp);

 

2. 내용 넣기

insert into notice1 (no, path, name, title, cont, time) value (null, "../../image", "a53c4148d72c1fca40abb91c01e994f4.jpg","[gkdlgkdl]", "contant!!", "2020-08-03");

또는 맨 뒤 시간을 default 로 바꾸면 현재시간으로 저장

하지만 나는 현재 지난 날짜도 화면에 띄워야하기 때문에

강제로 날짜를 집어넣어줌

3. 클래스 만들기 notice.class

원래는 이미지 , 타이틀 , 컨텐츠 별로 함수를 만들고 notice.php에서 각각 띄웠었다. (소스 참고)

<?

class notice {
	/* DB 접속 정보 */
	var $host   = 'localhost';  // 데이터베이스 서버 주소
	var $myUser = 'jeongeum';        // 데이터베이스 사용자 ID
	var $myPw   = '1202';  // 데이터베이스 사용자 PASSWD
	var $myDb   = 'test_db';    // 데이터베이스 명
	
	var $path;
	var $name;
	var $img = "";
	var $title = "";
	var $cont = "";
	var $full = "";
	var $conn;
	var $sql;
	var $result;
	
	function img_up() {
         $this->conn = mysqli_connect($this->host, $this->myUser, $this->myPw, $this->myDb);

        if (!$this->conn || mysqli_error($this->conn))
		{
			die ('could not connect');
		}
		$this->sql = "SELECT path,
                             name,
                             title,
                             cont				   
                       FROM notice1 
			           ORDER BY time desc";
			
			 $this->result = mysqli_query($this->conn,$this->sql);
			
			if (mysqli_num_rows($this->result) > 0)
			{
		      while($row = mysqli_fetch_assoc($this->result))
		      { 
		        $this->path = $row['path'];
		        $this->name = $row['name'];
		        $this->img = "<a href=#><img src='$this->path//$this->name'></a>";
		        
			  }return $this->img;
	        }
	        else
	        {
		     echo "테이블에 데이터가 없습니다.";
	        }
			mysqli_close($this->conn);
		      
	}
	
	function title_up() {
		$this->conn = mysqli_connect($this->host, $this->myUser, $this->myPw, $this->myDb);

        if (!$this->conn || mysqli_error($this->conn))
		{
			die ('could not connect');
		}
		$this->sql = "SELECT path,
                             name,
                             title,
                             cont				   
                       FROM notice1 
			           ORDER BY time desc";
			
			 $this->result = mysqli_query($this->conn,$this->sql);
			
			if (mysqli_num_rows($this->result) > 0)
			{
		      while($row = mysqli_fetch_assoc($this->result))
		      { 
		        $this->title1 = $row['title'];
				$this->title2 = "<h2>$this->title1</h2>";
			  }return $this->title2;
	        }
	        else
	        {
		     echo "테이블에 데이터가 없습니다.";
	        }
			mysqli_close($this->conn);
	}
	
	function cont_up() {
		$this->conn = mysqli_connect($this->host, $this->myUser, $this->myPw, $this->myDb);

        if (!$this->conn || mysqli_error($this->conn))
		{
			die ('could not connect');
		}
		$this->sql = "SELECT path,
                             name,
                             title,
                             cont				   
                       FROM notice1 
			           ORDER BY time desc";
			
			 $this->result = mysqli_query($this->conn,$this->sql);
			
			if (mysqli_num_rows($this->result) > 0)
			{
		      while($row = mysqli_fetch_assoc($this->result))
		      { 
		        $this->cont1 = $row['cont'];
				$this->cont2 = "<p>$this->cont1</p>";
			  }return $this->cont2;
	        }
	        else
	        {
		     echo "테이블에 데이터가 없습니다.";
	        }
			mysqli_close($this->conn);
	}
	
	
	
} 
 ?>

근데 이게 하나하나 띄워야하니까 효율적이지 못하고 낭비하고 있다는 것을 알려주셨다

나도 이게 비효율적인 것 같아서 하나로 합치긴 했었는데

이걸 어떻게 notice.php에서 띄워야 할지 몰라서 포기했었다.

과장님께서 알려주셔서

우선 이렇게 다 합쳐줬다

notice.class

function img_up() {
         $this->conn = mysqli_connect($this->host, $this->myUser, $this->myPw, $this->myDb);

        if (!$this->conn || mysqli_error($this->conn))
		{
			die ('could not connect');
		}
		$this->sql = "SELECT path,
                             name,
                             title,
                             cont				   
                       FROM notice1 
			           ORDER BY time desc";
			
			 $this->result = mysqli_query($this->conn,$this->sql);
			 
		if (mysqli_num_rows($this->result) > 0)
		{
		    while($row = mysqli_fetch_assoc($this->result))
			{
                //사용할 값들을 row에서 가져오고
				$this->path = $row['path'];
		        $this->name = $row['name'];
				$this->title1 = $row['title'];
				$this->cont1 = $row['cont'];
				
                //rtn이라는 변수에 html 코드들을 붙여준다 rtn뒤에 . 을 붙여줘야 내용이 다 붙는다
				$this->rtn .= "<li><div class='image'>\n";
				$this->rtn .= "<a href=#><img src='$this->path//$this->name'></a>\n";
				$this->rtn .= "</div><div class='title' style='float:left;'><a href='#'>\n";
				$this->rtn .= "<h2>$this->title1</h2>\n";
				$this->rtn .= "</div><div class='text' style='float:left;'>\n";
				$this->rtn .= "<p>$this->cont1</p>\n";
				$this->rtn .= "</a></div></li><hr style='border: 0; border-top: 1px solid #e1e1e1; height: 1px; margin-bottom: 0; width: 80%; float: left;'><br>\n";
				
				$this->title2 = "<h2>$this->title1</h2>";
		        $this->img = "<a href=#><img src='$this->path//$this->name'></a>";
				$this->cont2 = "<p>$this->cont1</p>";
			}
		}
	    else
	    {
		     $this->rtn = "테이블에 데이터가 없습니다.";
	    }
		
		mysqli_close($this->conn);
		
		return $this->rtn;
		      
	}

4. notice.php

notice.php 에서는

원래 하나하나 불러낸다고

<li>
		<div class="image">
		<?echo $obj1->img_up();?>
		</div>
		
		<div class="title" style="float:left;">
		<a href="#"><?echo $obj1->title_up();?>
		</div>
		<div class="text" style="float:left;">
		<?echo $obj1->cont_up();?></a>
		</div>
</li>

일케 했었는데

이렇게 초 씸쁠하게 코드를 끝낼 수 있다.

결과)

잘하고싶다 뭐든

댓글