Mövzu - PHP Yararli Kodlar

PHP Yararli Kodlar



Alt bölmə: PHP
Fəsil: Proqramlaşdırma

Tema faylları
Şərhlər 7
Paylaşım üçün təşəkkürlər
27 Fev 2025, 21:43
0
Tesekkurlər
27 Fev 2025, 19:02
0
paylaşım üçün təşəkkürlər
27 Fev 2025, 17:06
0
2.Pagination
[code=php]
function generatePagination(int $currentPage, int $totalPages): string
{
if ($totalPages < 1) {
return '';
}

$currentPage = max(1, min($currentPage, $totalPages));
$html = '<ul class="pagination">';

// Previous button
if ($currentPage > 1) {
$html .= sprintf(
'<li class="page-item page-item-prev"><a class="page-link" href="%s">&#8592;</a></li>',
filter_html(getPageUrl($currentPage - 1))
);
}

// Calculate page ranges
$startPage = max(1, $currentPage - 1);
$endPage = min($totalPages, $currentPage + 1);

// Show first two pages
if ($currentPage > 4) {
for ($i = 1; $i <= 2; $i++) {
$html .= sprintf(
'<li class="page-item"><a class="page-link" href="%s">%d</a></li>',
filter_html(getPageUrl($i)),
$i
);
}
$html .= '<li class="page-item page-item-disable"><a class="page-link" href="java:void(0)">...</a></li>';
}

// Show pages around current page
for ($i = $startPage; $i <= $endPage; $i++) {
$activeClass = $currentPage === $i ? ' page-item-active' : '';
$html .= sprintf(
'<li class="page-item%s"><a class="page-link" href="%s">%d</a></li>',
$activeClass,
filter_html(getPageUrl($i)),
$i
);
}

// Show last two pages
if ($currentPage < $totalPages - 3) {
$html .= '<li class="page-item page-item-disable"><a class="page-link" href="java:void(0)">...</a></li>';
for ($i = $totalPages - 1; $i <= $totalPages; $i++) {
$html .= sprintf(
'<li class="page-item"><a class="page-link" href="%s">%d</a></li>',
filter_html(getPageUrl($i)),
$i
);
}
}

// Next button
if ($currentPage < $totalPages) {
$html .= sprintf(
'<li class="page-item page-item-next"><a class="page-link" href="%s">&#8594;</a></li>',
filter_html(getPageUrl($currentPage + 1))
);
}

return $html . '</ul>';
}

$perPage = 10;

$stmt = $pdo->query("SELECT COUNT(*) FROM entries");
$total = $stmt->fetchColumn();
$totalPages = (int) ceil($total / $perPage);

$page = (int) ($_GET['page'] ?? 1);
$page = max(1, min($page, $totalPages));

$offset = ($page - 1) * $perPage;

$stmt = $pdo->prepare("SELECT * FROM entries ORDER BY `date` DESC, `id` DESC LIMIT :perPage OFFSET :offset");
$stmt->bindParam(':perPage', $perPage, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$entries = $stmt->fetchAll(PDO::FETCH_ASSOC);

//->use foreach loop in here for list the items.

echo generatePagination($page, $totalPages);
[/code]
27 Fev 2025, 16:28
0
1.Sekil olcusunu deyismek.
[code=php]
$imagePath = __DIR__ . '/images/1.jpg';

$imageSize = getimagesize($imagePath);

$imageType = $imageSize['mime'];

switch ($imageType) {
case 'image/jpeg':
$image = imagecreatefromjpeg($imagePath);
break;
case 'image/png':
$image = imagecreatefrompng($imagePath);
break;
case 'image/gif':
$image = imagecreatefromgif($imagePath);
break;
default:
throw new Exception('Unsupported image type');
}

$width = imagesx($image);
$height = imagesy($image);

$maxDimension = 400;
if ($width > $height) {
$newWidth = $maxDimension;
$newHeight = (int) ($height * $maxDimension / $width);
} else {
$newHeight = $maxDimension;
$newWidth = (int) ($width * $maxDimension / $height);
}

$newImage = imagecreatetruecolor($newWidth, $newHeight);

imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

$newImagePath = __DIR__ . '/images/1-resized.jpg';
imagejpeg($newImage, $newImagePath);

imagedestroy($image);
imagedestroy($newImage);
[/code]
27 Fev 2025, 16:23
0
Bu movzuda belke kimese lazim olar deye, php snippet-lari paylasacam.
27 Fev 2025, 16:21
0
Təbriklər ! Sizin Mövzunuz Əlavə Olundu ! Əgər mövzu Qaydalara uyğun olmazsa silinəcək. Mövzudan kənara çıxmayın, başqasının fikirlərinə hörmətlə yanaşın, probleminiz varsa şəxsidə həll edin, mövzuda artıq post yazmayın. Qeyd: Ailəsinə hörmət edən, qeyrəti olan söyüş söyməz,forum reklam etməz! Bu Yazılanlara Riayət Etmiyənlər Ban Olunacaq və ya Silinəcək!!
27 Fev 2025, 16:21
0
Bağışlayın, sizin üçün şərh yazmaq əlçatan deyil
Geriyə

azTOP.biz
Onlayn istifadəçilər
HAMISI