<?
include '../config.php';
include '../func.php';

head();
aut();
ban();

echo head . 'Флиртомания' . div . katalog;

switch ($_GET['mod']) {
    
    default:
    echo "[<a href='flirt.php?mod=add'>Добавить</a>]<br/>";
    $all = mysql_result(mysql_query("SELECT count(*) FROM `flirt_vopros`"), 0);
    if($all){
    $onpage = 10;
    $allpage = ceil($all/$onpage);
    $this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
    if($this_page < 1 || $this_page > $allpage)$this_page = 1;
    $start = $this_page*$onpage - $onpage;
    $result = mysql_query("SELECT * FROM `flirt_vopros` ORDER BY `id` DESC limit ".num($start).",".num($onpage)."");
    while($flirt = mysql_fetch_assoc($result)){
        echo "<b>".$flirt[vopros]."...</b><br/> 1: ".$flirt[variant1]."<br/> 2: ".$flirt[variant2]."<br/> 3: ".$flirt[variant3]."<br/> [<a href='flirt.php?id=$flirt[id]&mod=delete'>del]</a> [<a href='flirt.php?id=$flirt[id]&mod=edit'>edit]</a><hr>"; 
    
    }
    echo div;
    echo link1 . '<b>Страницы:</b> ';
    navigation($allpage,"flirt.php?p={p}",1,1);
    echo div;
    }
    break;
    
    case 'delete':
    $id = num($_GET['id']);
    $p = num($_GET['p']);
    mysql_query("DELETE FROM `flirt_vopros` WHERE `id` = '".$id."'");
    header('Location: flirt.php?p='.$p);
    break;
    
    case 'edit':
    $id = num($_GET['id']);
    $p = num($_GET['p']);
    $flirt = mysql_fetch_assoc(mysql_query("SELECT * FROM `flirt_vopros` WHERE `id` = '".$id."'"));
    
    if (isset($_POST['submit'])) {
        $vopros = html($_POST['vopros']);
        $variant1 = html($_POST['variant1']);
        $variant2 = html($_POST['variant2']);
        $variant3 = html($_POST['variant3']);
        if (mysql_query("UPDATE `flirt_vopros` SET `vopros` = '".$vopros."', `variant1` = '".$variant1."', `variant2` = '".$variant2."',`variant3` = '".$variant3."' WHERE `id` = '".$id."'"))
        echo 'Успешно обновлено<br/>';
        else
        echo 'Ошибка';
    }
    echo '<form action="flirt.php?mod=edit&id='.$id.'" method="post" name="form">';
    echo '<b>Вопрос:</b> <input name="vopros" type="text" value="'.$flirt['vopros'].'" maxlength="100" /><br/ ><br/>';
    echo '1: <input name="variant1" type="text" value="'.$flirt['variant1'].'" maxlength="100" /><br/ >';
    echo '2: <input name="variant2" type="text" value="'.$flirt['variant2'].'" maxlength="100" /><br/ >';
    echo '3: <input name="variant3" type="text" value="'.$flirt['variant3'].'" maxlength="100" /><br/ >';
    echo '<input name="submit" type="submit" class="button" value="Сохранить" /></form><br/>';
    break;
    
    case 'add':
    if (isset($_POST['submit'])) {
        $vopros = html($_POST['vopros']);
        $variant1 = html($_POST['variant1']);
        $variant2 = html($_POST['variant2']);
        $variant3 = html($_POST['variant3']);
        if (mysql_query("INSERT INTO `flirt_vopros` SET `vopros` = '".$vopros."', `variant1` = '".$variant1."', `variant2` = '".$variant2."',`variant3` = '".$variant3."'"))
        echo 'Успешно добавлено<br/>';
        else
        echo 'Ошибка';
    }
    echo '<form action="flirt.php?mod=add" method="post" name="form">';
    echo '<b>Вопрос:</b> <input name="vopros" type="text" value="'.$flirt['vopros'].'" maxlength="100" /><br/ ><br/>';
    echo '1: <input name="variant1" type="text" value="'.$flirt['variant1'].'" maxlength="100" /><br/ >';
    echo '2: <input name="variant2" type="text" value="'.$flirt['variant2'].'" maxlength="100" /><br/ >';
    echo '3: <input name="variant3" type="text" value="'.$flirt['variant3'].'" maxlength="100" /><br/ >';
    echo '<input name="submit" type="submit" class="button" value="Сохранить" /></form><br/>';
    break;
}


////////////////////////////////////////////////////////////////////////////////
navig();
foot();
?>