모든 커뮤니티 플랫폼의

근본이라 할 수 있는 CRUD의

기초 공부를 마쳤다.

 

 

앞으로 활용할 곳이 많은 듯하니

이곳에 보관하고 복습하며

필요할 때마다 참고하자

 


 

1️⃣ Index.php 코드

 

 

<?php
   function print_title(){
      if(isset($_GET['id']))
        {echo $_GET['id'];}
      else
        {echo "Welcome";}
						 }

   function print_description(){
      if(isset($_GET['id']))
         {echo file_get_contents("data/".$_GET['id']);}
      else 
          {echo "You did it!";}
								}


    function print_list(){
      $list = scandir('./data');

       $i = 0;
       while($i < count($list))
       {
        if($list[$i] != '.')
       {if($list[$i] != '..')
       {echo "<li>
          <a href=\"indexing.php?id=$list[$i]\">
          $list[$i]
          </a>
          </li>\n";}}
       $i = $i + 1;
       }
    }
    ?>


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title><?php print_title(); ?></title>
  </head>
  
  <body>

      <h1><a href="indexing.php">WEB</a></h1>

          <ol>
            <?php
              print_list();
			?>
          </ol>

          <a href="create.php">create</a>

          <?php if(isset($_GET['id'])) { ?>
              <a href="update.php?id=<?=$_GET['id']?>">update</a>

              <form action="delete_process.php" method="post">
              <input type="hidden" name="id" value="<?=$_GET['id']?>">
              <input type="submit" value="delete">
              </form>
                                  <?php } ?>

        

      <h2>
          <?php
            print_title();
          ?>
      </h2>

          <?php
               print_description();
          ?>


  </body>
</html>

 


 

 2️⃣ Create.php 코드

 

<?php
function print_title(){
  if(isset($_GET['id']))
    {echo $_GET['id'];}
  else
    {echo "Welcome";}
}




function print_description(){
  if(isset($_GET['id']))
  {echo file_get_contents("data/".$_GET['id']);}
  else {echo "You did it!";}
}





function print_list(){
  $list = scandir('./data');

   $i = 0;
   while($i < count($list))
   {
    if($list[$i] != '.')
   {if($list[$i] != '..')
   {echo "<li>
          <a href=\"indexing.php?id=$list[$i]\">
          $list[$i]
          </a>
          </li>\n";}}
   $i = $i + 1;
   }
}
?>


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title><?php print_title(); ?></title>
  </head>


  <body>

      <h1><a href="indexing.php">WEB</a></h1>

          <ol>
            <?php
              print_list();
 
          </ol>

          <a href="create.php">create</a>
            <form action="create_process.php" method="post">

              <p>
                 <input type="text"
                  name="title"
                  placeholder="Title">
              </p>

              <p>
                  <textarea name="description"
                   placeholder="hit me"></textarea>
              </p>

              <p>
                 <input type="submit">
              </p>
            </form>


  </body>
</html>

 

 

2️⃣-1 Create_process.php 코드

 

 

<?php
 file_put_contents('data/'.$_POST['title'], $_POST['description']);
 header('Location: /PHP/indexing.php?id='.$_POST['title']);
?>

 


 

3️⃣ Update.php 코드

 

<?php
function print_title(){
  if(isset($_GET['id']))
    {echo $_GET['id'];}
  else
    {echo "Welcome";}
}


function print_description(){
  if(isset($_GET['id']))
  {echo file_get_contents("data/".$_GET['id']);}
  else {echo "You did it!";}
}


function print_list(){
  $list = scandir('./data');

   $i = 0;
   while($i < count($list))
   {
    if($list[$i] != '.')
   {if($list[$i] != '..')
   {echo "<li>
          <a href=\"indexing.php?id=$list[$i]\">
          $list[$i]
          </a>
          </li>\n";}}
   $i = $i + 1;
   }
}
?>



<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title><?php print_title(); ?></title>
  </head>


  <body>

      <h1><a href="indexing.php">WEB</a></h1>
          <ol>
            <?php
              print_list();
            ?>
          </ol>

          <h2>

            <form action="update_process.php" method="post">
            <input type="hidden" name="old_title" value="<?$_GET['id']?>">
              <p>
                 <input type="text"
                  name="title"
                  placeholder="Title"
                  value=<?php print_title();?>>
              </p>

              <p>
                  <textarea name="description"
                   placeholder="hit me"><?php print_description();?></textarea>
              </p>

              <p>
                 <input type="submit">
              </p>
            </form>


  </body>
</html>

 

 

3️⃣-1 Update_process.php 코드

 

 

<?php
 rename('data/'.$_POST['old_title'], 'data/'.$_POST['title']);
 file_put_contents('data/'.$_POST['title'], $_POST['description']);
 header('Location: /PHP/indexing.php?id='.$_POST['title']);
?>

 


 

4️⃣ Delete.process.php 코드

 

<?php
unlink('data/'.$_POST['id']);
header('Location: indexing.php');
?>

 

 

 

삭제는 별도의 메인 코드를

작성할 필요 없이

Index 파일에 추가로 넣어준다.

 


 

사용된 중요 문법들 정리는

내일 이어서

 

 

 

728x90
반응형

+ Recent posts