Is it possible to do this with jquery and how? Sample code in php & mysql

Is it possible to do this with jquery and how? Sample code in php & mysql

Hello!

I'm really new to jquery and doesn't know if it's possible to do this with it but please help me.

I have coded a really simple image gallery with php and mysql to try to get it work with jquery, far from the best I have coded and not so secure but its just for testing so just help me with the jquery.

I have a next button and a previous button and if the user presses at one those I just want to change my content in my picture div. I want it to work like facebook gallery, if a user clicks at next it changes picture, comment and address in my browsers addressfield. I want mine to do the same. I want my next / previous buttons to work the same way as now but with jquery so I doesn't need to update my whole page every time I click on next or previous.

I know that I haven't attached any jquery code but its because I'm stranded and doesn't know where to begin.

So please help with this or to help me start with my next / previous buttons.

test.php
  1. <?php
  2. include "db_photos.php";
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8. </head>
  9. <body>
  10. <div id="logo">Logo</div>
  11. <div id="picture">
  12. <?php
  13. // Skriv ut bilden
  14. if($picture){
  15. echo '<div class="picture">'.$picture.'</div>';
  16. echo "<br />";
  17. echo '<a href="test.php?pid='.$prev.'&gid='.$_GET['gid'].'">< Previous</a>';
  18. echo " || ";
  19. echo '<a href="test.php?pid='.$next.'&gid='.$_GET['gid'].'">Next ></a>';
  20. echo '<h1>Kommentarer</h1>';
  21. if($comments != null){
  22. foreach($comments as $comment){
  23. echo $comment."<br />";
  24. }
  25. }
  26. else
  27. echo "No comments yet..";
  28. }
  29. ?>
  30. <!-- Comments Form -->
  31. <form action="" method="post">
  32. <h1>Comment</h1>
  33. <?php
  34. if($msg){
  35. echo '<div class="msg">'.$msg.'</div>';
  36. }
  37. ?> 
  38. <textarea id="comment" name="comment"></textarea>
  39. <div class="clear"></div>
  40. <p><input type="submit" name="submit" value="Comment" /></p>
  41. </form>
  42. </div>
  43. <div id="footer">Footer</div>
  44. </body>
  45. </html>

db_photos.php
  1. <?php

  2. include('db_connection.php');

  3. $err = array();
  4. $success = array();

  5. if($_GET['pid'] && $_GET['gid'] &&  $_GET['gid'] != null){
  6. $pid = mysql_real_escape_string($_GET['pid']);
  7. $gid = mysql_real_escape_string($_GET['gid']);
  8. $result = mysql_query("SELECT address FROM photos WHERE id='".$pid."' AND galleryid='".$gid."'");
  9. if(mysql_num_rows($result) == 1){
  10. $result = mysql_fetch_assoc($result);
  11. $address = $result['address'];
  12. $picture = "<img src='".$address."' />";
  13. // Next
  14. $result = mysql_query("
  15. SELECT id
  16. FROM photos
  17. WHERE id > '".$pid."'
  18. ORDER BY id ASC 
  19. LIMIT 1");
  20. if(mysql_num_rows($result) > 0){
  21. $result = mysql_fetch_assoc($result);
  22. $next = $result['id'];
  23. }
  24. else{
  25. $result = mysql_fetch_assoc(mysql_query("
  26. SELECT id
  27. FROM photos
  28. ORDER BY id ASC 
  29. LIMIT 1"));
  30. $next = $result['id'];
  31. }
  32. // Previous
  33. $result = mysql_query("
  34. SELECT id
  35. FROM photos
  36. WHERE id < '".$pid."'
  37. ORDER BY id DESC 
  38. LIMIT 1");
  39. if(mysql_num_rows($result) > 0){
  40. $result = mysql_fetch_assoc($result);
  41. $prev = $result['id'];
  42. }
  43. else{
  44. $result = mysql_fetch_assoc(mysql_query("
  45. SELECT id
  46. FROM photos
  47. ORDER BY id DESC
  48. LIMIT 1"));
  49. $prev = $result['id'];
  50. }
  51. $result = mysql_query("
  52. SELECT comment
  53. FROM photo_comments
  54. WHERE pid=".$_GET['pid']."");
  55. while ($row = mysql_fetch_array($result)){ 
  56. $comments[] = $row['comment'];
  57. }
  58. }
  59. else{
  60. $err[] = 'Your picture doesnt exists';
  61. }
  62. }

  63. if ($_POST['submit']=='Comment'){
  64. if($_POST['comment'] != null){
  65. $_POST['comment'] = mysql_real_escape_string($_POST['comment']);
  66. $_GET['pid'] = mysql_real_escape_string($_GET['pid']);
  67. mysql_query("INSERT INTO photo_comments(pid, comment, dt)
  68. VALUES(
  69. '".$_GET['pid']."',
  70. '".$_POST['comment']."',
  71. NOW()
  72. )");
  73. if(mysql_affected_rows($db_connection)==1){
  74. $success[]='You have added an comment';
  75. }
  76. else{
  77. $err[]='Something went wrong, please try again';
  78. }
  79. }
  80. else{
  81. $err[]='You have to write your comment before you can publish it.';
  82. }
  83. }

  84. if(count($err)){
  85. $msg = $msg.implode('<br />',$err)."<br />";
  86. }
  87. if(count($success)){
  88. $msg = $msg.implode('<br />',$success)."<br />";
  89. }
  90. ?>
    • Topic Participants

    • uffe