Jump to content

dan292

Members
  • Posts

    10
  • Joined

  • Last visited

Profile Information

  • Gender
    Male
  • Location
    UK

dan292's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Ok I've given up with that script, just cant get it to work. So got this other script which i've got half working: $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; //### Initialize variables $category = isset($_GET['cat']) ? $_GET['cat'] : FALSE ; $subcategory = isset($_GET['categories']) ? $_GET['categories'] : FALSE ; $sort = isset($_GET['sort']) ? $_GET['sort'] : FALSE ; $upto = isset($_GET['upto']) ? $_GET['upto'] : FALSE ; //### Create the URL parameters $urlParameters = '?cat'. $category .'&categories='. $subcategory .'&upto='. $upto .'&sort='. $sort .''; //### Start the query variable $query = 'SELECT * FROM `searchacts`'; //### Initialize query options $queryOptions = array(); //### Now check each of the sorting values //### Check which subcategory we are searching in if($category == 'Bands') { $queryOptions[] = "`category` = 'Bands'"; } else if($category == 'Acoustic') { $queryOptions[] = "`category` = 'Acoustic'"; } else if($category == 'DJ') { $queryOptions[] = "`category` = 'DJ'"; } else if($category == 'AlternativeEntertainment') { $queryOptions[] = "`category` = 'AlternativeEntertainment'"; } //### Now check each of the sorting values //### Check which subcategory we are searching in if($subcategory == 'PopandRock') { $queryOptions[] = "`subcategory` = 'Pop and Rock'"; } else if($subcategory == 'SoulFunkDiscoMowtown') { $queryOptions[] = "`subcategory` = 'Soul, Funk, Disco and Motown'"; } else if($subcategory == 'JazzBigBandSwingBlues') { $queryOptions[] = "`subcategory` = 'Jazz, Big Band, Swing and Blues'"; } else if($subcategory == 'Tribute') { $queryOptions[] = "`subcategory` = 'Tribute'"; } else if($subcategory == 'Cuban') { $queryOptions[] = "`subcategory` = 'Cuban'"; } else if($subcategory == 'Mexican') { $queryOptions[] = "`subcategory` = 'Mexican'"; } else if($subcategory == 'Medieval') { $queryOptions[] = "`subcategory` = 'Medieval'"; } else if($subcategory == 'Strings') { $queryOptions[] = "`subcategory` = 'Strings'"; } else if($subcategory == 'Woodwind') { $queryOptions[] = "`subcategory` = 'Woodwind'"; } else if($subcategory == 'Harpist') { $queryOptions[] = "`subcategory` = 'Harpist'"; } else if($subcategory == 'Piano/GuitarVocalDuo') { $queryOptions[] = "`subcategory` = 'Piano / Guitar Vocal Duo'"; } else if($subcategory == 'SoloPiano') { $queryOptions[] = "`subcategory` = 'Solo Piano'"; } else if($subcategory == 'PianoandVocal') { $queryOptions[] = "`subcategory` = 'Piano and Vocal'"; } else if($subcategory == 'FireArtists') { $queryOptions[] = "`subcategory` = 'FireArtists'"; } else if($subcategory == 'Magicians') { $queryOptions[] = "`subcategory` = 'Magicians'"; } else if($subcategory == 'Caricaturists') { $queryOptions[] = "`subcategory` = 'Caricaturists'"; } else if($subcategory == 'Comedians') { $queryOptions[] = "`subcategory` = 'Comedians'"; } else if($subcategory == 'Acrobats') { $queryOptions[] = "`subcategory` = 'Acrobats'"; } //### Check what value upto we are returning if($upto == '100') { $queryOptions[] = "`price` <= 100"; } else if($upto == '200') { $queryOptions[] = "`price` <= 200"; } else if($upto == '300') { $queryOptions[] = "`price` <= 300"; } else if($upto == '400') { $queryOptions[] = "`price` <= 400"; } else if($upto == '500') { $queryOptions[] = "`price` <= 500"; } else if($upto == '600') { $queryOptions[] = "`price` <= 600"; } else if($upto == '700') { $queryOptions[] = "`price` <= 700"; } else if($upto == '800') { $queryOptions[] = "`price` <= 800"; } else if($upto == '900') { $queryOptions[] = "`price` <= 900"; } else if($upto == '1000') { $queryOptions[] = "`price` <= 1000"; } else if($upto == '1500') { $queryOptions[] = "`price` <= 1500"; } else if($upto == '2000') { $queryOptions[] = "`price` <= 2000"; } else if($upto == '2500') { $queryOptions[] = "`price` <= 2500"; } else if($upto == '3000') { $queryOptions[] = "`price` <= 3000"; } //### Create final query $queryWhere = isset($queryOptions[0]) ? ' WHERE '.implode(' AND ', $queryOptions) : '' ; $finalQuery = $query . $queryWhere; //### Add the order by to the end of the query if($sort == 'PriceLow') { $finalQuery .= ' ORDER BY `price` ASC'; } else if($sort == 'PriceHigh') { $finalQuery .= ' ORDER BY `price` DESC'; } else if($sort == 'NameAZ') { $finalQuery .= ' ORDER BY `name` ASC'; } else if($sort == 'NameZA') { $finalQuery .= ' ORDER BY `name` DESC'; } //echo $finalQuery; //echo '<br>'.$urlParameters; $result = mysql_query($finalQuery); $num = mysql_num_rows($result); ?> But... it only works if I remove this from the original code: //### Start the query variable $query = 'SELECT * FROM `searchacts`'; but then the filters/sorting functions stop working. I would have thought the data select on the pagination would work with the filters aswell: // Get page data $query = "SELECT * FROM $tableName LIMIT $start, $limit"; as its basically the same except from the start limit got the script from here: http://papermashup.com/easy-php-pagination/ Thanks
  2. Ok full code: <?php include 'paginator.class.php'; ?> <?php error_reporting(E_ALL); ini_set("display_errors", 1); $username="username"; $password="password"; $database="database"; mysql_connect('localhost',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); //### Initialize variables $pages = isset($_GET['page']) ? $_GET['page'] : FALSE ; $category = isset($_GET['cat']) ? $_GET['cat'] : FALSE ; $subcategory = isset($_GET['categories']) ? $_GET['categories'] : FALSE ; $sort = isset($_GET['sort']) ? $_GET['sort'] : FALSE ; $upto = isset($_GET['upto']) ? $_GET['upto'] : FALSE ; //### Create the URL parameters $urlParameters = '?cat'. $category .'&categories='. $subcategory .'&upto='. $upto .'&sort='. $sort .'&page='. $pages .''; //### Start the query variable $query = 'SELECT * FROM `searchacts`'; //### Initialize query options $queryOptions = array(); //### Now check each of the sorting values //### Check which category we are searching in if($category == 'Bands') { $queryOptions[] = "`category` = 'Bands'"; } else if($category == 'Acoustic') { $queryOptions[] = "`category` = 'Acoustic'"; } else if($category == 'DJ') { $queryOptions[] = "`category` = 'DJ'"; } else if($category == 'AlternativeEntertainment') { $queryOptions[] = "`category` = 'AlternativeEntertainment'"; } //### Now check each of the sorting values //### Check which subcategory we are searching in if($subcategory == 'PopandRock') { $queryOptions[] = "`subcategory` = 'Pop and Rock'"; } else if($subcategory == 'SoulFunkDiscoMowtown') { $queryOptions[] = "`subcategory` = 'Soul, Funk, Disco and Motown'"; } else if($subcategory == 'JazzBigBandSwingBlues') { $queryOptions[] = "`subcategory` = 'Jazz, Big Band, Swing and Blues'"; } else if($subcategory == 'Tribute') { $queryOptions[] = "`subcategory` = 'Tribute'"; } else if($subcategory == 'Cuban') { $queryOptions[] = "`subcategory` = 'Cuban'"; } else if($subcategory == 'Mexican') { $queryOptions[] = "`subcategory` = 'Mexican'"; } else if($subcategory == 'Medieval') { $queryOptions[] = "`subcategory` = 'Medieval'"; } else if($subcategory == 'Strings') { $queryOptions[] = "`subcategory` = 'Strings'"; } else if($subcategory == 'Woodwind') { $queryOptions[] = "`subcategory` = 'Woodwind'"; } else if($subcategory == 'Harpist') { $queryOptions[] = "`subcategory` = 'Harpist'"; } else if($subcategory == 'Piano/GuitarVocalDuo') { $queryOptions[] = "`subcategory` = 'Piano/GuitarVocalDuo'"; } else if($subcategory == 'SoloPiano') { $queryOptions[] = "`subcategory` = 'Solo Piano'"; } else if($subcategory == 'PianoandVocal') { $queryOptions[] = "`subcategory` = 'Piano and Vocal'"; } else if($subcategory == 'FireArtists') { $queryOptions[] = "`subcategory` = 'FireArtists'"; } else if($subcategory == 'Magicians') { $queryOptions[] = "`subcategory` = 'Magicians'"; } else if($subcategory == 'Caricaturists') { $queryOptions[] = "`subcategory` = 'Caricaturists'"; } else if($subcategory == 'Comedians') { $queryOptions[] = "`subcategory` = 'Comedians'"; } else if($subcategory == 'Acrobats') { $queryOptions[] = "`subcategory` = 'Acrobats'"; } //### Check what value upto we are returning if($upto == '100') { $queryOptions[] = "`price` <= 100"; } else if($upto == '200') { $queryOptions[] = "`price` <= 200"; } else if($upto == '300') { $queryOptions[] = "`price` <= 300"; } else if($upto == '400') { $queryOptions[] = "`price` <= 400"; } else if($upto == '500') { $queryOptions[] = "`price` <= 500"; } else if($upto == '600') { $queryOptions[] = "`price` <= 600"; } else if($upto == '700') { $queryOptions[] = "`price` <= 700"; } else if($upto == '800') { $queryOptions[] = "`price` <= 800"; } else if($upto == '900') { $queryOptions[] = "`price` <= 900"; } else if($upto == '1000') { $queryOptions[] = "`price` <= 1000"; } else if($upto == '1500') { $queryOptions[] = "`price` <= 1500"; } else if($upto == '2000') { $queryOptions[] = "`price` <= 2000"; } else if($upto == '2500') { $queryOptions[] = "`price` <= 2500"; } else if($upto == '3000') { $queryOptions[] = "`price` <= 3000"; } else if($upto == '3500') { $queryOptions[] = "`price` <= 3500"; } else if($upto == '4000') { $queryOptions[] = "`price` <= 4000"; } else if($upto == '4500') { $queryOptions[] = "`price` <= 4500"; } else if($upto == '5000') { $queryOptions[] = "`price` <= 5000"; } else if($upto == '5500') { $queryOptions[] = "`price` <= 5500"; } else if($upto == '6000') { $queryOptions[] = "`price` <= 6000"; } //### Create final query $queryWhere = isset($queryOptions[0]) ? ' WHERE '.implode(' AND ', $queryOptions) : '' ; $finalQuery = $query . $queryWhere; //### Add the order by to the end of the query if($sort == 'PriceLow') { $finalQuery .= ' ORDER BY `price` ASC'; } else if($sort == 'PriceHigh') { $finalQuery .= ' ORDER BY `price` DESC'; } else if($sort == 'NameAZ') { $finalQuery .= ' ORDER BY `name` ASC'; } else if($sort == 'NameZA') { $finalQuery .= ' ORDER BY `name` DESC'; } //### Pagination $query = "SELECT COUNT(*) FROM searchacts {$queryWhere}"; $pages = new Paginator; $pages->items_total = $num_rows[0]; $pages->mid_range = 9; $pages->paginate(); echo $pages->display_pages(); //echo $finalQuery; //echo '<br>'.$urlParameters; //### Create the final query $finalQuery = $query . $queryWhere . $pages->limit; $result = mysql_query($finalQuery); $num = mysql_num_rows($result); ?> <li><a href="<?PHP echo '?categories= &upto='. $upto .'&sort='. $sort .'';?>">All Acts</a></li> <li><a href="<?PHP echo '?cat=Bands&upto='. $upto .'&sort='. $sort .'';?>">Bands</a></li> <li><a href="<?PHP echo '?categories=PopandRock&upto='. $upto .'&sort='. $sort .'';?>">Pop and Rock</a></li> <li><a href=<?PHP echo '?categories=SoulFunkDiscoMowtown&upto='. $upto .'&sort='. $sort .'';?>>Soul, Funk, Disco and Motown</a></li> <li><a href="<?PHP echo '?categories=JazzBigBandSwingBlues&upto='. $upto .'&sort='. $sort .'';?>">Jazz, Big Band, Swing and Blues</a></li> <li><a href=<?PHP echo '?categories=Tribute&upto='. $upto .'&sort='. $sort .'';?>>Tribute</a></li> <li><a href=<?PHP echo '?categories=Cuban&upto='. $upto .'&sort='. $sort .'';?>>Cuban</a></li> <li><a href="<?PHP echo '?categories=Mexican&upto='. $upto .'&sort='. $sort .'';?>">Mexican</a></li> <li><a href=<?PHP echo '?categories=Medieval&upto='. $upto .'&sort='. $sort .'';?>>Medieval</a></li> <li><a href="<?PHP echo '?cat=Acoustic&upto='. $upto .'&sort='. $sort .'';?>">Acoustic</a></li> <li><a href=<?PHP echo '?categories=Strings&upto='. $upto .'&sort='. $sort .'';?>>Strings</a></li> <li><a href=<?PHP echo '?categories=Woodwind&upto='. $upto .'&sort='. $sort .'';?>>Woodwind</a></li> <li><a href=<?PHP echo '?categories=Harpist&upto='. $upto .'&sort='. $sort .'';?>>Harpist</a></li> <li><a href=<?PHP echo '?categories=Piano/GuitarVocalDuo&upto='. $upto .'&sort='. $sort .'';?>>Piano / Guitar Vocal Duo</a></li> <li><a href=<?PHP echo '?categories=SoloPiano&upto='. $upto .'&sort='. $sort .'';?>>Solo Piano</a></li> <li><a href="<?PHP echo '?categories=PianoandVocal&upto='. $upto .'&sort='. $sort .'';?>">Piano and Vocal</a></li> <li><a href="<?PHP echo '?cat=DJ&upto='. $upto .'&sort='. $sort .'';?>">DJ</a></li> <li><a href="<?PHP echo '?cat=AlternativeEntertainment&upto='. $upto .'&sort='. $sort .'';?>">Alternative Entertainment</a></li> <li><a href=<?PHP echo '?categories=FireArtists&upto='. $upto .'&sort='. $sort .'';?>>Fire Artists</a></li> <li><a href=<?PHP echo '?categories=Magicians&upto='. $upto .'&sort='. $sort .'';?>>Magicians</a></li> <li><a href=<?PHP echo '?categories=Caricaturists&upto='. $upto .'&sort='. $sort .'';?>>Caricaturists</a></li> <li><a href=<?PHP echo '?categories=Comedians&upto='. $upto .'&sort='. $sort .'';?>>Comedians</a></li> <li><a href="<?PHP echo '?categories=Acrobats&upto='. $upto .'&sort='. $sort .'';?>">Acrobats</a></li> <div id="sortingcontainer"> <span class="sortbyheader">Sort By:</span> <form action='?' method='get' name='form_filter' class="sortoptions" > <input type="hidden" name="upto" value="<?PHP echo $upto;?>"> <input type="hidden" name="categories" value="<?PHP echo $subcategory;?>"> <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='?' method='get' name='form_filter' class="sortoptions" > <input type="hidden" name="sort" value="<?PHP echo $sort;?>"> <input type="hidden" name="categories" value="<?PHP echo $subcategory;?>"> <select name="upto"> <option value="100">Up to £100</option> <option value="200">Up to £200</option> <option value="300">Up to £300</option> <option value="400">Up to £400</option> <option value="500">Up to £500</option> <option value="600">Up to £600</option> <option value="700">Up to £700</option> <option value="800">Up to £800</option> <option value="900">Up to £900</option> <option value="1000">Up to £1000</option> <option value="1500">Up to £1500</option> <option value="2000">Up to £2000</option> <option value="2500">Up to £2500</option> <option value="3000">Up to £3000</option> <option value="3500">Up to £3500</option> <option value="4000">Up to £4000</option> <option value="4500">Up to £4500</option> <option value="5000">Up to £5000</option> <option value="5500">Up to £5500</option> <option value="6000">Up to £6000</option> </select> <br /> <input type='submit' value = 'Re-Order your results'> </form> </div> <?php $i=0; while ($i < $num) { $image=mysql_result($result,$i,"image"); $name=mysql_result($result,$i,"name"); $subcategory=mysql_result($result,$i,"subcategory"); $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 $subcategory; ?></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>£<?php echo $price; ?></div> <div class="searchview"><img src="<?php echo $view; ?>" /></div> </a> <?php $i++; } mysql_close(); ?> and where i'm testing it: http://tinyurl.com/ct8w7js Its all updated on the server so its as it is above Thanks
  3. I'm trying to add pagination but failing badly Here's my code: $query = "SELECT COUNT(*) FROM searchacts"; $pages = new Paginator; $pages->items_total = $num_rows[0]; $pages->mid_range = 9; $pages->paginate(); echo $pages->display_pages(); //### Initialize variables $pages = isset($_GET['page']) ? $_GET['page'] : FALSE ; $category = isset($_GET['categories']) ? $_GET['categories'] : FALSE ; $sort = isset($_GET['sort']) ? $_GET['sort'] : FALSE ; $upto = isset($_GET['upto']) ? $_GET['upto'] : FALSE ; //### Create the URL parameters $urlParameters = '?categories='. $category .'&upto='. $upto .'&sort='. $sort .'&page='. $pages .''; //### Start the query variable $query = 'SELECT * FROM `searchacts` $pages->limit'; //### Initialize query options $queryOptions = array(); //### Now check each of the sorting values //### Check which category we are searching in if($category == 'PopandRock') { $queryOptions[] = "`category` = 'Pop and Rock'"; } else if($category == 'SoulFunkDiscoMowtown') { $queryOptions[] = "`category` = 'Soul, Funk, Disco and Motown'"; } //### Check what value upto we are returning if($upto == '100') { $queryOptions[] = "`price` <= 100"; } else if($upto == '200') { $queryOptions[] = "`price` <= 200"; } else if($upto == '300') { $queryOptions[] = "`price` <= 300"; } //### Create final query $queryWhere = isset($queryOptions[0]) ? ' WHERE '.implode(' AND ', $queryOptions) : '' ; $finalQuery = $query . $queryWhere; //### Add the order by to the end of the query if($sort == 'PriceLow') { $finalQuery .= ' ORDER BY `price` ASC'; } else if($sort == 'PriceHigh') { $finalQuery .= ' ORDER BY `price` DESC'; } //echo $finalQuery; //echo '<br>'.$urlParameters; $result = mysql_query($finalQuery); $num = mysql_num_rows($result); and i've been trying to use this script/tutorial: http://net.tutsplus.com/tutorials/php/how-to-paginate-data-with-php/ so i've included the paginator.class.php, the $pages->limit on my select and tried to implement it into my url parameters Any Ideas? Thanks
  4. Hi PaulRyan, I thought it was that! So now thats all working.. on to the pagination. I found this script : http://papermashup.com/easy-php-pagination/ and this is the bit where you 'plug it in': $tableName="countries"; $targetpage = "index.php"; $limit = 10; $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 $query1 = "SELECT * FROM $tableName LIMIT $start, $limit"; $result = mysql_query($query1); I've tried numerous ways but can't seem to get it to work and obviously its going to collide with this part of the code: //### Start the query variable $query = 'SELECT * FROM `searchacts`'; So I only need one of these and have tried to combine them with no luck. Maybe I'm missing something? I also didnt include $result = mysql_query($query1); twice either
  5. Brilliant! All the sorting/filter functions are working. Yeah I agree about the all records. So just to go over it: this should select all records: //### Start the query variable $query = 'SELECT * FROM `searchacts`'; which is picked up by this to create final query: //### Create final query $finalQuery = $query. ' WHERE '.implode(' AND ', $queryOptions); which is picked up by this to output the results: $result = mysql_query($finalQuery); Is it expecting there to be a WHERE in the query or something? My code is exactly as I posted on my last post so I haven't changed anything except for the forms. And the only code you haven't seen is the connection: <?php error_reporting(E_ALL); ini_set("display_errors", 1); $username="username"; $password="password"; $database="database"; But it cant be that surely Cheers
  6. Hmm that's what I thought about returning all the records but it doesn't seem to: http://tinyurl.com/c3pxuzf And with the select forms It only seems to apply itself and not to the end of the url, so resets everthing. It works with your input form at the top but i'm assuming that's because its submiting it in one go. Heres my updated code: //### Initialize variables $category = isset($_GET['categories']) ? $_GET['categories'] : FALSE ; $sort = isset($_GET['sort']) ? $_GET['sort'] : FALSE ; $upto = isset($_GET['upto']) ? $_GET['upto'] : FALSE ; //### Create the URL parameters $urlParameters = '?categories='. $category .'&upto='. $upto .'&sort='. $sort .''; //### Start the query variable $query = 'SELECT * FROM `searchacts`'; //### Initialize query options $queryOptions = array(); //### Now check each of the sorting values //### Check which category we are searching in if($category == 'rockandpop') { $queryOptions[] = "`category` = 'Rock and Pop'"; } else if($category == 'tributebands') { $queryOptions[] = "`category` = 'Tribute Bands'"; } //### Check what value upto we are returning if($upto == 'upto100') { $queryOptions[] = "`price` <= 100"; } else if($upto == 'upto200') { $queryOptions[] = "`price` <= 200"; } else if($upto == 'upto300') { $queryOptions[] = "`price` <= 300"; } //### Create final query $finalQuery = $query. ' WHERE '.implode(' AND ', $queryOptions); //### Add the order by to the end of the query if($sort == 'PriceLow') { $finalQuery .= ' ORDER BY `price` ASC'; } else if($sort == 'PriceHigh') { $finalQuery .= ' ORDER BY `price` DESC'; } //echo $finalQuery; //echo '<br>'.$urlParameters; $result = mysql_query($finalQuery); $num = mysql_num_rows($result); ?> <li><a href="searchtestingv6.php">All Acts</a></li> <li><a href="<?PHP echo '?categories=rockandpop&upto='. $upto .'&sort='. $sort .'';?>">Rock and Pop</a></li> <li><a href=<?PHP echo '?categories=tributebands&upto='. $upto .'&sort='. $sort .'';?>>Tribute Bands</a></li> <form method="GET" action="<?PHP echo $urlParameters;?>"> <input type="text" name="sort" value="<?PHP echo $sort;?>" > <input type="text" name="upto" value="<?PHP echo $upto;?>"> <input type="text" name="categories" value="<?PHP echo $category;?>"> <input type="submit" value="Submit"> </form> <div id="sortingcontainer"> <span class="sortbyheader">Sort By:</span> <form action='<?PHP echo $urlParameters;?>' 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> </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 $urlParameters;?>' method='get' name='form_filter' class="sortoptions" > <select name="upto"> <option value="upto100">Up to £100</option> <option value="upto200">Up to £200</option> <option value="upto300">Up to £300</option> <br /> <input type='submit' value = 'Re-Order your results'> </form> </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>£<?php echo $price; ?></div> <div class="searchview"><img src="<?php echo $view; ?>" /></div> </a> <?php $i++; } mysql_close(); ?> I was actually using these: echo $finalQuery; echo '<br>'.$urlParameters; to see what it was outputting Thanks
  7. Hi, ok so at the moment when going onto the page initially there is no category selected.. so no results are displaying i'm guessing if I add this: //### Check which category we are searching in if($category == 'rockandpop') { $queryOptions[] = "`category` = 'Rock and Pop'"; } else if($category == 'tributebands') { $queryOptions[] = "`category` = 'Tribute Bands'"; } else if($category == '') { $queryOptions[] = "`category` = 'ALL HERE?'"; } So if nothing is selected it will display all but is there a way I can specify All records? Also to echo in the select form, how would that be done? would I have to echo the options individually? Something like: <form action='<?PHP echo $urlParameters;?>' method='get' name='form_filter' class="sortoptions" > <select name="sort" > <option value="all">All</option> <option value="<?PHP echo '?categories= $categories . &upto='. $upto .'&sort=PriceLow'. $sort .'';?>">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> Thanks
  8. Hi PaulRyan. Your code makes sense and the comments help to understand it So this is what ive got: error_reporting(E_ALL); ini_set("display_errors", 1); $username="username"; $password="passwordd"; $database="database"; mysql_connect('localhost',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); //### Initialize variables $category = isset($_GET['categories']) ? $_GET['categories'] : FALSE ; $sort = isset($_GET['sort']) ? $_GET['sort'] : FALSE ; $upto = isset($_GET['upto']) ? $_GET['upto'] : FALSE ; //### Create the URL parameters $urlParameters = '?categories='. $category .'&upto='. $upto .'&sort='. $sort .''; //### Start the query variable $query = 'SELECT * FROM `searchacts`'; //### Initialize query options $queryOptions = array(); //### Now check each of the sorting values //### Check which category we are searching in if($category == 'rockandpop') { $queryOptions[] = "`category` = 'Rock and Pop'"; } else if($category == 'tributebands') { $queryOptions[] = "`category` = 'Tribute Bands'"; } //### Check what value upto we are returning if($upto == 'upto100') { $queryOptions[] = "`price` <= 100"; } else if($upto == 'upto200') { $queryOptions[] = "`price` <= 200"; } //### Create final query $finalQuery = $query. ' WHERE '.implode(' AND ', $queryOptions); //### Add the order by to the end of the query if($sort == 'PriceLow') { $finalQuery .= ' ORDER BY `price` ASC'; } else if($sort == 'PriceHigh') { $finalQuery .= ' ORDER BY `price` DESC'; } //### Final query to search and return records from database echo $finalQuery; //### URL parameters, add this to the form action echo '<br>'.$urlParameters; $result = mysql_query($query); $num=mysql_num_rows($result); ?> <div id="sortingcontainer"> <span class="sortbyheader">Sort By:</span> <form action='<?PHP echo $urlParameters;?>' 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> <li><a href="?categories=rockandpop">Rock and Pop</a></li> <li><a href="?categories=tributebands">Tribute Bands</a></li> </div> But at the moment its printing the query on the page like: SELECT * FROM `searchacts` WHERE `category` = 'Rock and Pop' ?categories=rockandpop&upto=&sort= See here: http://tinyurl.com/c3pxuzf So I'm not sure what's going wrong. And how can I use the echos: <?PHP echo $sort;?> <?PHP echo $upto;?> <?PHP echo $category;?> on my select forms and links Also out of interest what is the relevance of the `` eg. in: SELECT * FROM `searchacts` Thanks!
  9. Hi, thanks for the reply. I've got it half working with your suggestion but Its only working for sorting. For example this now works fine: ?categories=rockandpop&sort=PriceHigh but this doesn't: ?categories=rockandpop&upto=upto600 and returns the error: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in I used the same method: if(isset($_GET['upto'])) { if($_GET['upto'] == 'upto100') { if ($query==''){ $query = "SELECT * FROM searchacts WHERE price <= '100'"; }else { $query.= " WHERE price <= '100'"; // Adds this to the end of the query } } elseif($_GET['upto'] == 'upto200') { if ($query==''){ $query = "SELECT * FROM searchacts WHERE price <= '200'"; } else{ $query .= " WHERE price <= '200'"; } } etc, etc I also need a way of adding it automatically to the url if you go into a category. eg. if i'm here: ?categories=rockandpop and sort by 'high to low' I get this: ?categories=rockandpop&sort=PriceHigh I've also got rid of the pagination to make it simpler for the meantime. http://tinyurl.com/c3pxuzf Thanks
  10. Hello all! I need a bit of help with my coding. I'm new to php so be nice! My goal is to fetch data from a database, display it in divs and be able to sort, filter etc. And I have succeeded to a point but have run into a few problems. First you might want to have a look at the users point of view: http://tinyurl.com/bv6fm7b And yes I am using mysql and not mysqli or pdo which I will convert to in the future, but most of the tutorials I've found use mysql so to get my head around it I am using that, I also probably have not put it all together the best way so any suggestions would be great. Ok so the main problem at the moment are my sort/filtering functions. For example if you go to my page, click 'Rock and Pop' and then sort it by 'price high to low' it will forget the 'Rock and Pop' Where clause and vice versa. Also with this problem are the other sorting options and the pagination. I'm using the GET method so I thought I'd try and enter say ?categories=rockandpop&sort=PriceHigh manually to the end of the url, but it only applied the price sorting and not the category. There is a lot of code so I wont post all of it but try to put examples of each function. Categories: 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'"; } Sort By: if(isset($_GET['sort'])) { if($_GET['sort'] == 'PriceLow') { $query = "SELECT * FROM searchacts ORDER BY price ASC"; } elseif($_GET['sort'] == 'PriceHigh') { $query = "SELECT * FROM searchacts ORDER BY price DESC"; } if(isset($_GET['upto'])) { if($_GET['upto'] == 'upto100') { $query = "SELECT * FROM searchacts WHERE price <= '100'"; } elseif($_GET['upto'] == 'upto200') { $query = "SELECT * FROM searchacts WHERE price <= '200'"; } Example of one of the forms: <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> </select> <br /> <input type='submit' value = 'Re-Order your results'> </form> And the data into the divs: <?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>£<?php echo $price; ?></div> <div class="searchview"><img src="<?php echo $view; ?>" /></div> </a> I've also attached my full code if that helps. Thanks code.txt
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.