1

I have a page that I'm trying to get AJAX thumbnail replacement & pagination to work. The page loads the initial thumbs: http://www.partiproductions.com/vault_test/1-a-b.php But when clicking nav buttons, it should change the thumbnails, but doesn't.

My click function (forward button):

$('#gort').mouseup (function() {
  var curpg = parseInt($('.thumbwrap').attr('data-cpg'),10);
  console.log('Cur Pg: ' + curpg);
  var newpg = curpg + 1;
  console.log('New Pg: ' + newpg);
  var $totpgs = parseInt($('.thumbwrap').attr('data-tpgs'),10);
  console.log('Total Pages: ' + $totpgs);
  if (newpg > 1) {$('#goleft').show(200)}
  if (newpg == $totpgs) {newpg = $totpgs; $('#gort').hide()}   // limit page to total pages
  if (newpg < $totpgs) {$('#gort').show(200)}
  $('.thumbwrap').attr('data-cpg', newpg);
  var $start = (newpg - 1) * 15;
  console.log('Start: ' + $start);
  // my attempt to pass $start to pagination.php
  $.ajax({
    url:'pagination.php',
    type:'POST',
    data:{start:$start},
    dataType:'JSON'
    success: function(response) {
      thumbparse(response);
    }
  });
});

Currently simple thumbparse function:

 function thumbparse(response) {
   alert(response);
   console.log('Thumbparse output: ' + $navresults);
 }

Pagination.php:

<?php
$thumbst = $_POST['start'];  // capture input from AJAX
echo "Inside Pagination: $thumbst";
require_once 'meekrodb.2.2.class.php';
require_once 'dconnect.php';
// pull from database using specific page items
$navresults = DB::query("SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb, thumb_lg FROM gallery ORDER BY (CASE Alphabet
  WHEN '1' THEN 1
  WHEN '2' THEN 2
  WHEN '3' THEN 3  
  WHEN 'A' THEN 4
  WHEN 'B' THEN 5
  ELSE 6
  END), theme
  LIMIT $thumbst,15");
if(isset($_POST['start']) && !empty($_POST['start'])) {    
  echo json_encode($navresults);
}  
// my attempt to loop through assigned variables that are echoed in main pg   
$x = 0;
foreach ($navresults as $row) {
  $x++;
  if ($x == 1) {
    $t1 = $row['theme'];
    $d1 = $row['developer'];
    $th1 = $row['thumb'];
    $thlg1 = $row['thumb_lg'];
  }
  ... other x's
}

I'm not seeing the echoes from pagination.php How do I get the new thumb data ($navresults) passed back to main page and get that data into the DOM? Code please, as I'm just learning - thanks.

Update 1:

function thumbparse(response) {
  alert(response);
  var obj = JSON.parse(response);
  alert(obj.count);
  console.log('Thumbparse results: ' + $navresults);
}

Update 2:

function thumbparse(response) {
  alert(response);
  console.log(response);
  var x = 0;
  var obj = response;
  $.each(obj, function(key, val) {
    x++;
    var y = x - 1;
    $('ul.thumbwrap .thumb:nth-of-type(y) img').attr({
      alt: obj.theme,
      src: obj.thumb,
      'data-retina': obj.thumb_lg
    });
    $('ul.thumbwrap .thumb:nth-of-type(y) p.hname').text(obj.theme);
    $('ul.thumbwrap .thumb:nth-of-type(y) p.hdev').text(obj.developer);
  });
}

Update 3:

function thumbparse(response) {
  alert(response);
  console.log(response);
  var x = 0;
  var obj = response;
  $.each(obj, function(key, val) {
    x++;
    var y = x - 1;
    console.log('Exist. src: ' + $('.thumb').eq(y).find('img').attr('src')); // correct
    console.log('New src: ' + obj.thumb);  // undefined
    $('.thumb').eq(y).find('img').attr({
      alt: obj.theme,
      src: obj.thumb,
      'data-retina': obj.thumb_lg
    });
    $('.thumb').eq(y).find('p.hname').text(obj.theme);
    $('.thumb').eq(y).find('p.hdev').text(obj.developer);
  });
}

Update 4:

function thumbparse(response) {
  alert(response);
  console.log(response);
  var x = 0;
  var obj = response;
  $.each(obj, function(key, val) {
    x++;
    var y = x - 1;
    console.log('Exist. src: ' + $('.thumb').eq(y).find('img').attr('src'));
    console.log('New src: ' + obj[key].thumb);
    $('.thumb').eq(y).find('img').attr({
      alt: obj[key].theme,
      src: obj[key].thumb,
      'data-retina': obj[key].thumb_lg
    });
    $('.thumb').eq(y).find('p.hname').text(obj[key].theme);
    $('.thumb').eq(y).find('p.hdev').text(obj[key].developer);
    if (obj[key].theme == '') {$('.thumb').eq(y).hide()}
  });
}
parti
  • 205
  • 3
  • 15
  • 1
    You want help with how to treat the json response that comes from pagination.php?? – Hackerman Jun 17 '14 at 17:53
  • Yes, I don't know how to get the response data back into the main page, i.e. change the thumbnails & related info. I guess that would be my thumbparse function? – parti Jun 17 '14 at 18:01
  • Have tried following @Marc B help on this subject : http://stackoverflow.com/questions/14918462/get-response-from-php-file-using-ajax, but still not getting anything. Would really appreciate some help w/ my code & understanding what is wrong w/ it. – parti Jun 17 '14 at 21:43
  • You need to process your json response in the success call....then run throught your json object and find the values do you need and change the dom with the new values via jquery... – Hackerman Jun 17 '14 at 21:50
  • Ok, I don't really understand what to fix. Tried updating thumbparse function, see update 1 above. I'm new to ajax, so posting code would really help me out - thanks. – parti Jun 17 '14 at 21:59
  • Your response looks like `Inside Pagination: 15` and then your json...from where is this message – Hackerman Jun 17 '14 at 22:04
  • That message is an echo in the 2nd line of my pagination.php, as seen above. – parti Jun 17 '14 at 22:08
  • 1
    Ok...first get rid of that line...it's messing up the json response XD – Hackerman Jun 17 '14 at 22:15
  • Ok starting to get somewhere, deleted the echo line. Now getting the alert, which just shows a bunch of objects. So how do I extract the values in the data? Should be 15 sets of thumb data, each w/ theme, developer, thumb & thumb_lg variables. – parti Jun 17 '14 at 22:20
  • 1
    Now get rid oh those lines `var obj = JSON.parse(response);` and `console.log('Thumbparse results: ' + $navresults);` – Hackerman Jun 17 '14 at 22:24
  • Ok, removed those lines, now just the alert in the function – parti Jun 17 '14 at 22:27
  • 1
    Now it's time to use $.each on this json object :) – Hackerman Jun 18 '14 at 01:09
  • And don't forget to add `console.log(response);` in the thumbparse function... – Hackerman Jun 18 '14 at 14:07
  • Ok, I've updated the function, see Update 2. How do I assign the var obj to the big json object? Tried to write the function as best I could figure out, please check. – parti Jun 18 '14 at 18:17
  • It's showing this info on the javascript console `Cur Pg: 1 1-a-b.php:320 New Pg: 2 1-a-b.php:322 Total Pages: 3 1-a-b.php:324 Start: 15` but nothing about the object... – Hackerman Jun 18 '14 at 18:20
  • Whoops, sorry about that - published it now. Note: throws error on var obj because I don't know how to assign it to big json object – parti Jun 18 '14 at 18:58
  • 1
    Try `var obj = response;` – Hackerman Jun 18 '14 at 19:10
  • Ok, assigned obj = response. Now getting error on nth-of-type. Obviously trying to target each thumb & assign the values. – parti Jun 18 '14 at 19:28
  • Ok, look at update 3. Realized I am using jQuery 1.8.3 and nth-of-type isn't supported in that older version. Changed to using eq(). Not sure where error is , as thumbs still not changing on click of forward button. Please take a look at update 3 code. – parti Jun 18 '14 at 20:27
  • 1
    Now you code looks good...you have no error, but nothing is change...can you please put a `console.log($('.thumb').eq(y).find('img'));` inside the .each :) – Hackerman Jun 18 '14 at 21:05
  • Based on your html structure, follow this sample and check the javascript console : http://jsfiddle.net/robertrozas/e2HbA/ – Hackerman Jun 18 '14 at 21:15
  • Ok, it looks like you want to check the src values, so I put console logs in for exist. & new src values - see update 3. The new src values are undefined, isn't 'obj.thumb' the correct syntax? – parti Jun 18 '14 at 21:37
  • 1
    Try this: `console.log('New src: ' + obj[key].thumb);` – Hackerman Jun 18 '14 at 21:41
  • Ok, they're coming in roughly... Found small bug - if using prev. button & going back to page 1, then a null gets thrown - Had been feeding $start=0, is that not correct, as page 1 should list thumbs from beginning of $navresults? – parti Jun 19 '14 at 00:13
  • 1
    So far, so long, but we are making progress, but Chile win to Spain so i have to celebrate(i'm chilean)...as a clue you have to check for the response, and if is null or empty, instead of calling thumbparse you should load the initial values... – Hackerman Jun 19 '14 at 00:36
  • Hmmmm... found 'window.location.reload(true);' which loads loads the entire page again - prefer not to use as it definitely looks different than other page renders. Trying to figure out how to store the initial values - maybe session storage - haven't been able to get that to work? Or is there a way to load the initial vales w/ a dedicated php file & pass those values in? Passing in using echo from 2nd page never seems to work for me. Need direction to go in - obviously ;) – parti Jun 19 '14 at 22:54
  • Worked on it some more & got the initial values to load. Robert, just wanted to say thank you for being so patient & yet having such direct & helpful solutions - a real change from others leading me around in circles ;) Can you post an answer w/ something in it, so I can reward you for answering the question. – parti Jun 20 '14 at 22:33
  • Ok, and here is my email address: robert.rozas.n@gmail.com, feel free to contact me when you have coding problems...you can add me to linkedin either(check my stackoverflow profile)....cheers :) – Hackerman Jun 22 '14 at 02:51

1 Answers1

1

Try this:

function thumbparse(response) {
 console.log(response);
  var x = 0;
  var obj = response;
 $.each(obj, function(key, val) {
 x++;
var y = x - 1;
console.log('Exist. src: ' + $('.thumb').eq(y).find('img').attr('src'));//Actual src
console.log('New src: ' + obj[key].thumb); //new src from json response
$('.thumb').eq(y).find('img').attr({
  alt: obj[key].theme,
  src: obj[key].thumb,
  'data-retina': obj[key].thumb_lg
});
$('.thumb').eq(y).find('p.hname').text(obj[key].theme);
$('.thumb').eq(y).find('p.hdev').text(obj[key].developer);
if (obj[key].theme == '') {$('.thumb').eq(y).hide()}
 });
}
Hackerman
  • 12,139
  • 2
  • 34
  • 45