Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Wywoływanie funkcji
Beziworld
post
Post #1





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 30.03.2011
Skąd: Mucharz

Ostrzeżenie: (0%)
-----


Jestem w trakcie pisania CMS'a na własny użytek. I mam problem przy stworzeniu działającej listy użytkowników z opcją Edytuj i Usuń

Model:
Kod
class usersmodel
{
    private $__config;
    private $__router;
    private $__db;
    private $__params;
    
    public function __construct()
    {
        $this->__config = registry::register("config");
        $this->__router = registry::register("router");
        $this->__db = registry::register("db");
        $this->__params = $this->__router->getParams();
    }
    
    public function drawUsersList()
    {
        $result = "<table class=\"text wideTable\" cellspacing=\"0\">\n
        <tbody>
        <tr class=\"legend\">
            <td style=\"min-width: 30px!important\">ID</td>
            <td>Imię i Nazwisko</td>
            <td>Nick</td>
            <td>E-Mail</td>
            <td>Data Urodzenia</td>
            <td>Funkcje</td>
        </tr>";
        
        $users = $this->__db->execute("SELECT users.id, users.fullname, users.username, users.mail, users.birthdate
        FROM users");
        
        if(!empty($users))
        {
            foreach($users as $user)
            {
                $result .= "<tr class=\"content\">
                    <td style=\"min-width: 30px!important\">{$user['id']}</td>
                    <td>{$user['fullname']}</td>
                    <td>{$user['username']}</td>
                    <td>{$user['mail']}</td>
                    <td>{$user['birthdate']}</td>
                    <td><a class=\"subtle\" href=\"".SERVER_ADDRESS."administrator/users/view/".$user['id']."\">Edycja użytkownika</a> |
                    <a class=\"subtle\" href=\"java script:void(0);\" onClick=\"removeUsers('".$user['id']."');\">Usuń użytkownika</a></td>
                </tr>";
            }

            $result .= "</tbody></table>";
        }

            return $result;
    }
}


Funkcje:
Kod
function saveUsers(id)
{
    var r = "";
    var rid = "";
    
    if(id == undefined || id == "")
    {
        r = "Czy chcesz utworzyć nowego użytkownika ?";
        rid = "new";
    }
    else
    {
        r = "Czy chcesz zapisać wprowadzone zmiany ?";
        rid = id;
    }
    
    var ask = confirm(r);
    if(ask)
    {        
        var fullname = $("#userFullname").val();
        var username = $("#userUsername").val();
        var password = $("#userPassword").val();
        var mail = $("#userMail").val();
        var birthdate = $("#userBirthdate").val();
        
        $.ajax({
            type: "POST",
            url: "application/media/_external/administrator.php",
            processdata: true,
            data: "saveUsers=" + rid + "&fullname=" + fullname + "&username=" + username + "&password=" + password + "&mail=" + mail + "&birthdate=" + birthdate,
            dataType: 'html',
            success: function(data)
            {
                if(data == "true")
                {
                    window.location.replace("administrator/users");
                }
                else
                {
                    alert(data);
                }
            }                
           });
    }
}
function removeUsers(id)
{
    var ask = confirm("Czy na pewno chcesz usunąć tego użytkownika ?");
    if(ask)
    {
        $.ajax({
            type: "POST",
            url: "application/media/_external/administrator.php",
            processdata: true,
            data: "removeUsers=true&id=" + id,
            dataType: 'html',
            success: function(data)
            {
                if(data == "true")
                {
                    window.location.reload();
                }
                else
                {
                    alert(data);

                }
            }                
           });
    }
}


application/media/_external/administrator.php
Kod
elseif(isset($_POST['saveUsers']))
{
    if($_POST['saveUsers'] == "new")
    {
        $r = $db->execute("INSERT INTO users VALUES (NULL, '{$_POST['fullname']}', '{$_POST['username']}', '{$_POST['password']}', '{$_POST['mail']}', '{$_POST['birthdate']}')");
    }
    else
    {
        $r = $db->execute("UPDATE users SET fullname = '{$_POST['fullname']}', username = '{$_POST['username']}', password = '{$_POST['password']}', mail = '{$_POST['mail']}', birthdate = '{$_POST['birthdate']}'
        WHERE id = '{$_POST['saveUsers']}'");
    }

    if(!$r)
    {
        echo "Wystąpił błąd podczas aktualizacji użytkowników!";
    }
    else
    {
        echo "true";
    }
    
}
elseif(isset($_POST['removeUsers']))
{
    $r1 = $db->execute("DELETE FROM users WHERE id = '{$_POST['id']}'");
    echo ($r1) ? "true" : "Wystąpił błąd podczas usuwania użytkownika!";
}
else
{
    die("Dostęp do tej strony został zablokowany przez administratora!");
}



Dla porównania załączam opcję pisania artykułów, która o dziwo działa:
Kod
<?php

class articlesmodel
{
    private $__config;
    private $__router;
    private $__db;
    private $__params;
    
    public function __construct()
    {
        $this->__config = registry::register("config");
        $this->__router = registry::register("router");
        $this->__db = registry::register("db");
        $this->__params = $this->__router->getParams();
    }
    
    public function drawArticlesList()
    {
        $result = "<table class=\"text wideTable\" cellspacing=\"0\">\n
        <tbody>
        <tr class=\"legend\">
            <td style=\"min-width: 30px!important\">ID</td>
            <td>Tytuł artykułu</td>
            <td style=\"max-width: 600px!important\">Skrócona treść</td>
            <td>Data dodania</td>
            <td>Autor</td>
            <td>Ocena</td>
            <td>Funkcje</td>
        </tr>";
        
        $articles = $this->__db->execute("SELECT articles.id, articles.title, articles.text, articles.date, articles.author, SUM(ocena) as ocena
        FROM articles
        LEFT JOIN votes on articles.id = votes.id_artykul
        GROUP BY votes.id_artykul");
        
        if(!empty($articles))
        {
            foreach($articles as $article)
            {
                $result .= "<tr class=\"content\">
                    <td style=\"min-width: 30px!important\">{$article['id']}</td>
                    <td>{$article['title']}</td>
                    <td style=\"max-width: 600px!important\">".substr($article['text'], 0, 497)."...</td>
                    <td>{$article['date']}</td>
                    <td>{$article['author']}</td>
                    <td>{$article['ocena']}</td>
                    <td><a class=\"subtle\" href=\"".SERVER_ADDRESS."administrator/articles/view/".$article['id']."\">Edycja artykułu</a> | <a class=\"subtle\" href=\"java script:void(0);\" onClick=\"removeArticle('".$article['id']."');\">Usuń artykuł</a></td>
                </tr>";
            }

            $result .= "</tbody></table>";
        }

            return $result;
    }
}


Kod
function saveArticle(id)
{
    var q = "";
    var uid = "";
    
    if(id == undefined || id == "")
    {
        q = "Czy chcesz utworzyć nowy artykuł o podanej treści ?";
        uid = "new";
    }
    else
    {
        q = "Czy chcesz zapisać wprowadzone zmiany w artykule ?";
        uid = id;
    }
    
    var ask = confirm(q);
    if(ask)
    {
        CKEDITOR.instances['editor1'].updateElement();
        var content = CKEDITOR.instances['editor1'].getData();
        content = htmlspecialchars_decode(content);
        content = content.replace("&nbsp;", " ");
        
        var title = $("#artTitle").val();
        var date = $("#artDate").val();
        var author = $("#artAuthor").val();
        
        $.ajax({
            type: "POST",
            url: "application/media/_external/administrator.php",
            processdata: true,
            data: "saveArticle=" + uid + "&title=" + title + "&date=" + date + "&author=" + author + "&text=" + escape(content),
            dataType: 'html',
            success: function(data)
            {
                if(data == "true")
                {
                    window.location.replace("administrator/articles");
                }
                else
                {
                    alert(data);
                }
            }                
           });
    }
}

function removeArticle(id)
{
    var ask = confirm("Czy na pewno chcesz usunąć ten artykuł ?");
    if(ask)
    {
        $.ajax({
            type: "POST",
            url: "application/media/_external/administrator.php",
            processdata: true,
            data: "removeArticle=true&id=" + id,
            dataType: 'html',
            success: function(data)
            {
                if(data == "true")
                {
                    window.location.reload();
                }
                else
                {
                    alert(data);
                }
            }                
           });
    }
}


Kod
elseif(isset($_POST['saveArticle']))
{
    if($_POST['saveArticle'] == "new")
    {
        $q = $db->execute("INSERT INTO articles VALUES (NULL, '{$_POST['title']}', '{$_POST['text']}', '{$_POST['date']}', '{$_POST['author']}')");
    }
    else
    {
        $q = $db->execute("UPDATE articles SET title = '{$_POST['title']}', text = '{$_POST['text']}', date = '{$_POST['date']}', author = '{$_POST['author']}' WHERE id = '{$_POST['saveArticle']}'");
    }

    if(!$q)
    {
        echo "Wystąpił błąd podczas aktualizacji artykułów!";
    }
    else
    {
        echo "true";
    }
    
}
elseif(isset($_POST['removeArticle']))
{
    $q1 = $db->execute("DELETE FROM articles WHERE id = '{$_POST['id']}'");
    $q2 = $db->execute("DELETE FROM votes WHERE id_artykul = '{$_POST['id']}'");
    echo ($q1 && $q2) ? "true" : "Wystąpił błąd podczas usuwania artykułu!";
}



Może jestem ślepy, ale naprawdę nie widzę gdzie popełniłem błąd. Jeśli ktoś z bardziej doświadczonych userów jest wstanie mi pomóc to będę wdzięczny.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.08.2025 - 17:09