2012-04-18 179 views
2

我一直在做一些代码,这实际上从MySQL数据下拉并通过PHP显示。结果在页面上一次显示3个,我从来没有遇到任何问题。php sql结果与分页

我最近在数据库项目中添加了一个字段:'售罄'和'库存',并试图(悲惨地)得到两个搜索 - 首先我要显示库存中的项目,然后是那些已售罄。不幸的是,我使用多年的分页代码并不喜欢我运行两个PHP查询,并且只是在页面中添加3个额外的项目(在适用的情况下)。

的完整代码:

 <form name="form1" method="get" action="products.php"> 
      <?php 
     if(!empty($msg)) { 
     echo $msg[0]; 
     } 
     ?> 



      <input name="q" class="textInput2" input type="search" id="q" placeholder="search image name..." autosave="applestyle_srch" results="5" onKeyUp="applesearch.onChange('srch_fld','srch_clear')" />  


       <input name="doSearch" type="hidden" id="doSearch2" value="Search"> 


     <?php if ($get['doSearch'] == 'Search') { 
     // find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM products"; 
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR); 
$r = mysql_fetch_row($result); 
$numrows = $r[0]; 

// number of rows to show per page 
$rowsperpage = 3; 
// find out total pages 
$totalpages = ceil($numrows/$rowsperpage); 

// get the current page or set a default 
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { 
    // cast var as int 
    $currentpage = (int) $_GET['currentpage']; 
} else { 
    // default page num 
    $currentpage = 1; 
} // end if 

// if current page is greater than total pages... 
if ($currentpage > $totalpages) { 
    // set current page to last page 
    $currentpage = $totalpages; 
} // end if 
// if current page is less than first page... 
if ($currentpage < 1) { 
    // set current page to first page 
    $currentpage = 1; 
} // end if 

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage; 


     if($get['q'] == '') { 
    $sql = "SELECT * FROM products WHERE status='sold' ORDER BY `price` ASC LIMIT $offset, $rowsperpage"; 
     } 
     else { 
     $sql = "select * from products where `title` like '%$_REQUEST[q]%' LIMIT $offset, $rowsperpage"; 
     } 

$result1 = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR); 


     if($get['q'] == '') { 
    $sql = "SELECT * FROM products WHERE status='in stock' ORDER BY `price` ASC LIMIT $offset, $rowsperpage"; 
     } 
     else { 
     $sql = "select * from products where `title` like '%$_REQUEST[q]%' LIMIT $offset, $rowsperpage"; 
     } 

$result2 = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR); 


     ?></form></div> 
<div id="pagination" style="float:right; display:inline; margin-right:10px;"> 
      page:<?php 

/****** build the pagination links ******/ 
// if not on page 1, don't show back links 
if ($currentpage > 1) { 
    // show << link to go back to page 1 
    echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search&currentpage=1'><<</a> "; 
    // get previous page num 
    $prevpage = $currentpage - 1; 
    // show < link to go back to 1 page 
    echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search&currentpage=$prevpage'><</a> "; 
} // end if 
     // range of num links to show 
$range = 3; 

// loop to show links to range of pages around current page 
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { 
    // if it's a valid page number... 
    if (($x > 0) && ($x <= $totalpages)) { 
     // if we're on current page... 
     if ($x == $currentpage) { 
     // 'highlight' it but don't make a link 
     echo " [<b>$x</b>] "; 
     // if not current page... 
     } else { 
     // make it a link 
     echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search&currentpage=$x'>$x</a> "; 
     } // end else 
    } // end if 
} // end for 

// if not on last page, show forward and last page links   
if ($currentpage != $totalpages) { 
    // get next page 
    $nextpage = $currentpage + 1; 
    // echo forward link for next page 
    echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search&currentpage=$nextpage'>></a> "; 
    // echo forward link for lastpage 
    echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search&currentpage=$totalpages'>>></a> "; 
} // end if 
/****** end build pagination links ******/ 
?> 
      </div> 

    </div> 
    </div> 


</div></p> 


<div class="category-products"> 


    <form name "searchform" action="products.php" method="post"> 

    <?php while($rrows = mysql_fetch_array($result2)) 
{ 
    echo '<div><div id="searchimage"><a class="product-image" href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">'; 
    echo '<img src="' . $rrows['photo'] . '" width="225" height="150" alt="" title="' . $rrows['title'] . '" /></a></div>'; 
    echo '<div id="searchdetails"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr> 
    <td height="30"><h3 class="product-name"><a href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">' . $rrows['title'] . '</a></h3>'; 
    echo '</td> 
    </tr> 
    <tr> 
    <td>' . $rrows['desc'] . ''; 
    echo '</td> 
    </tr> 
    <tr> 
    <td><h3 class="price">&pound;' . $rrows['price'] . '&nbsp;' . $rrows['pandp'] . '</h3>'; echo '</td> 
    </tr> 
</table></div>'; 
echo '</div>'; 
} 
?> 

<?php while($rrows = mysql_fetch_array($result1)) 
{ 
    echo '<div><div id="searchimage"><a class="product-image" href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">'; 
    echo '<img src="' . $rrows['photo'] . '" width="225" height="150" alt="" title="' . $rrows['title'] . '" /><img src="soldout.png" id="soldout" /></a></div>'; 
    echo '<div id="searchdetails"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr> 
    <td height="30"><h3 class="product-name"><a href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">' . $rrows['title'] . '</a></h3>'; 
    echo '</td> 
    </tr> 
    <tr> 
    <td>' . $rrows['desc'] . ''; 
    echo '</td> 
    </tr> 
    <tr> 
    <td><h3 class="price">&pound;' . $rrows['price'] . '&nbsp;' . $rrows['pandp'] . '</h3>'; echo '</td> 
    </tr> 
</table></div>'; 
echo '</div>'; 
} 
?> 

    </form> <?php } ?> 

我完全可以理解的是,代码可能是更清洁 - (我没有现在的修订过程分页多年来,它一直工作) - 但我假设必须有可以更简单地显示表中的所有项目,而不必同时运行$ result1和$ result2查询。

任何帮助非常感谢!

JD

回答

1

这是我为我编程的电路板所做的功能。我希望这能以某种方式帮助你。 这对我来说有点麻烦,因为我有一段时间没有更新。

这个函数要求的5个必需变量是行数,页面的URL以便链接正常工作,“每页帖子”基本上意味着每页需要多少个结果(默认为20),链接的名称,所以如果你希望下一页出现在页面链接,最后如果你想改变$ _GET你想要改变页面。默认是$ _GET ['page']。

function pagelinks($numrows, $primaryurl, $ppp=20, $aname="", $getpage="default") 
    { 
    global $baseurl; //this is for something like http://somesite.com/ 

    //If the GET method is not defined, $_GET[page] is used. 
    if ($getpage == "default") 
    { 
    $page = intval($_GET['page']); 
    $getpage = "page"; 
    } 
    else 
    { 
    $page = intval($_GET[$getpage]); 
    } 
    $ppp = intval($ppp); 

    if ($aname != "") 
    { 
    $linkname = "#$aname"; 
    //$linkname2 = "<a name=\"next_page\"></a>"; 
    } 

    //If we don't define the posts/articles per page, we go to a postsperpage user default. 
    if ($ppp == 0) 
    { 
    $ppp = 20; 
    } 

    //If the page is negative, we define it 1. 
    if (intval($page) < 0) 
    { 
    $page = 1; 
    } 

    //If the GET page is higher than the highest possible, we make it the highest limiting page. 
    if (($numrows/$ppp + 1) < intval($page)) 
    { 
    $page = intval($numrows/$ppp + 1); 
    } 

    //If the url doesn't point to any page, we just set it to the first group of the query limit. 
    if (!$page) 
    { 
    $page = 1; 
    } 
    $min = (($page - 1) * $ppp); 
    $maxnum = floor($numrows/$ppp); 
    $pages = "<center><small>Pages:</small><br>"; 
    //If we are on the first page, don't link to any previous pages because there are none 
    //Else we link the arrow to the page one less than current. 
    if (($page - 1) <= 0) 
    { 
    $pages .= "<img src=\"$baseurl/boardfiles/images/previous_mono.gif\" border=\"0\" alt=\"\">"; 
    } 
    else 
    { 
    $pages .= "<a href=\"".$primaryurl."&amp;$getpage=".($page-1)."$linkname\"><img src=\"$baseurl/boardfiles/images/previous.gif\" border=\"0\" alt=\"<\"></a>"; 
    } 
    for ($i = 0; $i <= $maxnum; $i++) 
    { 
    $i2 = $i+1; 
    if ($i != $maxnum || ($numrows/$ppp) != (floor($numrows/$ppp)) || !$numrows) 
    { 
     //If the page number matches our page, it isn't linked. 
     //Else we keep linking all existing page numbers. 
    if ($page == $i2) 
     { 
     $pages .= " $i2"; 
     } 
    else 
     { 
     $pages .= " <a href=\"".$primaryurl."&amp;$getpage=".($i + 1)."$linkname\">".($i + 1)."</a>"; 
     } 
    } 
    else 
    { 
     $lastcutoff = 1; 
    } 
    } 
    //If we are on the last page, don't link to the next page because we are on the top one. 
    //Else we link the arrow to the page one more than current. 
    if ($page > $maxnum || ($lastcutoff && $page > $maxnum - 1)) 
    { 
    $pages .= " <img src=\"$baseurl/boardfiles/images/next_mono.gif\" border=\"0\" alt=\"\">"; 
    } 
    else 
    { 
    $pages .= " <a href=\"".$primaryurl."&amp;$getpage=".($page+1)."$linkname\"><img src=\"$baseurl/boardfiles/images/next.gif\" border=\"0\" alt=\">\"></a>"; 
    } 
    $pages .= "</center>"; 
    $return['pages'] = $pages; 
    $return['querylimit'] = " LIMIT $min, $ppp"; 
    $return['min'] = $min; //Used in users.php 
    return $return; 
    } 

现在您需要稍微更改一下代码以适应您的需求,但接下来您需要将其置于查询中。下面是一些实例的使用:

#-- Page Stuff --# 
    $articlesperpage = 10; 
    $numrows = $misc['totalarticles']; 

    $p = pagelinks($numrows, "$baseurl/?p=news", $articlesperpage); 
    $pages = $p['pages']; 
    #-- Page Stuff --# 

    $allnews = sql_query("SELECT * FROM `news` ORDER BY `id` DESC$p[querylimit]"); 

重要的是使用$ P [querylimit]限制你的查询,并使用$您的分页链接的网页。希望这是有用的!




编辑:我最近也创造了这一个使用选择选项列表,而不是一堆链接,您可以代替上述的希望另一分页功能。而且我还修复了一个小时前我发现的错误。有趣的是,当我再次审视这件事时,我是如何抓住它的。

function pagination2($numrows, $primaryurl, $ppp=20) 
    { 
    global $baseurl; 

    $page = intval($_GET['page']); 
    $ppp = intval($ppp); 

    //If we don't define the posts/articles per page, we go to a postsperpage user default. 
    if ($ppp == 0) 
    { 
    $ppp = 20; 
    } 

    //If the page is negative, we define it 1. 
    if (intval($page) < 0) 
    { 
    $page = 1; 
    } 

    //If the GET page is higher than the highest possible, we make it the highest limiting page. 
    if (($numrows/$ppp) < intval($page)) 
    { 
    $page = intval($numrows/$ppp); 
    } 

    //If the url doesn't point to any page, we just set it to the first group of the query limit. 
    if (!$page) 
    { 
    $page = 1; 
    } 
    $min = (($page - 1) * $ppp); 
    $maxnum = floor($numrows/$ppp); 

    $pages = "<select onchange=\"window.location.href=this.value;\" style=\"font-size: 8pt;\">"; 

    for ($i = 0; $i <= $maxnum; $i++) 
    { 
    $i2 = $i + 1; 
    if ($i != $maxnum || ($numrows/$ppp) != (floor($numrows/$ppp)) || !$numrows) 
    { 
    if ($page == $i2) 
     { 
     $pages .= "<option value=\"$primaryurl&amp;page=$i2\" selected>Page $i2</option>"; 
     } 
    else 
     { 
     $pages .= "<option value=\"$primaryurl&amp;page=$i2\">Page $i2</option>"; 
     } 
    } 
    } 

    $pages .= "</select>"; 


    $return['pages'] = $pages; 
    $return['querylimit'] = " LIMIT $min, $ppp"; 
    $return['min'] = $min; //Used in users.php 
    return $return; 
    }