PHP Cambiare il risultato delle query sql

Fastidio

Utente Gold
1 Gennaio 2014
594
55
104
287
Salve!
Vorrei cambiare il risultato delle query prima che siano stampate a schermo. Si tratta di una pagina di amministrazione con lista utenti e relativi dati.
Vorrei cambiare il formato della data (quello attuale è il predefinito di mysql) e trasformare il risultato di user_level da 0 in "Utente".
Come faccio? Mi ci sto scervellando da un po' senza venirne a capo.. mi sono venute tante idee ma nessuna ha risolto il mio problema.
Ho messo in allegato lo screenshot di come è adesso.
Vi ringrazio in anticipo.
Qui c'è il codice:
PHP:
<?php
require_once('../includes/config.php');

session_start();

if (isset ($_SESSION['logged'])) {
    if ($_SESSION['user_level'] == 1 && $_SESSION['logged'] == 1){

?>

<html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=800;height=700">
        <link rel="stylesheet" href="./../style.css" />
      </head>
    <body>
      <div class="content">
        <h3>Pannello di controllo</h3>
                <br />
            <nav class='menu'>
                <p>Benvenuto <strong><?php echo $_SESSION['username'] ?></strong>.</p>
                <a href='./create_user.php'>Crea Utente</a>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <a href='./../logout.php'>Esci</a>
            </nav>
                <br />

<?php

    $sql = "SELECT *
            FROM utenti
            ORDER BY userid ASC
        ";

    $result = mysql_query($sql) or die ("Qualcosa è andato storto." . mysql_error());

    echo "
            ";

    echo '
        <table>
       
            <th>Id</th>
            <th>Username</th>
            <th>Password</th>
            <th>Last log</th>
            <th>Tipo di utente</th>
            <th>Azioni</th>
            ';


        while ($row = mysql_fetch_assoc($result)) {

            //Info
           
            echo "<tr>";
            echo "<td>" . $row["userid"] . "</td>" .
                 "<td>" . $row["username"] . "</td>" .
                 "<td>" . $row["password"] . "</td>" .
                 "<td>" . $row["lastlog"] . "</td>" .
                 "<td>" . $row["user_level"] . "</td>" .

            //Azioni

                "<td>" . '<a href="./update_user.php?id=' . $row['userid'] . '&username='. $row['username'] .'&user_level='. $row['user_level'] . '">Modifica Utente</a>' . "&nbsp;&nbsp;&nbsp;" .
                 '<a href="./delete_user.php?id='. $row['userid'] .' &username= '. $row['username'] .'">Elimina Utente</a>' . "</td>"  ;

            echo "</tr>";

            }

    echo '</table>';


    }else {
        echo "<p>Non sei autorizzato ad accedere a questa pagina.</p>";
    }
}else{

    header ('Location: ./../login.php');

}
?>
 

Allegati

  • Cattura.JPG
    Cattura.JPG
    36.3 KB · Visualizzazioni: 34
Salve!
Come faccio? Mi ci sto scervellando da un po' senza venirne a capo.. mi sono venute tante idee ma nessuna ha risolto il mio problema.
Ho solo modificato le parti inerenti a ciò che chiedi, senza badare al resto e senza testare...Prova:

PHP:
<?php
require_once('../includes/config.php');

session_start();

if (isset ($_SESSION['logged'])) {
    if ($_SESSION['user_level'] == 1 && $_SESSION['logged'] == 1){

?>

<html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=800;height=700">
        <link rel="stylesheet" href="./../style.css" />
      </head>
    <body>
      <div class="content">
        <h3>Pannello di controllo</h3>
                <br />
            <nav class='menu'>
                <p>Benvenuto <strong><?php echo $_SESSION['username'] ?></strong>.</p>
                <a href='./create_user.php'>Crea Utente</a>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <a href='./../logout.php'>Esci</a>
            </nav>
                <br />

<?php

    $sql = "SELECT *
            FROM utenti
            ORDER BY userid ASC
        ";

    $result = mysql_query($sql) or die ("Qualcosa è andato storto." . mysql_error());

    echo "
            ";

    echo '
        <table>
     
            <th>Id</th>
            <th>Username</th>
            <th>Password</th>
            <th>Last log</th>
            <th>Tipo di utente</th>
            <th>Azioni</th>
            ';


        while ($row = mysql_fetch_assoc($result)) {

            //Info
         
            echo "<tr>";
            echo "<td>" . $row["userid"] . "</td>" .
                 "<td>" . $row["username"] . "</td>" .
                 "<td>" . $row["password"] . "</td>" .
                 "<td>" . date('d-m-Y',$row["lastlog"]) . "</td>" .
                 "<td>" .
                              switch ($row["user_level"]) {
                                    case 0:
                                             "User";
                                             break;
                                    case 1:
                                             "Admin";
                                             break;
                              }
                . "</td>" .

            //Azioni

                "<td>" . '<a href="./update_user.php?id=' . $row['userid'] . '&username='. $row['username'] .'&user_level='. $row['user_level'] . '">Modifica Utente</a>' . "&nbsp;&nbsp;&nbsp;" .
                 '<a href="./delete_user.php?id='. $row['userid'] .' &username= '. $row['username'] .'">Elimina Utente</a>' . "</td>"  ;

            echo "</tr>";

            }

    echo '</table>';


    }else {
        echo "<p>Non sei autorizzato ad accedere a questa pagina.</p>";
    }
}else{

    header ('Location: ./../login.php');

}
?>
 
  • Mi piace
Reazioni: Fastidio
Ultima modifica:
Per favore smettila di usare mysql_connect... è deprecato... usa PDO
devo farlo.. e poi so che dovrei usare mysqli.
Non esiste solo il pdo:mad:
Messaggio unito automaticamente:

Ho solo modificato le parti inerenti a ciò che chiedi, senza badare al resto e senza testare...Prova:

PHP:
<?php
require_once('../includes/config.php');

session_start();

if (isset ($_SESSION['logged'])) {
    if ($_SESSION['user_level'] == 1 && $_SESSION['logged'] == 1){

?>

<html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=800;height=700">
        <link rel="stylesheet" href="./../style.css" />
      </head>
    <body>
      <div class="content">
        <h3>Pannello di controllo</h3>
                <br />
            <nav class='menu'>
                <p>Benvenuto <strong><?php echo $_SESSION['username'] ?></strong>.</p>
                <a href='./create_user.php'>Crea Utente</a>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <a href='./../logout.php'>Esci</a>
            </nav>
                <br />

<?php

    $sql = "SELECT *
            FROM utenti
            ORDER BY userid ASC
        ";

    $result = mysql_query($sql) or die ("Qualcosa è andato storto." . mysql_error());

    echo "
            ";

    echo '
        <table>
  
            <th>Id</th>
            <th>Username</th>
            <th>Password</th>
            <th>Last log</th>
            <th>Tipo di utente</th>
            <th>Azioni</th>
            ';


        while ($row = mysql_fetch_assoc($result)) {

            //Info
      
            echo "<tr>";
            echo "<td>" . $row["userid"] . "</td>" .
                 "<td>" . $row["username"] . "</td>" .
                 "<td>" . $row["password"] . "</td>" .
                 "<td>" . date('d-m-Y',$row["lastlog"]) . "</td>" .
                 "<td>" .
                              switch ($row["user_level"]) {
                                    case 0:
                                             "User";
                                             break;
                                    case 1:
                                             "Admin";
                                             break;
                              }
                . "</td>" .

            //Azioni

                "<td>" . '<a href="./update_user.php?id=' . $row['userid'] . '&username='. $row['username'] .'&user_level='. $row['user_level'] . '">Modifica Utente</a>' . "&nbsp;&nbsp;&nbsp;" .
                 '<a href="./delete_user.php?id='. $row['userid'] .' &username= '. $row['username'] .'">Elimina Utente</a>' . "</td>"  ;

            echo "</tr>";

            }

    echo '</table>';


    }else {
        echo "<p>Non sei autorizzato ad accedere a questa pagina.</p>";
    }
}else{

    header ('Location: ./../login.php');

}
?>
Grazie, è buona idea anche se sintatticamente sbagliata.. cercherò di rielaborare la tua idea
Messaggio unito automaticamente:

L'ho rielaborata e funziona!
Grazie a tutti!
Qui c'è il codice funzionante, nel caso servisse a qualcun'altro:
PHP:
<?php
require_once('../includes/config.php');

session_start();

if (isset ($_SESSION['logged'])) {
    if ($_SESSION['user_level'] == 1 && $_SESSION['logged'] == 1){

?>

<html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=800;height=700">
        <link rel="stylesheet" href="./../style.css" />
      </head>
    <body>
      <div class="content">
        <h3>Pannello di controllo</h3>
                <br />
            <nav class='menu'>
                <p>Benvenuto <strong><?php echo $_SESSION['username'] ?></strong>.</p>
                <a href='./create_user.php'>Crea Utente</a>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <a href='./../logout.php'>Esci</a>
            </nav>
                <br />

<?php

    $sql = "SELECT *
            FROM utenti
            ORDER BY userid ASC
        ";

    $result = mysql_query($sql) or die ("Qualcosa è andato storto." . mysql_error());

    echo "
            ";

    echo '
        <table>
       
            <th>Id</th>
            <th>Username</th>
            <th>Password</th>
            <th>Last log</th>
            <th>Tipo di utente</th>
            <th>Azioni</th>
            ';


        while ($row = mysql_fetch_assoc($result)) {

            //Info
           
            echo "<tr>";
            echo "<td>" . $row["userid"] . "</td>" .
                 "<td>" . $row["username"] . "</td>" .
                 "<td>" . $row["password"] . "</td>" .
                 "<td>" . date("d-m-Y, H:i:s", strtotime ($row["lastlog"])) . "</td>" .
                 "<td>";
                              switch ($row["user_level"]) {
                                    case 0:

                                         echo "User";
                                            break;
                                    case 1:
                                   
                                        echo "Admin";
                                            break;
                              }
                echo "</td>" .


            //Azioni

                "<td>" . '<a href="./update_user.php?id=' . $row['userid'] . '&username='. $row['username'] .'&user_level='. $row['user_level'] . '">Modifica Utente</a>' . "&nbsp;&nbsp;&nbsp;" .
                 '<a href="./delete_user.php?id='. $row['userid'] .' &username= '. $row['username'] .'">Elimina Utente</a>' . "</td>"  ;

            echo "</tr>";

            }

    echo '</table>';


    }else {
        echo "<p>Non sei autorizzato ad accedere a questa pagina.</p>";
    }
}else{

    header ('Location: ./../login.php');

}
?>
 

Allegati

  • Cattura.JPG
    Cattura.JPG
    38.6 KB · Visualizzazioni: 20