-5

このような URL パラメータをどのように機能させますか?:

/searchtestingv4.php?categories=rockandpop&sort=低価格

また

/searchtestingv4.php?sort=PriceLow&page=2

ここで私はそれを試しています: http://tinyurl.com/bv6fm7b

ここに私のコードがあります:

ページネーション:

$tableName="searchacts";        
    $targetpage = "searchtestingv4.php";    
    $limit = 5; 

    $query = "SELECT COUNT(*) as num FROM $tableName";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];

    $stages = 3;
    $page = mysql_escape_string($_GET['page']);
    if($page){
        $start = ($page - 1) * $limit; 
    }else{
        $start = 0; 
        }   

    // Get page data
    $query = "SELECT * FROM $tableName LIMIT $start, $limit";
    $result = mysql_query($query);

    // Initial page num setup
    if ($page == 0){$page = 1;}
    $prev = $page - 1;  
    $next = $page + 1;                          
    $lastpage = ceil($total_pages/$limit);      
    $LastPagem1 = $lastpage - 1;                    


    $paginate = '';
    if($lastpage > 1)
    {   




        $paginate .= "<div class='paginate'>";
        // Previous
        if ($page > 1){
            $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
        }else{
            $paginate.= "<span class='disabled'>previous</span>";   }



        // Pages    
        if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
            }
        }
        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
        {
            // Beginning only hide later pages
            if($page < 1 + ($stages * 2))       
            {
                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
            // Middle hide some front and some back
            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
            // End only hide early pages
            else
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
            }
        }

                // Next
        if ($page < $counter - 1){ 
            $paginate.= "<a href='$targetpage?page=$next'>next</a>";
        }else{
            $paginate.= "<span class='disabled'>next</span>";
            }

        $paginate.= "</div>";       


}
 echo $total_pages.' Results';
 // pagination
 echo $paginate;

次の条件でフィルタリングされたカテゴリ:

if($_GET['categories'] == 'rockandpop') {   

        $query = "SELECT * FROM searchacts WHERE category='Rock and Pop'";   
    } 

        if($_GET['categories'] == 'tributebands') {   

        $query = "SELECT * FROM searchacts WHERE category='Tribute Bands'";   
    }  

どこでフィルタされた価格まで、例えば:

// process form when posted 
if(isset($_GET['upto'])) { 
    if($_GET['upto'] == 'upto100') { 

        $query = "SELECT * FROM searchacts WHERE price <= '100'";   
    }   

オプションで並べ替え例:

// process form when posted 
if(isset($_GET['sort'])) { 
    if($_GET['sort'] == 'PriceLow') { 

        $query = "SELECT * FROM searchacts ORDER BY price ASC";   
    }   

フォーム例:

<div id="sortingcontainer"> <span class="sortbyheader">Sort By:</span>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='get' name='form_filter' class="sortoptions" >  
  <select name="sort">  
      <option value="all">All</option>  
      <option value="PriceLow">Price (Low to High)</option>  
      <option value="PriceHigh">Price (High to Low)</option>  
      <option value="NameAZ">Name (A-Z)</option>  
      <option value="NameZA">Name (Z-A)</option>  
      </select>  
    <br />  
    <input type='submit' value = 'Re-Order your results'>  
</form>
  </div>
    <div id="sortingcontainer"> <span class="sortbyheader">Sort By:</span>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='get' name='form_filter' class="sortoptions" >  
  <select name="upto">  
      <option value="upto100">Up to £100</option>  
      <option value="upto200">Up to £200</option>  

      </select>  
    <br />  
    <input type='submit' value = 'Re-Order your results'>  
</form>
  </div>

私のデータをdivに収集します:

<?php
$i=0;
while ($i < $num) {

$image=mysql_result($result,$i,"image");
$name=mysql_result($result,$i,"name");
$category=mysql_result($result,$i,"category");
$description=mysql_result($result,$i,"description");
$stamps=mysql_result($result,$i,"stamps");
$stickmen=mysql_result($result,$i,"stickmen");
$price=mysql_result($result,$i,"price");
$view=mysql_result($result,$i,"view");
$actpagelink=mysql_result($result,$i,"actpagelink");


?>


  <a href="<?php echo $actpagelink; ?>" class="searchitem">
  <div class="searchimage"><img src="<?php echo $image; ?>"/></div>
  <div class="searchtext">
    <div class="searchname"><?php echo $name; ?></div>
    <div class="searchcategory"><?php echo $category; ?></div>
    <div class="searchdescription"><?php echo $description; ?></div>
  </div>
  <div class="searchstamps"><img src="<?php echo $stamps; ?>" /></div>
  <div class="searchstickmen"><img src="<?php echo $stickmen; ?>" /></div>
  <div class="searchprice"><span class="pricefrom">from</span>&pound;<?php echo $price; ?></div>

  <div class="searchview"><img src="<?php echo $view; ?>" /></div>


  </a>

  <?php
$i++;
}


mysql_close();
?>
4

1 に答える 1

1

まず第一に、 でエラーを抑制すること@は非常に悪い考えです。

mysql_*関数がすぐになくなることはないのは事実ですが、mysql拡張機能は非推奨であるため、代わりに PDO または mysqli を使用することをお勧めします。

為に:

/searchtestingv4.php?categories=rockandpop&sort=低価格

次の方法で変数にアクセスできます。

$_GET['categories']; // rockandpop
$_GET['sort']; // PriceLow

為に:

/searchtestingv4.php?sort=PriceLow&page=2

次の方法で変数にアクセスできます。

$_GET['sort']; // PriceLow
$_GET['page']; // 2

の存在をvar次の方法で確認できます。

(isset($_GET['key'])) // is true only if the GET var with key `key` exists

そして、それらを使用してあらゆる種類のロジックを作成できます。たとえば、sort必須にしたい場合は、次のようにします。

if (!isset($_GET['sort'])) { /* print error */ }

また:

if (!in_array('sort', $_GET)) { /* print error */ }
于 2013-03-04T19:33:07.677 に答える