Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.97 KB | None | 0 0
  1. <?php
  2.   $con = mysqli_connect("localhost", "root", "", "bazaphp");
  3. ?>
  4.  
  5. <?php
  6.   function makeOption($val) {
  7.     echo "<option>" . $val . "</option>";
  8.   }
  9. ?>
  10.  
  11. <?php
  12.   function makeForm($conf) {
  13.     $sql = "SELECT DISTINCT age FROM users ORDER BY age";
  14.     $result = mysqli_query($conf, $sql);
  15.  
  16.     echo "choose age";
  17.     echo "<form method='post' action='form.php'>";
  18.       echo "<select name='opcja'>";
  19.         while ( $row = mysqli_fetch_assoc($result) ) {
  20.           makeOption($row['age']);
  21.         }
  22.       echo "</select>";
  23.       echo "<input type='submit' name='showAge'>";
  24.     echo "</form>";
  25.  
  26.   }
  27. ?>
  28.  
  29. <?php
  30.   function makeFormGender($conf) {
  31.     $sql = "SELECT DISTINCT gender FROM users";
  32.     $result = mysqli_query($conf, $sql);
  33.  
  34.     echo "choose gender";
  35.     echo "<form method='post' action='form.php'>";
  36.       echo "<select name='opcja'>";
  37.         while ( $row = mysqli_fetch_assoc($result) ) {
  38.           makeOption($row['gender']);
  39.         }
  40.       echo "</select>";
  41.       echo "<input type='submit' name='showGen'>";
  42.     echo "</form>";
  43.  
  44.   }
  45. ?>
  46.  
  47. <?php
  48.   makeForm($con);
  49.   makeFormGender($con);
  50. ?>
  51.  
  52. <?php
  53.  
  54.   function orderById() {
  55.     $sql = "SELECT * FROM users ORDER BY id";
  56.     return $sql;
  57.   }
  58.  
  59.   function orderByFirstname() {
  60.     $sql = "SELECT * FROM users ORDER BY firstname";
  61.     return $sql;
  62.   }
  63.  
  64.   function orderByLastname() {
  65.     $sql = "SELECT * FROM users ORDER BY lastname";
  66.     return $sql;
  67.   }
  68.  
  69.   function orderByAge() {
  70.     $sql = "SELECT * FROM users ORDER BY age";
  71.     return $sql;
  72.   }
  73.  
  74.   function orderByGender() {
  75.     $sql = "SELECT * FROM users ORDER BY gender";
  76.     return $sql;
  77.   }
  78.  
  79. ?>
  80.  
  81. <table>
  82.  
  83.   <tr>
  84.     <th><a href="form.php?temp=<?php echo 1 ?>">id</a></th>
  85.     <th><a href="form.php?temp=<?php echo 2 ?>">firstname</a></th>
  86.     <th><a href="form.php?temp=<?php echo 3 ?>">lastname</a></th>
  87.     <th><a href="form.php?temp=<?php echo 4 ?>">age</a></th>
  88.     <th><a href="form.php?temp=<?php echo 5 ?>">gender</a></th>
  89.   </tr>
  90.  
  91.   <?php
  92.     if ( $_GET['temp'] == 1 ) {
  93.       $sql = orderById();
  94.     }
  95.     if ( $_GET['temp'] == 2 ) {
  96.       $sql = orderByFirstname();
  97.     }
  98.     if ( $_GET['temp'] == 3 ) {
  99.       $sql = orderByLastname();
  100.     }
  101.     if ( $_GET['temp'] == 4 ) {
  102.       $sql = orderByAge();
  103.     }
  104.     if ( $_GET['temp'] == 5 ) {
  105.       $sql = orderByGender();
  106.     }
  107.     if ( $_GET['temp'] == NULL ) {
  108.       $sql = "SELECT * FROM users";
  109.     }
  110.  
  111.     if ( isset($_GET['edit']) ) {
  112.       $sql = "SELECT * FROM users WHERE id='{$_GET['edit']}'";
  113.  
  114.       $result = mysqli_query($con, $sql);
  115.       while ( $row = mysqli_fetch_assoc($result) ) : ?>
  116.  
  117.       <form action="" method="post">
  118.         firstname <input type="text" name="fname">
  119.         firstname <input type="text" name="fname">
  120.         firstname <input type="text" name="fname">
  121.         firstname <input type="text" name="fname">
  122.         firstname <input type="text" name="fname">
  123.       </form>
  124.  
  125. </table>
  126.  
  127. <?php
  128.  
  129. /*
  130.  *
  131.  *
  132.  *
  133.  *
  134.  * if(isset($_GET['edit'])) {
  135.  * $sql="SELECT * from user_sch where id='{$_GET['edit']}'";
  136.  
  137.   $result=mysqli_query($con,$sql);
  138.   while($row = mysqli_fetch_assoc($result))
  139.   {
  140.  
  141.  
  142.  ?>
  143.  
  144. <form action="" method="post">
  145.    
  146. imie<input type="text" name="first_na" placeholder="<?php echo $row['firstname'] ?>">
  147. nazwisko<input type="text" name="last_na" placeholder="<?php echo $row['lastname'] ?>">
  148. wiek <input type="text" name="age" placeholder="<?php echo $row['age'] ?>">
  149. <select name="gender">
  150.     <option value="M">M</option>
  151.     <option value="F">F</option>
  152.     </select>
  153. <input type="submit" name="send"/>
  154. </form>
  155.  
  156. <?php
  157. }
  158. }
  159. if($_POST['wys_age'])
  160. {
  161.     $temp=$_POST['opcja'];
  162.  
  163.     $sql="SELECT * from user_sch WHERE age = $temp";
  164.    
  165.  
  166. }
  167. if($_POST['wys_gen'])
  168. {
  169.     $temp=$_POST['opcja'];
  170.    
  171.  
  172.     $sql="SELECT * from user_sch WHERE gender = '$temp'";
  173.     //echo $sql;
  174.  
  175. }
  176. if($_POST['send'])
  177. {
  178.  
  179.     $name=$_POST['first_na'];
  180.     $lastn=$_POST['last_na'];
  181.     $age=$_POST['age'];
  182.     $gender=$_POST['gender'];
  183.     $id=$_GET['edit'];
  184.         $sql="UPDATE user_sch SET firstname='$name',lastname='$lastn',gender='$gender',age='$age' WHERE id='$id'";
  185.     //echo $sql;
  186.  
  187.  
  188.     mysqli_query($con,$sql);
  189.     header("Refresh:0; url=table_edit.php");
  190.  
  191. }
  192.  
  193.  
  194. //$sql="SELECT * from user_sch";
  195. if(isset($_GET['edit'])) {
  196.     $sql="SELECT * FROM user_sch";
  197.    
  198. }
  199.  
  200. $result=mysqli_query($con,$sql);
  201.  
  202.   while($row = mysqli_fetch_assoc($result))
  203.   {
  204.    echo '<tr>';
  205.     echo '<td>'.$row['ID'].'</td>';
  206.         echo '<td>'.$row['firstname'].'</td>';
  207.              echo "<td>{$row['lastname']}</td>";
  208.                  echo "<td>{$row['age']}</td>";
  209.                 echo "<td>{$row['gender']}</td>";
  210.                 echo '<td> <a href="table_edit.php?del='.$row['ID'].'">del</td>';
  211.                 echo '<td> <a href="table_edit.php?edit='.$row['ID'].'">edit</td>';
  212.                
  213.              
  214.         echo "</tr>";        
  215.  
  216.   }
  217.  
  218. ?>
  219.  *
  220.  *
  221.  *
  222.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement