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
- <?php
- include "db_photos.php";
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- </head>
- <body>
- <div id="logo">Logo</div>
- <div id="picture">
- <?php
- // Skriv ut bilden
- if($picture){
- echo '<div class="picture">'.$picture.'</div>';
- echo "<br />";
- echo '<a href="test.php?pid='.$prev.'&gid='.$_GET['gid'].'">< Previous</a>';
- echo " || ";
- echo '<a href="test.php?pid='.$next.'&gid='.$_GET['gid'].'">Next ></a>';
- echo '<h1>Kommentarer</h1>';
- if($comments != null){
- foreach($comments as $comment){
- echo $comment."<br />";
- }
- }
- else
- echo "No comments yet..";
- }
- ?>
- <!-- Comments Form -->
- <form action="" method="post">
- <h1>Comment</h1>
- <?php
- if($msg){
- echo '<div class="msg">'.$msg.'</div>';
- }
- ?>
- <textarea id="comment" name="comment"></textarea>
- <div class="clear"></div>
- <p><input type="submit" name="submit" value="Comment" /></p>
- </form>
- </div>
- <div id="footer">Footer</div>
- </body>
- </html>
db_photos.php
- <?php
- include('db_connection.php');
- $err = array();
- $success = array();
- if($_GET['pid'] && $_GET['gid'] && $_GET['gid'] != null){
- $pid = mysql_real_escape_string($_GET['pid']);
- $gid = mysql_real_escape_string($_GET['gid']);
-
- $result = mysql_query("SELECT address FROM photos WHERE id='".$pid."' AND galleryid='".$gid."'");
- if(mysql_num_rows($result) == 1){
- $result = mysql_fetch_assoc($result);
- $address = $result['address'];
- $picture = "<img src='".$address."' />";
-
- // Next
- $result = mysql_query("
- SELECT id
- FROM photos
- WHERE id > '".$pid."'
- ORDER BY id ASC
- LIMIT 1");
- if(mysql_num_rows($result) > 0){
- $result = mysql_fetch_assoc($result);
- $next = $result['id'];
- }
- else{
- $result = mysql_fetch_assoc(mysql_query("
- SELECT id
- FROM photos
- ORDER BY id ASC
- LIMIT 1"));
- $next = $result['id'];
- }
- // Previous
- $result = mysql_query("
- SELECT id
- FROM photos
- WHERE id < '".$pid."'
- ORDER BY id DESC
- LIMIT 1");
- if(mysql_num_rows($result) > 0){
- $result = mysql_fetch_assoc($result);
- $prev = $result['id'];
- }
- else{
- $result = mysql_fetch_assoc(mysql_query("
- SELECT id
- FROM photos
- ORDER BY id DESC
- LIMIT 1"));
- $prev = $result['id'];
- }
-
- $result = mysql_query("
- SELECT comment
- FROM photo_comments
- WHERE pid=".$_GET['pid']."");
- while ($row = mysql_fetch_array($result)){
- $comments[] = $row['comment'];
- }
- }
- else{
- $err[] = 'Your picture doesnt exists';
- }
- }
- if ($_POST['submit']=='Comment'){
- if($_POST['comment'] != null){
- $_POST['comment'] = mysql_real_escape_string($_POST['comment']);
- $_GET['pid'] = mysql_real_escape_string($_GET['pid']);
-
- mysql_query("INSERT INTO photo_comments(pid, comment, dt)
- VALUES(
- '".$_GET['pid']."',
- '".$_POST['comment']."',
- NOW()
- )");
- if(mysql_affected_rows($db_connection)==1){
- $success[]='You have added an comment';
- }
- else{
- $err[]='Something went wrong, please try again';
- }
- }
- else{
- $err[]='You have to write your comment before you can publish it.';
- }
- }
- if(count($err)){
- $msg = $msg.implode('<br />',$err)."<br />";
- }
- if(count($success)){
- $msg = $msg.implode('<br />',$success)."<br />";
- }
- ?>