这是分页还是其他什么?

时间:2012-05-31 16:58:53

标签: php pagination mysqli

我还在学习有关PHP的更多有趣细节。示例:从MySQL迁移到MySQLi。我目前正在尝试输入这样的内容:http://music.daum.net/artist/main?artist_id=2289

通过切割网址从分页中学到的东西:

  • 主?
  • artist_id =
  • 2289

我怎样才能制作这样的页面?我有2个部分可供使用,并在解决这个问题时将其他部分用完。

  • 艺术家信息(以 testhub-artist.php 提供)
  • 专辑(以 testhub-artistalbum.php 提供)
  • 音乐视频
  • 照片部分

我希望在制作网页时更轻松,而不是为每个人制作单独的文件夹。

我的网址是:“../artist/detail?artist_id =#”

<小时/> 这是艺术家页面的顶部。

<?php
//Connect to ...
include "testhub-artist.php";
include "testhub-artistalbum.php";
?>

testhub-artist.php

<?php
//Connect to database
include "mysqli_connect.php";

// Construct our join query
$sql = "SELECT * FROM individuals WHERE soloID = 1";

// Create results
$result = mysqli_query($link, $sql);

// Checking if query is successful
if($result){

// Print out the contents of each row into a table 
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){

// If else states on each variable
    if ($profilepic = $row['profilepic']){
        $profilepic = $row['profilepic'];
    }else{
        $profilepic = "DamjuNoImage";
    }
    if ($engname = $row['engname']){
        $engname = $row['engname'];
    }else{
        $engname = "Unknown";
    }
    if ($korname = $row['korname']){
        $korname = $row['korname'];
    }else{
        $korname = "Unknown";
    }
    if ($engbn = $row['engbn']){
        $engbn = $row['engbn'];
    }else{
        $engbn = "Unknown";
    }
    if ($korbn = $row['korbn']){
        $korbn = $row['korbn'];
    }else{
        $korbn = "Unknown";
    }
    if ($dateofbirth = $row['dateofbirth']){
        $dateofbirth = $row['dateofbirth'];
    }else{
        $dateofbirth = "Unknown";
    }
    if ($occupation = $row['occupation']){
        $occupation = $row['occupation'];
    }else{
        $occupation = "Unknown";
    }
    if ($debut = $row['debut']){
        $debut = $row['debut'];
    }else{
        $debut = "Unknown";
    }
    if ($recordlabel = $row['recordlabel']){
        $recordlabel = $row['recordlabel'];
    }else{
        $recordlabel = "Unknown";
    }
    if ($officialsite = $row['officialsite']){
        $officialsite = $row['officialsite'];
    }else{
        $officialsite = "#";
    }
    if ($sitename = $row['sitename']){
        $sitename = $row['sitename'];
    }else{
        $sitename = "Unknown";
    }
} // End of while statement
}else{
    $engname = "Unknown";
    $korname = "Unknown";
    $engbn = "Unknown";
    $korbn = "Unknown";
    $dateofbirth = "Unknown";
    $occupation = "Unknown";
    $debut = "Unknown";
    $recordlabel = "Unknown";
    $officialsite = "#";
    $sitename = "Unknown";
} // End of If statement

// Free result set
//mysqli_free_result($result);

?>

testhub-artistalbum.php

<?php
//connect to db
include "mysqli_connect.php";

//check for a page number. If not, set it to page 1
if (!(isset($_GET['albumpage']))){
    $albumpage = 1;
}else{
    $albumpage = $_GET['albumpage'];
}

//query for record count to setup pagination
$sqli = "SELECT * FROM albums WHERE soloID = 3";
$album_data = mysqli_query($link, $sqli);
$album_rows = mysqli_num_rows($album_data); 

//number of photos per page
$album_pagerows = 4; 

//get the last page number
$last_album = ceil($album_rows/$album_pagerows); 

//make sure the page number isn't below one, or more than last page num
if ($albumpage < 1){
    $albumpage = 1;
}elseif ($albumpage > $last_album){
    $albumpage = $last_album;
}

//Set the range to display in query
$max_album = 'limit ' .($albumpage - 1) * $album_pagerows .',' .$album_pagerows;

//get all of the photos
$albumList = "";
$sqli2 = "SELECT * FROM albums WHERE soloID = 3 ORDER BY releasedate DESC $max_album";
$album_sql = mysqli_query($link, $sqli2);

//check for photos
$albumCount = mysqli_num_rows($album_sql);

if ($albumCount > 0){
    while($album_rows = mysqli_fetch_array($album_sql)){
    $albumID = $album_rows["albumID"];
    $albumpic = $album_rows["albumpic"];
    $title = $album_rows["albumTitle"];
    $releasedate = $album_rows["releasedate"];
    $page = $album_rows["page"];
    $albumList .= '
      <li class="albumthumb">
         <a href="' . $page . '" title="' . $title . '"><img class="profile" src="../albums/album_th/' . $albumpic . '.jpg" alt="' . $albumpic . '" width="120" height="120" border="0" /><p class="datatitle">' . $title . '</p></a><p class="data-releasedate">' . $releasedate . '</p>
       </li>
                  ';
    }
}else{
    $albumList = "There are no available albums at this time!";
}

//mysql_close();
?>

很抱歉没有清楚解释。我希望能够在制作像网址这样的个人资料页面时使用分页。我想使用url中的数字来更改sql代码中的id(soloID)。

节省时间的好主意,对吗? MySQLi每次看到它都会变得更容易。

谢谢。

<小时/> 修改时间:5/31/2012 5:44 PM CT

$artist = $_GET['artist_id']

    if(is_numeric($_GET['artist_id'])){
    $artist = $_GET['artist_id'];
}else{
    $artist = 1;
}

2 个答案:

答案 0 :(得分:2)

artist/detail?artist_id=#

您可以使用detail作为页面(可能有一个带索引的详细文件夹),并在详细信息页面上,在某处获取artist_id的$_GET[]变量。所以你的代码看起来像这样:

$artist = $_GET['artist_id']; // Filter this variable

$sql = "SELECT * FROM individuals WHERE soloID = '{$artist}'";

/**
 * Verify if the ID exists
 * Display query results, etc.
 */

因此,每次更改URL中的artist_id变量时,页面都应相应更改。

答案 1 :(得分:1)

欢迎来到我的第二个喜欢的语言!我喜欢php。 有人已经回答了你的问题,但我有一些建议。

您拥有的代码不易受到影响,因为用户提供的数据通过数学传递......但内联变量是让您自己对SQL Injection攻击开放的好方法。查找bind_param()并准备好陈述并养成使用它们的习惯。总是。几乎总是......

不幸的是,SQL不允许您绑定用于LIMIT,ORDER BY,GROUP BY的值之类的内容,因此您必须自己处理这些值。 永远不要相信任何来自用户的东西,所以要做好工作并检查它。

排序列应始终为列名。检查一下。

if ( ! in_array($sort_column,array('column1','column2','column3') ) ) $sort_column = 'column1';

限制应始终为整数。像这样投下它们。

$safe_limit = (int) $user_limit;

无需将数组值复制到另一个变量中。直接使用它们。

你需要将你的值转义为html。查找urlencode()htmlentities()

我的IE需要大量的内存,所以我必须稍后完成。