티스토리 뷰
(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>
일케 했었는데
이렇게 초 씸쁠하게 코드를 끝낼 수 있다.
결과)
잘하고싶다 뭐든
'현장실습 🏙' 카테고리의 다른 글
[현장 실습]2021 하계 5주25일~7주34일차 지옥의 갤러리 이전달 다음달 만들기 (0) | 2021.10.23 |
---|---|
[현장 실습]2021 하계 5주25일차- 슬라이드 갤러리 이전/다음달 버튼만들기 (0) | 2021.10.23 |
[현장 실습]2021 하계 5주21일차 - 관리자 페이지 만들기 1 (0) | 2021.10.23 |
[현장 실습]2021 하계 4주20일차 - php와 mysql 을 사용하여 테이블에 사진 저장하고 불러오기 3 (php class 사용) (0) | 2021.10.23 |
[현장 실습]2021 하계 4주19일차 - php와 mysql 을 사용하여 테이블에 사진 저장하고 불러오기 2 (0) | 2021.10.23 |
- Total
- Today
- Yesterday
- C언어
- 정보처리기사 실기 정리
- php게시판만들기
- php
- 이더리움
- css grid
- 리액트
- php 달력만들기 응용
- MySQL
- 노마드코더
- CSS
- 홈페이지 만들기
- 정처기 실기 정리
- HTML
- indexOf()
- 정보처리기사 실기
- 졸업작품
- 블록체인
- 정보처리기사
- 프로그래머스
- JavaScript
- set 객체
- 졸업작품준비
- 갤러리띄우기
- 백준
- 현장실습 기록
- 스마트컨트랙트
- 현장실습
- DAPP
- 정처기 실기
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |