我正在尝试创建一个网站,显示图片和一些关于图片的信息作为个人学习项目。我遇到的问题不一定来自sql查询而是分页。我仔细检查了代码并意识到它是由$ page和$ start引起的,但我不知道如何解决它 代码用于分页和SQL查询
<?php
include ('config/db_connect.php');
/* this is for the pages counter main */
// number of pages
$per_page =4;
//query pages
$pages_query = mysql_query("select count('track_id') from music_db");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset ($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$music_db_query = "select * from music_db limit $start , $per_page";
$music_db_execute = mysql_query($music_db_query);
while($row = mysql_fetch_array($music_db_execute)) {
echo '<link rel="stylesheet" type="text/css" href="css/music.css">';
echo '<div style="background-color:#3a3a3a;">';
echo '<div class="medium-12 columns">';
echo '<article class="mainholder">';
echo '<div class="imageholder"><a href="#"><img src="'.$row['track_art'].'"/></a></div>';
echo '<div class="maindetails"><a href="#"><h5 style="color:white;">'.$row['track_title'].': '.$row['track_artist'].'</h5></a></div>';
$com = $row['track_comments'];
$comred =readMoreFunction($com);
echo '<div class="comments"><a style=" color:white;" href="#"><p>'.$comred .'</p></a></div>';
echo '</article>';
echo '</div>';
echo '</div>';
}
function readMoreFunction($story_desc,$link,$targetFile,$id) {
//Number of characters to show
$chars =210;
$story_desc = substr($story_desc,0,$chars);
$story_desc = substr($story_desc,0,strrpos($story_desc,' '));
$story_desc = $story_desc." <a href='$link?$targetFile=$id'>Read More...</p> </a>";
return $story_desc;
}
if($pages >= 1) {
for($x=1; $x<=$pages; $x++){
echo '<a href ="$page='.$x.'">'.$x.'</a> ';
}
}
mysql_close();
?>
答案 0 :(得分:0)
您的代码是正确的,只是尝试打印结果。看看你得到了什么
由于
答案 1 :(得分:0)
请检查您设置的分页链接:
<?php
include ('config/db_connect.php');
/* this is for the pages counter main */
// number of pages
$per_page =4;
//query pages
$pages_query = mysql_query("select count('track_id') from music_db");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset ($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$music_db_query = "select * from music_db limit $start , $per_page";
$music_db_execute = mysql_query($music_db_query);
while($row = mysql_fetch_array($music_db_execute)) {
echo '<link rel="stylesheet" type="text/css" href="css/music.css">';
echo '<div style="background-color:#3a3a3a;">';
echo '<div class="medium-12 columns">';
echo '<article class="mainholder">';
echo '<div class="imageholder"><a href="#"><img src="'.$row['track_art'].'"/></a></div>';
echo '<div class="maindetails"><a href="#"><h5 style="color:white;">'.$row['track_title'].': '.$row['track_artist'].'</h5></a></div>';
$com = $row['track_comments'];
$comred =readMoreFunction($com);
echo '<div class="comments"><a style=" color:white;" href="#"><p>'.$comred .'</p></a></div>';
echo '</article>';
echo '</div>';
echo '</div>';
}
function readMoreFunction($story_desc,$link,$targetFile,$id) {
//Number of characters to show
$chars =210;
$story_desc = substr($story_desc,0,$chars);
$story_desc = substr($story_desc,0,strrpos($story_desc,' '));
$story_desc = $story_desc." <a href='$link?$targetFile=$id'>Read More...</p> </a>";
return $story_desc;
}
if($pages >= 1) {
for($x=1; $x<=$pages; $x++){
echo '<a href ="?page='.$x.'">'.$x.'</a> '; // Here I have updated file..
}
}
mysql_close();
?>