Jump to content

Recommended Posts

I've been doing basic PHP work on sites for a few years now, but I've never been tempted to learn how the ARRAY function worked, nor in what instances to use it.  Now I've stumbled upon the jCarousel script, and I need to know how to use the ARRAY function to populate the jquery plugin.

 

I have the dynamically driven AJAX version of the plugin downloaded and the example images work fine in the php file which is accessed by the AJAX.  The only problem is I have no idea how to integrate the PHP coding I was using into the new PHP file which calls for the images to be placed (apparently manually?) into an array, like so:

 

<?php

// Array indexes are 0-based, jCarousel positions are 1-based.
$first = max(0, intval($_GET['first']) - 1);
$last  = max($first + 1, intval($_GET['last']) - 1);

$length = $last - $first + 1;

// ---

$images = array(
    'http://static.flickr.com/66/199481236_dc98b5abb3_s.jpg',
    'http://static.flickr.com/75/199481072_b4a0d09597_s.jpg',
    'http://static.flickr.com/57/199481087_33ae73a8de_s.jpg',
    'http://static.flickr.com/77/199481108_4359e6b971_s.jpg',
    'http://static.flickr.com/58/199481143_3c148d9dd3_s.jpg',
    'http://static.flickr.com/72/199481203_ad4cdcf109_s.jpg',
    'http://static.flickr.com/58/199481218_264ce20da0_s.jpg',
    'http://static.flickr.com/69/199481255_fdfe885f87_s.jpg',
    'http://static.flickr.com/60/199480111_87d4cb3e38_s.jpg',
    'http://static.flickr.com/70/229228324_08223b70fa_s.jpg',
);

$total    = count($images);
$selected = array_slice($images, $first, $length);

// ---

header('Content-Type: text/xml');

echo '<data>';

// Return total number of images so the callback
// can set the size of the carousel.
echo '  <total>' . $total . '</total>';

foreach ($selected as $img) {
    echo '  <image>' . $img . '</image>';
}

echo '</data>';

?>

 

Here's the PHP code I was using to display the images:

 

<?php

mysql_connect(localhost,user,pw);
@mysql_select_db(db) or die( "Unable to select database");

$query = "SELECT * FROM related LEFT JOIN film ON related.title = film.title 
WHERE related.name = '$name' ";
$result = mysql_query( $query ) or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
extract($row);

echo '<li><a href="filmography.php?title='.$title.'"><img src="images/thumbnails/'.$image.'.jpg" alt="" title="'.$title.' ('.$year.')"></a></li>';

}
?>

 

Is anyone able to explain to me how I'm supposed to integrate the above PHP script into the array?  Am I going about this all wrong and need to dramatically change the first snippet of code in order to access the database and retrieve the data in the format I want?

 

I sincerely apologize if I sound desperate, but after hours of looking at examples of arrays I still have no idea how I am supposed to use the code from the jCarousel in conjunction with my typical PHP style.

 

Any help would be most appreciated.

Link to comment
https://forums.phpfreaks.com/topic/180823-solved-php-arrays-a-lot-of-confusion/
Share on other sites

Changing this


while ($row = mysql_fetch_array($result))
{
extract($row);

echo '<li><a href="filmography.php?title='.$title.'"><img src="images/thumbnails/'.$image.'.jpg" alt="" title="'.$title.' ('.$year.')"></a></li>';

}

 

to this

 

$images = array();

while ($row = mysql_fetch_array($result))
{
$images[] = "images/thumbnails/{$row['image']}.jpg";
}

 

should load your images urls into $images array.

$images = array();

while ($row = mysql_fetch_array($result))
{
$images[] = "images/thumbnails/{$row['image']}.jpg";
}

 

should load your images urls into $images array.

 

That did the trick wonderfully, but I've realized another problem which deals with my $query.  I have the $query retrieving information from the database based upon the name of an individual who's page is being viewed, like so:

 

$query = "SELECT * FROM related LEFT JOIN film ON related.title = film.title WHERE related.name = '$name' ";

 

Because the PHP is being remotely pulled rom a separate page than the person's page, the $query cannot return any information based upon that WHERE statement.

 

Is there any way to connect the separate PHP page to the page in which the jCarousel is embedded to make the WHERE statement functional?

Since it was apparent AJAX was going to be difficult with what I wanted to do with the $query, I settled for another content slider I found at Tutorialzine.  The script is much more adept at user customization and I found I could place the $query statement within the page the slider was in, thus solving my database problem.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.