By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

jquery image not loading from array in php

100+
P: 116
i have the jquery loading the image gallery part as follows

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. function mycarousel_itemLoadCallback(carousel, state)
  4. {
  5.     // Check if the requested items already exist
  6.     if (carousel.has(carousel.first, carousel.last)) {
  7.         return;
  8.     }
  9.  
  10.     jQuery.get(
  11.         'dynamic_ajax_php.php',
  12.         {
  13.             first: carousel.first,
  14.             last: carousel.last
  15.         },
  16.         function(xml) {
  17.             mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
  18.         },
  19.         'xml'
  20.     );
  21. };
  22.  
  23. function mycarousel_itemAddCallback(carousel, first, last, xml)
  24. {
  25.     // Set the size of the carousel
  26.     carousel.size(parseInt(jQuery('total', xml).text()));
  27.  
  28.     jQuery('image', xml).each(function(i) {
  29.         carousel.add(first + i, mycarousel_getItemHTML(jQuery(this).text()));
  30.     });
  31. };
  32.  
  33. /**
  34.  * Item html creation helper.
  35.  */
  36. function mycarousel_getItemHTML(id,url)
  37. {
  38.     return '<img src="' + url + '" width="75" height="75" alt="" />';
  39. };
  40.  
  41. jQuery(document).ready(function() {
  42.     jQuery('#mycarousel').jcarousel({
  43.         // Uncomment the following option if you want items
  44.         // which are outside the visible range to be removed
  45.         // from the DOM.
  46.         // Useful for carousels with MANY items.
  47.  
  48.         // itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) { carousel.remove(i); }},
  49.         itemLoadCallback: mycarousel_itemLoadCallback
  50.     });
  51. });
  52.  
  53. </script>
i want that the images should be loaded from the array in php holding the location of the images . my php script is as follows

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. // Array 
  5.  
  6.  
  7. $total = -1;
  8.  
  9. $images = array();
  10. $query = "SELECT images_list.loc FROM images_list ";
  11.     $result = mysql_query($query);
  12.     confirm_query($result);
  13.     $images[]=" ";
  14.         while ($record = mysql_fetch_assoc($result)) {
  15.           $images[] = $record['loc'] ;    
  16.  
  17.           }
  18.           $total    = count($images); 
  19.           $selected = array_slice($images, $first, $length);
  20.  
  21. // ---
  22.  
  23. header('Content-Type: text/xml');
  24.  
  25. echo '<data>';
  26.  
  27. echo '  <total>' . $total . '</total>';
  28.  
  29. foreach ($selected as $img) {
  30.     echo '  <image>' . $img . '</image>';
  31. }
  32.  
  33. echo '</data>';
  34.  
  35.  
  36.  
  37. function confirm_query($r) {
  38.         if (!$r) {
  39.             die("Database query failed: " . mysql_error());
  40.         }
  41.     }
  42. ?>
  43.  

but the images don't load into the gallery....someone plz help
Apr 23 '12 #1
Share this Question
Share on Google+
1 Reply


100+
P: 1,059
I didnt read your code. it wont take me anywhere, I would rather suggest you something.

say x.php would be called by ajax request, and it would return an array to the client. here how you can achieve it:

in x.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $a[0]='data1';
  3. $a[1]='data2';
  4. $a[3]='data3';
  5. $a[4]='data4';
  6. echo "<script>";
  7. echo "var jarray=new Array()";
  8. for($i=0;$i<count($a);$i++)
  9. {
  10.  echo "jarray[".$i."]='".$a[$i]."'";
  11. }
  12. echo "</script>";
  13. ?>
  14.  
in jquery success
Expand|Select|Wrap|Line Numbers
  1. $("#some_hidden_div").html(returned_text_by_PHP);
  2.  
  3. //codes to access jarray
  4.  
at this stage you have the array in a usable status,
Apr 23 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.