Php 在我的“foreach声明”中加入下一个/上一个

Php 在我的“foreach声明”中加入下一个/上一个,php,foreach,Php,Foreach,我想在我的页面上有上一个|下一个链接来更改图片 我使用函数获取相关元素。然而,我不知道在我的函数中需要什么额外的代码,以及在哪里放置它。还有html部分应该包含的内容 我已经看了很多“foreach”的下一页/上一页,但我似乎无法与它们联系起来 代码: Html页面: <a href="">Last</a>| <a href="">Next</a> </div> <?php $image_album_id =$_GET['i

我想在我的页面上有上一个|下一个链接来更改图片

我使用函数获取相关元素。然而,我不知道在我的函数中需要什么额外的代码,以及在哪里放置它。还有html部分应该包含的内容

我已经看了很多“foreach”的下一页/上一页,但我似乎无法与它们联系起来

代码:

Html页面:

<a href="">Last</a>|
<a href="">Next</a>
</div>


<?php
 $image_album_id =$_GET['image_album_id'];
 $image_data = image_data($image_album_id, 'album_id', 'albumname', 'ext', 'timestamp');

 echo '';
 ?>

<td class="smallfont albumthumb2" align="center" valign="middle"  >
<a href="album_viewT.php?=<?php echo 'images/albums/thumbs/'. $image_data['album_id'].        '/'. $image_album_id. '.' .$image_data['ext']; ?> "></a>
<img alt="" class="album_cover" src="<?php echo 'images/albums/thumbs/'.       $image_data['album_id']. '/'. $image_album_id. '.' .$image_data['ext'];?> " height="175px"      width="175px">
</td>
非常感谢。我希望我有道理

谢谢你的迅速回复

因为有很多东西要看和消化,我想我会看看它是否有效

唉,不

有一个分析错误:语法错误,意外
“您的问题缺少一些数据,例如,您访问的生成此代码的页面是什么,即URL,以及您的数据是如何排序的。例如,我可以看到您正在使用album_viewT.php提供潜在的更多信息?脚本,但它不一定是显示您生成的HTML的脚本

因此,在所有这些之后,我将做出一些假设,希望这将给您提供正确的指导,以获得解决方案

我假设你的访问页面是 我还假设数据的顺序是按image\u album\u id。 最后,我假设您的数据库中有数据,我不需要检查当前图像的模板中是否有返回的数据。你需要自己解决这个问题。 您需要首先从数据库中获取数据:

function image_data($image_album_id) 
{
    $results         = array(
                           'previous' => FALSE, 
                           'current'  => FALSE, 
                           'next'     => FALSE,
                       );
    $image_album__id = (int)$image_album_id;
    $args            = func_get_args();
    unset($args[0]);
    $fields = '`'.implode('`, `', $args).'`';

    // Current record 
    $results['current'] = get_data(
                              $fields, 
                              "AND image_album_id = {$image_album_id}"
                          );

    // Previous - no need to run this if we don't have a current
    if ($results['current'])
    {
        // Current record 
        $results['previous'] = get_data(
                                   $fields, 
                                   "AND image_album_id < {$image_album_id} " . 
                                   "LIMIT 1"
                               );
    }

    // Next - no need to run this if we don't have a current
    if ($results['current'])
    {
        // Current record 
        $results['next'] = get_data(
                               $fields, 
                               "AND image_album_id > {$image_album_id} " . 
                               "LIMIT 1"
                           );
    }

    // If all went well the $results array will contain the data 
    // for the 3 records. If we are at the beginning or the end, 
    // the previous and/or next will be FALSE
    return $results;
}

function get_data($fields, $where = '')
{
    $return = FALSE;

    // Template
    $template = "SELECT %s "
              . "FROM album_images "
              . "WHERE member_id = 1 %s";

    // Current record 
    $sql          = sprintf($template, $fields, $where);
    $query        = mysql_query($sql);
    $query_result = mysql_fetch_assoc($query);

    // If data has been found
    if ($query_result)
    {
        $return = $query_result;
    }

    return $return;
}
对于您的HTML页面:

<?php
    $image_album_id = $_GET['image_album_id'];
    $image_data     = image_data(
                          $image_album_id, 
                          'album_id', 'albumname', 'ext', 'timestamp'
                      );

    $prev_link = '';
    $next_link = '';

    if ($image_data['previous'])
    {
        $prev_id   = $image_data['previous']['album_id'];
        $prev_name = $image_data['previous']['albumname'];
        $prev_link = <a href="/albums/id/{$prev_id}" title="$prev_name"}>Previous</a>";
    }

    if ($image_data['next'])
    {
        $next_id   = $image_data['next']['album_id'];
        $next_name = $image_data['next']['albumname'];
        $next_link = <a href="/albums/id/{$next_id}" title="$next_name"}>Next</a>";
    }

    $curr_id   = $image_data['current']['album_id'];
    $curr_ext  = $image_data['current']['ext'];
?>

<?php echo $prev_link; ?>|<?php echo $next_link; ?>
</div>

<td class="smallfont albumthumb2" align="center" valign="middle">
    <a href="album_viewT.php?images/albums/thumbs/
        <?php echo "{$curr_id}/{$image_album_id}.{$curr_ext}; ?>
        <img alt="" class="album_cover" 
             src="images/albums/thumbs/
                 <?php echo "{$curr_id}/{$image_album_id}.{$curr_ext}"; ?>
             height="175px"
             width="175px" />
    </a>
</td>
注意:为了清晰起见,我在HTML文件中拆分了img和a标记的行

<?php
    $image_album_id = $_GET['image_album_id'];
    $image_data     = image_data(
                          $image_album_id, 
                          'album_id', 'albumname', 'ext', 'timestamp'
                      );

    $prev_link = '';
    $next_link = '';

    if ($image_data['previous'])
    {
        $prev_id   = $image_data['previous']['album_id'];
        $prev_name = $image_data['previous']['albumname'];
        $prev_link = <a href="/albums/id/{$prev_id}" title="$prev_name"}>Previous</a>";
    }

    if ($image_data['next'])
    {
        $next_id   = $image_data['next']['album_id'];
        $next_name = $image_data['next']['albumname'];
        $next_link = <a href="/albums/id/{$next_id}" title="$next_name"}>Next</a>";
    }

    $curr_id   = $image_data['current']['album_id'];
    $curr_ext  = $image_data['current']['ext'];
?>

<?php echo $prev_link; ?>|<?php echo $next_link; ?>
</div>

<td class="smallfont albumthumb2" align="center" valign="middle">
    <a href="album_viewT.php?images/albums/thumbs/
        <?php echo "{$curr_id}/{$image_album_id}.{$curr_ext}; ?>
        <img alt="" class="album_cover" 
             src="images/albums/thumbs/
                 <?php echo "{$curr_id}/{$image_album_id}.{$curr_ext}"; ?>
             height="175px"
             width="175px" />
    </a>
</td>