Utai Silva
Utai Silva

Reputation: 29

PHP, phpmyadmin, html, javascript

I am having an issue ordering into a table primary id values which auto-increment from phpmyadmin and adding them into a table from recent to latest (greatest to lowest) can i get some help? I am using json to get php to javascript. This code only shows the table from latest to recent.

$app->get('/proyects/all', function () use($servername, $dbname, $dbuser, $dbpassword)
{

$response = array();

try
{
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbuser, $dbpassword);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $conn->prepare("SELECT * FROM proyectos");

    if ($stmt->execute())    {
        $result = $stmt->fetchAll();
    }


    $conn = null;

    if(!empty($result))
    {
        $response['proyects'] = array();
        foreach ($result as $key => $value)
        {
            $response['proyects'][$key] = $value;

        }
        $response['success'] = true;

        echo json_encode($response);
        return;
    }
    else
    {
        $response['success'] = false;
        $response['message'] = "Proyects not found";

        echo json_encode($response);
        return;
    }

}
catch(PDOException $e)
{
    $response['success'] = false;
    $response['message'] = $e->getMessage();

    echo json_encode($response);
    return;
}
});

<script type="text/javascript">
     $(document).ready(function()
     {
        $.ajax(
        {
            url: path+'/proyects/all',
            type: 'get',
            dataType: "json",
            success: function(json)
            {
                console.log(json);

                if (json.success)
                {
                    $.each(json.proyects, function( index, value ) {

                      $('#proyecs').append('<tr><td>'+value.idproyect+'</td><td>'+value.pdescription+'</td><td>'+value.presponsible+'</td><td>'+value.pdateini+'</td><td>'+value.pdatefin+'</td><td>'+value.pstatus+'&#37</td><td><button type="button" class="btn btn-default">Details</button></td></tr>');
                    });
                }
                else
                {
                    Materialize.toast(json.message, 5000, 'red accent-4');
                }
            },
            error : function(e, settings, exception)
            {
                Materialize.toast("Oops! an error has ocurred", 5000, 'red accent-4');
            }
        });
    });
</script>

@sesonrario @rex sorry forgot the code

Upvotes: 0

Views: 48

Answers (1)

sensorario
sensorario

Reputation: 21668

Could

    $stmt = $conn->prepare("SELECT * FROM proyectos order by id");

solve your problem?

Upvotes: 1

Related Questions