SitePoint Sponsor

User Tag List

Results 1 to 20 of 20

Threaded View

  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP MYSQL WHERE Clause

    Hi, I've got a problem with using a WHERE clause in my code. Bear with me as I'm new to php. Here it is:

    PHP Code:
    <body>
    <?php
    $username
    ="USERNAME";
    $password="PASSWORD";
    $database="DATABASE";

    mysql_connect(localhost,$username,$password);
    @
    mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM searchacts";
    $result=mysql_query($query);

    // process form when posted 
    if(isset($_POST['value'])) { 
        if(
    $_POST['value'] == 'PriceLow') { 
           
            
    $query "SELECT * FROM searchacts ORDER BY price ASC";   
        }   
        elseif(
    $_POST['value'] == 'PriceHigh') {   
            
            
    $query "SELECT * FROM searchacts ORDER BY price DESC";   
        }
        elseif(
    $_POST['value'] == 'NameAZ') {   
            
            
    $query "SELECT * FROM searchacts ORDER BY name ASC";   
        } 
        
        elseif(
    $_POST['value'] == 'NameZA') {   
            
            
    $query "SELECT * FROM searchacts ORDER BY name DESC";   
        }
        
        elseif(
    $_POST['value'] == 'partybands') {   
            
            
    $query "SELECT * FROM searchacts
    WHERE category='Party Bands'"
    ;   
        }
        
        else {   
            
            
    $query "SELECT * FROM searchacts";   
        }   
        
        
    $result mysql_query($query); 

        } 
        
    $result mysql_query("SELECT * FROM searchacts
    WHERE category='Party Bands'"
    );
        
        
    $num=mysql_numrows($result);

    ?>


    <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >  
        <select name="value">  
            <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'>  
    </form>

    <a href="partybands">Party Bands</a>



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

     
    </body>
    So as you can see I'm outputting the data into divs and there is also a dropdown with ''ORDER BY'' sorting the data. I've also put a WHERE clause after that but when that is there it disables the ''order by''. I also need to connect the WHERE to a link which will activate it as there will be quite a lot of them so it can filter the data.

    Thanks
    Last edited by SpacePhoenix; Feb 25, 2013 at 00:38. Reason: placed php tags around php code


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •