Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: JSON Return Values

by poj (Abbot)
on Mar 23, 2018 at 19:38 UTC ( [id://1211632]=note: print w/replies, xml ) Need Help??


in reply to Re: JSON Return Values
in thread JSON Return Values

Why not encode a structure than keeps the record data and the flags separate

my $json = encode_json({ record => $data, ReturnedRecord => $return_count, DeleteRecord => $delete_count });

then no need to delete anything

var url = ".../update_tables.cgi"; var param = { "action":"delete_Record", "user_id":userid, }; $.getJSON(url, param, function(data){ if (data.ReturnedRecord){ loadJSONFormData(data.record); } }
sub delete_Record { my $userid = $query->param('user_id'); warn("Delete record based on user_id = '$userid'"); # SQL my $stmt_del = 'DELETE FROM users WHERE user_id = ?'; my $stmt_sel = ' (SELECT * FROM users WHERE user_id < ? ORDER BY user_id DESC LIMIT 1) UNION (SELECT * FROM users WHERE user_id > ? ORDER BY user_id ASC LIMIT 1)'; my $data = {}; my $return_count = 0; my $exit = 0; my $delete_count = $dbh->do($stmt_del,undef,$userid); if ($delete_count eq '0E0') { # Nothing deleted $delete_count = 0; } else { # select prev or next record my $sth = $dbh->prepare($stmt_sel); $sth->execute($userid,$userid); if ($data = $sth->fetchrow_hashref()){ $return_count = 1; } else { $exit = 1; $data = {}; } } # output my $json = encode_json({ record => $data, ReturnedRecord => $return_count, DeleteRecord => $delete_count }); print "Content-Type: application/json\n\n"; print $json; warn("Finished print $json"); exit($exit); }
poj

Replies are listed 'Best First'.
Re^3: JSON Return Values
by tultalk (Monk) on Mar 23, 2018 at 20:27 UTC

    Pretty slick. Lots of shortcuts particularly the UNION.

    Thanks much

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1211632]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-26 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found