<?php

class rdv
{
  function select_rdv($selected)
  {
    $data = "<select name=\"id_rdv\" id=\"id_rdv\" size=\"1\">\n";
    $data .= "<option value=\"\">-- Choisir --</option>\n";

    $q = "SELECT * FROM rdv ORDER BY rdv_date ASC";
    $r = mysql_query($q);

    if ($r)
    {
      if ($t = mysql_num_rows($r))
      {
        while ($n = mysql_fetch_assoc($r))
          $data .= "<option value=\"" . $n["id_rdv"] . "\"" . ($n["id_rdv"] == $selected ? " selected" : "") . ">" . $n["rdv_date"] . "</option>\n";
      }
    }
    else
      die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

    $data .= "</select>";

    return $data;
  }

  function list_rdv($id_pat)
  {
    $data = array();

    $q = "SELECT * FROM rdv WHERE id_pat = '" . $id_pat . "' ORDER BY rdv_date DESC";
    $r = mysql_query($q);

    if ($r)
    {
      if ($t = mysql_num_rows($r))
      {
        while ($n = mysql_fetch_assoc($r))
          $data[] = $n;
      }
    }
    else
      die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

    return $data;
  }

  function get_rdv($id_rdv)
  {
    $q = "SELECT * FROM rdv WHERE id_rdv = '" . $id_rdv . "'";
    $r = mysql_query($q);

    if ($r)
    {
      $n = mysql_fetch_assoc($r);
      return $n;
    }
    else
      die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

    return false;
  }

  function edit_rdv($id_pat, $rdv_date)
  {
    $q = "INSERT INTO rdv (id_pat, rdv_date) VALUES ('" . $id_pat . "' , '" . $rdv_date . "')";
    $r = mysql_query($q);

    if ($r)
      return true;
    else
      die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

    return false;
  }

  function del_rdv($id_rdv)
  {
    $q = "DELETE FROM rdv WHERE id_rdv = '" . $id_rdv . "'";
    $r = mysql_query($q);

    if ($r)
    {
      $q = "OPTIMIZE TABLE rdv";
      $r = mysql_query($q);

      return true;
    }
    else
      die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

    return false;
  }
}

?>