2012-07-14 59 views
1

如何更改此代码,以便它一次只显示一个图像,并带有下一个和上一个按钮以浏览图像。在php中使用next/prev按钮在sql中显示图像

I used the code from this website

$sql = "select * from people"; 
    $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); 
    while ($row = mysql_fetch_assoc($result)) 
    { 
     echo "<div class=\"picture\">"; 
     echo "<p>"; 

// Note that we are building our src string using the filename from the database 
     echo "<img src=\"images/" . $row['filename'] . "\" alt=\"\" /><br />"; 
     echo $row['fname'] . " " . $row['lname'] . "<br />"; 
     echo "</p>"; 
     echo "</div>"; 

如果没有人愿意帮助,他们可以点我的教程或网站,可能有答案的方向。我是新来的PHP,所以所有的帮助非常感谢。

+0

你用什么来显示图像?灯箱? – lusketeer 2012-07-14 22:48:51

+0

我希望学习如何在代码中调整图像大小,而不是诉诸于lightbox这是它目前的样子:goawaymom.com/test2/images.php – uneducatedguy 2012-07-14 23:08:54

+0

你只是想在页面或整个画廊上展示一张图片,并使用灯箱浏览它们? – lusketeer 2012-07-14 23:13:06

回答

1
$page = $_GET['page'];  
$sql = "select * from people LIMIT $page,1"; 
while(...){ 
    ... 
    $next_page = $page+1; 
    $prev_page = $page-1; 

    $next_btn = "<a href='script.php?page=".$next_page."'>Next</a>"; 
} 

这是一个基本的实现,不要忘记负/最大验证和mysql注入!

+0

回应:'我要使用灯箱吗?'我希望学习如何在代码内调整图像大小,而不是使用lightbox 这是它目前的样子:http://goawaymom.com/test2/images.php 我将尝试代码Dinu Rodnitchi给了我。感谢您的快速回复,如果您对结果感兴趣,我会及时更新。 – uneducatedguy 2012-07-14 23:00:34

0

图像作物。你也可以传递参数的URL,以获取所需输出的图像大小这样image.php?src=img/random.jpg&w=300&h=200

<?php 
header("Content-type: image/jpeg"); 
$image = imagecreatefromjpeg($_GET['src']); 

$thumb_width = $_GET['w']; 
$thumb_height = $_GET['h']; 

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

$original_aspect = $width/$height; 
$thumb_aspect = $thumb_width/$thumb_height; 

if($original_aspect >= $thumb_aspect) { 
    // If image is wider than thumbnail (in aspect ratio sense) 
    $new_height = $thumb_height; 
    $new_width = $width/($height/$thumb_height); 
} else { 
    // If the thumbnail is wider than the image 
    $new_width = $thumb_width; 
    $new_height = $height/($width/$thumb_width); 
} 

$thumb = imagecreatetruecolor($thumb_width, $thumb_height); 

// Resize and crop 
imagecopyresampled($thumb, 
        $image, 
        0 - ($new_width - $thumb_width)/2, // Center the image horizontally 
        0 - ($new_height - $thumb_height)/2, // Center the image vertically 
        0, 0, 
        $new_width, $new_height, 
        $width, $height); 
imagejpeg($thumb); 
?> 

在把一个图像在页面上在时间方面,并通过浏览他们使用一个和下一个按钮。这将需要一些JavaScript来实现。

有许多不错的画廊,一次显示一个图像,他们已经照顾图像调整大小问题。看看HERE