如何使用URL参数

时间:2013-03-04 19:14:55

标签: php mysql url parameters

如何制作像这样的网址参数?:

/searchtestingv4.php?categories=rockandpop&sort=PriceLow

/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();
?>

1 个答案:

答案 0 :(得分:1)

首先用@来抑制错误是一个非常糟糕的主意。

虽然mysql_*函数不会很快消失,但建议使用PDO或mysqli,因为mysql扩展名已弃用。

有关:

  

/searchtestingv4.php?categories=rockandpop&sort=PriceLow

您可以使用以下方式访问变量:

$_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 */ }