[risolto] Nuova pagina lista oggetti (gdrcd) postato il 13/03/2023 15:04:52 nel forum programmazione, gdrcd e open source e modificato da haruka il 15/03/2023 15:13:22
Mi sto perdendo in un bicchier d'acqua, lo so.
In poche parole, voglio permettere alla gestione di poter vedere quali immagini sono presenti nella folder /themes/advanced/imgs/items
considerando che da noi i giocatori possono creare oggetti bypassando la gestione. Un controllo visivo è immediato e possiamo capire a colpo d'occhio se c'è stato un abuso - i giocatori possono solo creare abbigliamento, oggetti di vita quotidiana e non armi e simili.
Ho implementato questo codice, la pagina si chiama "servizi_listaoggettifolder.inc.php" e si trova in pages
Funziona per la prima pagina, funziona anche il sorting, ma se cerco di andare a pagina 2, mi dice "modulo non trovato" che è errore generico di gdrcd.
Che sto sbagliando con la paginazione? ^^'
<?php /*HELP: */
if($_SESSION['permessi'] >= MODERATOR) { /* se almeno admin */
?>
<div>
<!-- Titolo della pagina -->
<div class="page_title">
<h3>Elenco immagini nella cartella dedicata</h3>
</div>
<!-- Operazioni -->
<div class="page_body">
<?php /**/
$path = '***PARTE OSCURATA***/themes/advanced/imgs/items';
// get the directory contents as an array
$dir_contents = scandir($path);
// remove . and .. from the array
$dir_contents = array_diff($dir_contents, array('.', '..'));
// check if the sort option is set in the query string
if (isset($_POST['sort_by'])) {
$sort_by = $_POST['sort_by'];
} else {
$sort_by = 'date_asc'; // default sorting is by date
}
// sort the array based on the selected option
switch ($sort_by) {
case 'name_desc':
sort($dir_contents, SORT_NATURAL | SORT_FLAG_CASE); // sort by name, ascending
break;
case 'date_asc':
$dir_contents = array_map(function($file) use($path) {
return [$file, filemtime("$path/$file")];
}, $dir_contents); // add file modification time to each file name
usort($dir_contents, function($a, $b) {
return $b[1] - $a[1]; // sort by modification time, descending
});
$dir_contents = array_map(function($file) {
return $file[0]; // remove the modification time from the array
}, $dir_contents);
break;
default:
sort($dir_contents, SORT_NATURAL | SORT_FLAG_CASE); // default sorting is by name, ascending
}
// set the number of images per page
$per_page = 200;
// get the current page number from the query string
if (isset($_POST['page'])) {
$page = $_POST['page'];
} else {
$page = 1;
}
// calculate the offset
$offset = ($page - 1) * $per_page;
// slice the array to get the current page's images
$images = array_slice($dir_contents, $offset, $per_page);
// print the images
echo 'IMMAGINI:';
echo '<div style="display: flex; flex-wrap: wrap;">';
foreach ($images as $image) {
// check if the file is an image
if (exif_imagetype("$path/$image")) {
// print the image
echo '<div style="width: 200px; height: 200px; margin: 5px;">';
echo "<a href='../../themes/advanced/imgs/items/$image' target='_blank'><img src='../../themes/advanced/imgs/items/$image' width='200' height='200'></a>";
echo "<div style='text-align: center;'>$image</div>";
echo '</div>';
}
}
echo '</div>';
// calculate the total number of pages
$total_pages = ceil(count($dir_contents) / $per_page);
// print the pagination links
echo '<div style="text-align: center;">';
echo '<form action="main.php?page=servizi_listaoggettifolder" method="post">';
echo '<select name="sort_by">';
echo "<option value='name_desc'".($sort_by == 'name_desc' ? ' selected' : '').">Name (A to Z)</option>";
echo "<option value='date_asc'".($sort_by == 'date_asc' ? ' selected' : '').">Date Edited (Newest First)</option>";
echo '</select>';
echo '<input type="submit" value="Sort">';
echo '</form>';
// loop through the total number of pages and print the pagination links
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $page) {
echo "<span style='font-weight: bold;'>$i</span> ";
} else {
echo "<a href='main.php?page=servizi_listaoggettifolder&page=$i&sort_by=$sort_by'>$i</a> ";
}
}
echo '</div>';
} else { echo '<div class="warning">Non sei abilitato a vedere questa pagina</div>'; }//fine if se non si è admin ?>
</div><!-- operazioni-->
</div><!-- box -->
Leggendra.eu - Fantasy steampunk itinerante
https://www.leggendra.eu
Pagine → 1 2
13/03/2023 16:31:27 e modificato da cbr89 il 13/03/2023 16:47:02
Ciao Haruka,
Probabilmente la variabile $_POST['page'] è vuota perché quando usi il link con href di fatto non vai in post ma in Get.
Quindi secondo me se cambi
$_POST['page']
$_GET['page']
Claudio
13/03/2023 18:25:28
Usare un ?page=qualcosa&page=altro è un po’ rischioso, no? :P
13/03/2023 18:57:37
playergdr ha scritto: Usare un ?page=qualcosa&page=altro è un po’ rischioso, no? :P
Claudio
14/03/2023 13:53:30
Intanto grazie a entrambi. E no, se al posto di _post metto _get non va proprio più il form di selezione.
playergdr ha scritto: Usare un ?page=qualcosa&page=altro è un po’ rischioso, no? :P
main.php?page=messages_center&op=read&group=536&offset=6
Leggendra.eu - Fantasy steampunk itinerante
https://www.leggendra.eu
14/03/2023 20:17:05
haruka ha scritto: Intanto grazie a entrambi. E no, se al posto di _post metto _get non va proprio più il form di selezione.
playergdr ha scritto: Usare un ?page=qualcosa&page=altro è un po’ rischioso, no? :P
main.php?page=messages_center&op=read&group=536&offset=6
$pagina= $_GET['page'];
Claudio
14/03/2023 21:39:08
haruka ha scritto: Intanto grazie a entrambi. E no, se al posto di _post metto _get non va proprio più il form di selezione.
15/03/2023 14:30:55 e modificato da haruka il 15/03/2023 14:39:15
playergdr ha scritto:
Non è quello.
Oltre alla risposta tecnica e molto molto corretta da un punto di vista informatico, (complimenti), io preferisco fartela su un esempio.
La tua ?page=qualcosa ti porta a una specifica pagina.
Se in qualcosa ci aggiunti dei “richiami” come &richiamo=valore&richiamo2=valore2 va benissimo.
Però tu chiami due cose diverse con lo stesso nome e questo rischia di generare un conflitto.
Prova pure a cambiare semplicemente quel page dopo & e via
<?php /*HELP: */
if($_SESSION['permessi'] >= MODERATOR) { /* se almeno admin */
?>
<div>
<!-- Titolo della pagina -->
<div class="page_title">
<h3>Elenco immagini nella cartella dedicata</h3>
</div>
<!-- Operazioni -->
<div class="page_body">
<?php /**/
$path = '**parte oscurata**/themes/advanced/imgs/items';
// get the directory contents as an array
$dir_contents = scandir($path);
// remove . and .. from the array
$dir_contents = array_diff($dir_contents, array('.', '..'));
// check if the sort option is set in the query string
if (isset($_POST['sort_by'])) {
$sort_by = $_POST['sort_by'];
} else {
$sort_by = 'date_asc'; // default sorting is by date
}
// sort the array based on the selected option
switch ($sort_by) {
case 'name_desc':
sort($dir_contents, SORT_NATURAL | SORT_FLAG_CASE); // sort by name, ascending
break;
case 'date_asc':
$dir_contents = array_map(function($file) use($path) {
return [$file, filemtime("$path/$file")];
}, $dir_contents); // add file modification time to each file name
usort($dir_contents, function($a, $b) {
return $b[1] - $a[1]; // sort by modification time, descending
});
$dir_contents = array_map(function($file) {
return $file[0]; // remove the modification time from the array
}, $dir_contents);
break;
default:
sort($dir_contents, SORT_NATURAL | SORT_FLAG_CASE); // default sorting is by name, ascending
}
// set the number of images per page
$per_page = 200;
// get the current page number from the query string
if (isset($_POST['pagination'])) {
$pagination = $_POST['pagination'];
} else {
$pagination = 1;
}
// calculate the offset
$offset = ($pagination - 1) * $per_page;
// slice the array to get the current page's images
$images = array_slice($dir_contents, $offset, $per_page);
// print the images
echo 'IMMAGINI:';
echo '<div style="display: flex; flex-wrap: wrap;">';
foreach ($images as $image) {
// check if the file is an image
if (exif_imagetype("$path/$image")) {
// print the image
echo '<div style="width: 200px; height: 200px; margin: 10px;">';
echo "<a href='../../themes/advanced/imgs/items/$image' target='_blank'><img src='../../themes/advanced/imgs/items/$image' width='200' height='200'></a>";
echo "<div style='text-align: center; height:100%;'>$image</div>";
echo '</div>';
}
}
echo '</div>';
// calculate the total number of pages
$total_pages = ceil(count($dir_contents) / $per_page);
// print the pagination links
echo '<div style="text-align: center;">';
echo '<form action="main.php?page=servizi_listaoggettifolder" method="post">';
echo '<select name="sort_by">';
echo "<option value='name_desc'".($sort_by == 'name_desc' ? ' selected' : '').">Name (A to Z)</option>";
echo "<option value='date_asc'".($sort_by == 'date_asc' ? ' selected' : '').">Date Edited (Newest First)</option>";
echo '</select>';
echo '<input type="submit" value="Sort">';
echo '</form>';
// loop through the total number of pages and print the pagination links
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $pagination) {
echo "<span style='font-weight: bold;'>$i</span> ";
} else {
echo "<a href='main.php?page=servizi_listaoggettifolder&pagination=$i&sort_by=$sort_by'>$i</a> ";
}
}
echo '</div>';
} else { echo '<div class="warning">Non sei abilitato a vedere questa pagina</div>'; }//fine if se non si è admin ?>
</div><!-- operazioni-->
</div><!-- box -->
Leggendra.eu - Fantasy steampunk itinerante
https://www.leggendra.eu
15/03/2023 15:05:05
Ciao Haruka,
Quando segui un link come fai tu per la paginazione , usando
<a href="main.php?paginazione=7" > Link </a>
if(isset($_POST['pagination']))
Claudio
15/03/2023 15:11:56 e modificato da haruka il 15/03/2023 15:12:16
cbr89 ha scritto:...
<?php /*HELP: */
if($_SESSION['permessi'] >= MODERATOR) { /* se almeno admin */
?>
<div>
<!-- Titolo della pagina -->
<div class="page_title">
<h3>Elenco immagini nella cartella dedicata</h3>
</div>
<!-- Operazioni -->
<div class="page_body">
<?php /**/
$path = '**inserire percorso assoluto**/themes/advanced/imgs/items';
// get the directory contents as an array
$dir_contents = scandir($path);
// remove . and .. from the array
$dir_contents = array_diff($dir_contents, array('.', '..'));
// check if the sort option is set in the query string
if (isset($_REQUEST['sort_by'])) {
$sort_by = $_REQUEST['sort_by'];
} else {
$sort_by = 'date_asc'; // default sorting is by date
}
// sort the array based on the selected option
switch ($sort_by) {
case 'name_desc':
sort($dir_contents, SORT_NATURAL | SORT_FLAG_CASE); // sort by name, ascending
break;
case 'date_asc':
$dir_contents = array_map(function($file) use($path) {
return [$file, filemtime("$path/$file")];
}, $dir_contents); // add file modification time to each file name
usort($dir_contents, function($a, $b) {
return $b[1] - $a[1]; // sort by modification time, descending
});
$dir_contents = array_map(function($file) {
return $file[0]; // remove the modification time from the array
}, $dir_contents);
break;
default:
sort($dir_contents, SORT_NATURAL | SORT_FLAG_CASE); // default sorting is by name, ascending
}
// set the number of images per page
$per_page = 200;
// get the current page number from the query string
if (isset($_REQUEST['pagination'])) {
$pagination = $_REQUEST['pagination'];
} else {
$pagination = 1;
}
// calculate the offset
$offset = ($pagination - 1) * $per_page;
// slice the array to get the current page's images
$images = array_slice($dir_contents, $offset, $per_page);
// print the images
echo 'IMMAGINI:';
echo '<div style="display: flex; flex-wrap: wrap;">';
foreach ($images as $image) {
// check if the file is an image
if (exif_imagetype("$path/$image")) {
// print the image
echo '<div style="width: 200px; height: 200px; margin: 10px;">';
echo "<a href='../../themes/advanced/imgs/items/$image' target='_blank'><img src='../../themes/advanced/imgs/items/$image' width='200' height='200'></a>";
echo "<div style='text-align: center; height:100%;'>$image</div>";
echo '</div>';
}
}
echo '</div>';
// calculate the total number of pages
$total_pages = ceil(count($dir_contents) / $per_page);
// print the pagination links
echo '<div style="text-align: center;">';
echo '<form action="main.php?page=servizi_listaoggettifolder" method="post">';
echo '<select name="sort_by">';
echo "<option value='name_desc'".($sort_by == 'name_desc' ? ' selected' : '').">Name (A to Z)</option>";
echo "<option value='date_asc'".($sort_by == 'date_asc' ? ' selected' : '').">Date Edited (Newest First)</option>";
echo '</select>';
echo '<input type="submit" value="Sort">';
echo '</form>';
// loop through the total number of pages and print the pagination links
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $pagination) {
echo "<span style='font-weight: bold;'>$i</span> ";
} else {
echo "<a href='main.php?page=servizi_listaoggettifolder&pagination=$i&sort_by=$sort_by'>$i</a> ";
}
}
echo '</div>';
} else { echo '<div class="warning">Non sei abilitato a vedere questa pagina</div>'; }//fine if se non si è admin ?>
</div><!-- operazioni-->
</div><!-- box -->
Leggendra.eu - Fantasy steampunk itinerante
https://www.leggendra.eu
15/03/2023 15:15:39
😎🎉🎉🎉🎉
Claudio
Discussione seguita da
Pagine → 1 2
Aggiungi ai Preferiti Inoltra Discussione Forum Programmazione, GDRCD e Open Source Elenco Forum
breathofspring ha aperto un annuncio di ricerca: Breath of Spring ricerca Beta Tester
✋😟
Abbiamo rilevato che stai usando un software per bloccare la pubblicità!
L'utilizzo di GDR-online.com è gratuito da oltre 18 anni ma è necessario coprire i costi per mantenerlo online... ti chiediamo di aiutarci disabilitando il blocco dei banner pubblicitari.
Togliere il blocco significa fare in modo che GDR-online.com possa continuare a far conoscere piccoli giochi amatoriali gratuitamente e offrire un servizio sempre migliore e adatto alle esigenze dei suoi giocatori!
Se questo avviso continua ad apparire nonostante tu abbia autorizzato il portale:
- Verifica nelle opzioni che venga disattivato AdBlock in tutto il dominio gdr-online.com e non solo in una pagina
- Controlla di non avere fra le estensioni installate altre analoghe ad AdBlock, come AdBlock Plusbr, ublock origin, ecc.
- Probabilmente sul tuo PC è installato un Antivirus che ha anche funzioni di blocco pubblicità
- Se usi Firefox impostare il "Blocco Contenuti" su "Normale" e non su "Restrittivo". Per farlo clicca sull'icona dello scudo vicino all'URL e clicca su "Disattiva Blocco per questo sito"
- Prova a premere CTRL + F5, riloggarti e vedi se il problema persiste