i have already made a working script that would load previously uploaded Images (with their title, size, category etc.) into a gallery. It looks like this: https://i.stack.imgur.com/RWASu.jpg
The problem is, i made it load an image of RANDOM ID, there will be more such images after sliding right/left, and i would like to have them displayed by ID, but each other to be the NEXT ID, instead of being this same or random. How could i make it work?
Here is the script so far:
<?php
$result = mysql_query("SELECT id, rozmiar, technika, kategoria, image_time, title FROM {$table} ORDER BY RAND() LIMIT 1");
if (mysql_num_rows($result) == 0)
echo '<ul><li>Nie wgrano żadnych plików.</li>';
else
{
echo '<ul>';
while(list($id, $rozmiar, $technika, $kategoria, $image_time, $title) = mysql_fetch_row($result))
{
echo "<div class='image-frame left-image'>";
echo "<a href='".$_SERVER['PHP_SELF']."?show=$id' data-lightbox='galeria' data-title='{$title}, {$technika} | {$rozmiar}'><img width='450' src='".$_SERVER['PHP_SELF']."?show=$id'></a>";
echo "<div class='description'><p>{$technika}, {$rozmiar}</p></div></div>";
}
echo '</ul>';
}
?>
<?php
$result = mysql_query("SELECT id, rozmiar, technika, kategoria, image_time, title FROM {$table} ORDER BY RAND() LIMIT 1");
if (mysql_num_rows($result) == 0)
echo '<ul><li>Nie wgrano żadnych plików.</li>';
else
{
echo '<ul>';
while(list($id, $rozmiar, $technika, $kategoria, $image_time, $title) = mysql_fetch_row($result))
{
echo "<div class='image-frame right-image'>";
echo "<a href='".$_SERVER['PHP_SELF']."?show=$id' data-lightbox='galeria' data-title='{$title}, {$technika} | {$rozmiar}'><img width='450' src='".$_SERVER['PHP_SELF']."?show=$id'></a>";
echo "<div class='description'><p>{$technika}, {$rozmiar}</p></div></div>";
}
echo '</ul>';
}
?>
Any ideas on how may i make it into one script, that would load multiple blocks with images with ascending ID's? Would be grateful for all suggestions.
The solution would be like this:
Retrieve all the rows from the table without any
ORDER BY
orLIMIT
clause, like this:Count total number of rows retrieved and create an empty
$resultArray
array, like this:Loop through the result set and append the rows to the
$resultArray
array, like this:Use
rand()
function to pick one random row from the$resultArray
array and implement circular array logic from this$resultArray
array to achieve your desired result, like this:Here's the complete code,
Sidenote: Don't use
mysql_*
functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Usemysqli
orpdo
instead. And this is why you shouldn't usemysql_*
functions.