Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

게시판 목록 구현 #3

Closed
chaemino opened this issue Jun 6, 2023 · 1 comment
Closed

게시판 목록 구현 #3

chaemino opened this issue Jun 6, 2023 · 1 comment

Comments

@chaemino
Copy link
Owner

chaemino commented Jun 6, 2023

개요

게시판 목록을 구현한다.

결과

image

진행

mainpage.ejs

      <div class="post-list">
       <!-- 게시글 목록 -->
         <table id="board">
           <thead>
             <tr>
               <th style="width: 10%; height: 6vh;">게시글 번호</th>
               <th style="width: 40%; height: 6vh;">제목</th>
               <th style="width: 20%; height: 6vh;">작성 일자</th>
               <th style="width: 10%; height: 6vh;">작성자</th>
             </tr>
           <thead>
           <!-- 게시글 목록 보여주는 ejs -->
           <tbody>
             <% for (i=0; i<list.length; i++) { %>
             <tr>
               <td><%=list[i].게시글ID%></td>
               <td style="text-align:left"><%=list[i].제목%></td>
               <td><%=list[i].작성일자%></td>
               <td><%=list[i].작성자%></td>
             </tr>
             <% } %>
           <tbody>
        </table>
       </div>
app.get('/products/:page', (req, res) => {
     const page_size = 3; // 각 페이지의 최대 항목 수
     const page = req.params.page || 1 // if page is null then 1, 현재 페이지 번호
     console.log(page);

     const sql = 'SELECT * FROM 게시글';
     maria.query(sql, (err, rows, fields) => {
         for(let i=0; i<rows.length; i++){
             console.log('rows'+JSON.stringify(rows[i]));
             rows[i].작성일자 = moment(rows[i].작성일자).format('YYYY-MM-DD');
         }
         console.log("rows: "+rows[1].date);
         if (err) cosole.log('QUERY FAIL\n' + err);
         else res.render('mainpage.ejs', {list:rows});
     });
 });
@chaemino
Copy link
Owner Author

chaemino commented Jun 16, 2023

게시글의 경우, 가장 최신의 글이 상단에 위치한다.

  • app.js

SQL문에 ORDER BY 게시글ID DESC 정렬 코드를 추가했다.

/* show post list */
 /* Offset Pagination */
 app.get('/:page', (req, res) => {
     const page_size = 3; // 각 페이지의 최대 항목 수
     const page = Number(req.params.page || 1)
     console.log(req.params);
     console.log(`SELECT * FROM 게시글 limit ${page-1}, ${page+4}`);

     // select * from 게시글 limit 0,5
     // offset부터 limit까지

     const sql = `SELECT * FROM 게시글 ORDER BY 게시글ID DESC LIMIT ${page-1}, ${page+2};`;
     maria.query(sql, (err, rows, fields) => {
         for(let i=0; i<rows.length; i++){
             console.log('rows'+JSON.stringify(rows[i]));
             rows[i].작성일자 = moment(rows[i].작성일자).format('YYYY-MM-DD');
         }
         console.log("rows: "+rows[1].date);
         if (err) console.log('query is not excuted. select fail...\n' + err);
         else res.render('mainpage.ejs', {list:rows});
     });
 });

결과

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant