PHP image change order – смяна на подредбата на картинки

Искаше ми се да подобря някои галерии които съм правил за разни сайтове. Не знам как до сега не съм се сетил да вкарам нещо толкова полезно и толкова дребно, като кодиране, като смяната на подредбата или последователността (change order).
PHP picture change order
Ще покжа прост пример, като ще използвам обикновена текстова база данни със следното съдържание:

a=>edno
b=>dve
c=>tri
d=>chetiri
e=>pet

И php скрипта:

<?php
$act = $_GET['act'];
$id = $_GET['id'];
 
$x=0;
 
if( isset($act) && ($act == 'up' || $act == 'down')){
	$arf = file("db.txt");
	foreach($arf as $line){
		list($k, $v) = preg_split('/=>/', rtrim($line));
		$input[$k] = $v;
		if($k == $id){
			$xp = $x;
		}
		$x++;
	}
	$inkey = array_keys($input);
	$inval = array_values($input);
	if($act == 'up'){
		$prevord = array_slice($inval, $xp-1, 1);
		$previd = array_slice($inkey, $xp-1, 1);
		$curord = array_slice($inval, $xp, 1);
 
		$prevord = $prevord[0];
		$previd = $previd[0];
		$curord = $curord[0];
		$curid = $id;
 
		$input[$previd] = $curord;
		$input[$curid] = $prevord;
 
		foreach ($input as $key => $val){
			$write .= $key."=>".$val."\r\n";
		}
		file_put_contents("db.txt", $write);
		header("Location:sortar.php");
		exit;
	} else {
		$nextord = array_slice($inval, $xp+1, 1);
		$nextid = array_slice($inkey, $xp+1, 1);
		$curord = array_slice($inval, $xp, 1);
 
		$nextord = $nextord[0];
		$nextid = $nextid[0];
		$curord = $curord[0];
		$curid = $id; // c
 
		$input[$nextid] = $curord;
		$input[$curid] = $nextord;
 
		foreach ($input as $key => $val){
			$write .= $key."=>".$val."\r\n";
		}
		file_put_contents("db.txt", $write);
		header("Location:sortar.php");
		exit;
	}
}
 
 
 
 
$arf = file("db.txt");
foreach($arf as $line){
	list($k, $v) = preg_split('/=>/', rtrim($line));
	$input[$k] = $v;
}
 
$inkey = array_keys($input);
$inval = array_values($input);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style type="text/css">
body {background: #ededed;}
table {border-collapse:separate; border-spacing:5px; }
table tr td {border: 1px solid #fff;}
table tr td:first-child {background: #ddd;}
table tr td:first-child a {text-decoration: none; color: blue; font-weight: bold; font-size: 24px;}
</style>
</head>
 
<body>
	<table>
	<?php
	for($x=0; $x<count($inval);$x++){ ?>
		<tr>
		<?php if($x==0){ ?>
			<td><a href="sortar.php?act=down&id=<?php echo $inkey[$x]; ?>" >\/</a></td>
			<td><img src="img/<?php echo $inval[$x]; ?>.jpg"></td><td><?php echo $inkey[$x]; ?></td>
		<?php } elseif($x==(count($inval)-1)){ ?>
			<td><a href="sortar.php?act=up&id=<?php echo $inkey[$x]; ?>" >/\</a></td>
			<td><img src="img/<?php echo $inval[$x]; ?>.jpg"></td><td><?php echo $inkey[$x]; ?></td>
		<?php } else { ?>
			<td><a href="sortar.php?act=up&id=<?php echo $inkey[$x]; ?>" >/\</a>
			<a href="sortar.php?act=down&id=<?php echo $inkey[$x]; ?>" >\/</a></td>
			<td><img src="img/<?php echo $inval[$x]; ?>.jpg"></td><td><?php echo $inkey[$x]; ?></td>
		<?php } ?>
		</tr> <?php
	}?>
	</table>	
</body>
</html>

Демо може да се види тук: PHP change order images

Share and Enjoy !

Shares

Leave a Reply

Your email address will not be published. Required fields are marked *

*