Guest User

Untitled

a guest
Aug 14th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. ## PHP
  2.  
  3. <?php
  4.  
  5. function GetPostAndCommentsAsXML($postID, $db)
  6. {
  7. $xml = new DomDocument;
  8.  
  9. $posts = $xml->createElement("posts");
  10. $xml->appendChild($posts);
  11.  
  12. if(!$db) return $xml;
  13.  
  14. $result = $db->query(sprintf("call list_post_comments(%d)", $postID));
  15.  
  16. if(!$result) return $xml;
  17.  
  18. $row = $result->fetch_assoc();
  19.  
  20. $post = $xml->createElement("post");
  21.  
  22. foreach($row as $col => $val) $post->setAttribute($col, $val);
  23.  
  24. $result->free();
  25.  
  26. $posts->appendChild($post);
  27.  
  28. $db->next_result();
  29. $result = $db->use_result();
  30.  
  31. while($row = $result->fetch_assoc()){
  32.  
  33. $comment = $xml->createElement("comment");
  34.  
  35. foreach($row as $col => $val) $comment->setAttribute($col, $val);
  36.  
  37. if(!is_null($post)) $post->appendChild($comment);
  38. }
  39. $result->free();
  40.  
  41. return $xml;
  42. }
  43.  
  44. header("Content-type: text/xml");
  45.  
  46. $postID = 1;
  47.  
  48. $db = new Mysqli("localhost", "foo_dbo", "pass", "foo_db");
  49.  
  50. $xml = GetPostAndCommentsAsXML($postID, $db);
  51.  
  52. echo $xml->saveXML();
  53.  
  54. $db->close();
  55.  
  56. ?>
  57.  
  58. ## SQL
  59.  
  60. -- TABLES
  61.  
  62. drop table if exists users;
  63. create table users
  64. (
  65. user_id int unsigned not null auto_increment primary key,
  66. username varbinary(32) unique not null
  67. )
  68. engine=innodb;
  69.  
  70. drop table if exists posts;
  71. create table posts
  72. (
  73. post_id int unsigned not null auto_increment primary key,
  74. user_id int unsigned not null,
  75. created_date datetime not null,
  76. subject varchar(255) not null,
  77. key posts_user_idx(user_id)
  78. )
  79. engine=innodb;
  80.  
  81.  
  82. drop table if exists post_comments;
  83. create table post_comments
  84. (
  85. comment_id int unsigned not null auto_increment primary key,
  86. post_id int unsigned not null,
  87. user_id int unsigned not null,
  88. created_date datetime not null,
  89. comment varchar(1024) not null,
  90. key post_comments_posts_idx(post_id),
  91. key post_comments_user_idx(user_id)
  92. )
  93. engine=innodb;
  94.  
  95. -- VIEWS
  96.  
  97. drop view if exists posts_view;
  98. create view posts_view as
  99. select
  100. p.*,
  101. date_format(p.created_date, '%e-%b-%Y') as created_date_fmt,
  102. u.username
  103. from
  104. posts p
  105. inner join users u on p.user_id = u.user_id;
  106.  
  107.  
  108. drop view if exists post_comments_view;
  109. create view post_comments_view as
  110. select
  111. c.*,
  112. date_format(c.created_date, '%e-%b-%Y - %H:%i') as created_date_fmt,
  113. p.subject,
  114. u.username
  115. from
  116. post_comments c
  117. inner join posts p on c.post_id = p.post_id
  118. inner join users u on c.user_id = u.user_id;
  119.  
  120.  
  121. -- STORED PROCEDURES
  122.  
  123. drop procedure if exists list_post_comments;
  124.  
  125. delimiter #
  126.  
  127. create procedure list_post_comments
  128. (
  129. in p_post_id int unsigned
  130. )
  131. proc_main:begin
  132.  
  133. select * from posts_view where post_id = p_post_id;
  134.  
  135. select * from post_comments_view where post_id = p_post_id;
  136.  
  137. end proc_main #
  138.  
  139. delimiter ;
  140.  
  141. -- TEST DATA
  142.  
  143. insert into users (username) values ('f00'),('bar'),('alpha'),('beta');
  144.  
  145. insert into posts (user_id, created_date, subject) values
  146. (1,now() - interval 3 day, 'post 1'),
  147. (2,now() - interval 2 day, 'post 2'),
  148. (3,now() - interval 1 day, 'post 3'),
  149. (4,now(), 'post 4');
  150.  
  151. insert into post_comments (post_id,user_id,created_date,comment) values
  152. (1,2,now(),'post 1 comment 1'),
  153. (1,3,now(),'post 1 comment 2'),
  154. (1,4,now(),'post 1 comment 3');
  155.  
  156. -- TESTING
  157.  
  158. call list_post_comments(1);
Add Comment
Please, Sign In to add comment