Jump to content

Archived

This topic is now archived and is closed to further replies.

sarah_uk

trouble filtering products with "URL" Help me Pleeeaaase!

Recommended Posts

Hello!
Ok, first off i am a total noob at PHP i am making a website for part of my presentation and i have got stuck!
Right what a want is to filter my product types from my database using the URL parameter in Dreamweaver.
I have got my links all working (ending with "TypeID=1/2/3/4 etc") and database sorted, i just need to know how to filter them so it only shows up the products with an ID of 1 or 2 or 3 etc depending on the link.
If you need any more information let me know, i am using phpMyAdmin and Dreamweaver to create the site.
Plleeaaassee help someone!
Sarah

Share this post


Link to post
Share on other sites
yes, be more specific. show your current code. 

here's how i interpret this: you have a link that shows, for example:

[code]
<a href='blah.php?TypeID=1'>Type 1</a>
[/code]

right?

and now you want to do what...select from the database based on TypeID? in blah.php you would do something like this:

[code]
<?php
  if ($_GET['TypeID']) {
    $TypeID = $_GET['TypeID'];
    //good place to sanitize your variable here

    //change tablename to your tablename and id to your column name that holds the id
    $sql = "select * from tablename where id='$TypeID'";
    $result = mysql_query($sql);
  }

  //need to echo the results?
  while ($list = mysql_fetch_array($result)) {
    //$list is now an array of each columnname => value
    //you can echo it out
    echo $list['column1'] . " " . $list['column2'] . "<br>";
    //or store it in another array so you can use it later
    $info[] = $list;
    //or just dump everything out?
    foreach($list as $key => $val) {
      echo $key . " : " . $val . "<br>";
    }
  }
?>
[/code]

Share this post


Link to post
Share on other sites
So you have link like "mypage.php?TypeID=1/2/3/4", yes?

To process,
[code]<?php
$typeArray = explode('/', $_GET['TypeID']);  // split at each / to get an array
$typeList = join (',' , $typeArray);  // now jon them with commas (-->1,2,3,4)
$sql = "SELECT * FROM products WHERE type IN ($typeList)";
?>[/code]

Share this post


Link to post
Share on other sites
or yeah, if you meant that instead.. you can reduce barand's code down a bit:

[code]
<?php
  $typeList = preg_replace('|/|',',',$_GET['TypeID']);
  $sql = "SELECT * FROM products WHERE type IN ($typeList)";
?>
[/code]

oh snap! I outdid Barand!  ;D

Share this post


Link to post
Share on other sites
Thank you everyone for replying! sorry i really dont understand much of what you posted though lol

yes i do have a link with the following code in the "Properties" part:

"products.php?TypeID=<?php echo $row_rsProductTypes['TypeID']; ?>"

the link name has a "repeat region" so it shows up with all the names of the products and when you click on one of them each seperate link ends in a different "?TypeID=1/2/3/4/5" number.

Here is my code for the main bit
[code]
"<?php require_once('Connections/fitnessdatabase.php'); ?>
<?php


$currentPage = $_SERVER["PHP_SELF"];

$maxRows_rsProducts = 4;
$pageNum_rsProducts = 0;
if (isset($_GET['pageNum_rsProducts'])) {
  $pageNum_rsProducts = $_GET['pageNum_rsProducts'];
}
$startRow_rsProducts = $pageNum_rsProducts * $maxRows_rsProducts;

$colname_rsProducts = "-1";
if (isset($_GET['1'])) {
  $colname_rsProducts = (get_magic_quotes_gpc()) ? $_GET['1'] : addslashes($_GET['1']);
}
mysql_select_db($database_fitnessdatabase, $fitnessdatabase);
$query_rsProducts = sprintf("SELECT ProductID, ProductName, Price, Description, Image FROM products WHERE TypeID = 1 ORDER BY ProductName ASC", $colname_rsProducts);
$query_limit_rsProducts = sprintf("%s LIMIT %d, %d", $query_rsProducts, $startRow_rsProducts, $maxRows_rsProducts);
$rsProducts = mysql_query($query_limit_rsProducts, $fitnessdatabase) or die(mysql_error());
$row_rsProducts = mysql_fetch_assoc($rsProducts);

if (isset($_GET['totalRows_rsProducts'])) {
  $totalRows_rsProducts = $_GET['totalRows_rsProducts'];
} else {
  $all_rsProducts = mysql_query($query_rsProducts);
  $totalRows_rsProducts = mysql_num_rows($all_rsProducts);
}
$totalPages_rsProducts = ceil($totalRows_rsProducts/$maxRows_rsProducts)-1;

mysql_select_db($database_fitnessdatabase, $fitnessdatabase);
$query_rsProductTypes = "SELECT TypeID, TypeName FROM producttypes WHERE Online = 1 ORDER BY TypeName ASC";
$rsProductTypes = mysql_query($query_rsProductTypes, $fitnessdatabase) or die(mysql_error());
$row_rsProductTypes = mysql_fetch_assoc($rsProductTypes);
$totalRows_rsProductTypes = mysql_num_rows($rsProductTypes);

$queryString_rsProducts = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_rsProducts") == false &&
        stristr($param, "totalRows_rsProducts") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_rsProducts = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_rsProducts = sprintf("&totalRows_rsProducts=%d%s", $totalRows_rsProducts, $queryString_rsProducts);
?>"[/code]

I hope you understand what i mean? please help! if you want anything else just ask.
Sarah

Share this post


Link to post
Share on other sites

×

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.