Jump to content


Photo

trouble filtering products with "URL" Help me Pleeeaaase!


  • Please log in to reply
4 replies to this topic

#1 sarah_uk

sarah_uk
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 02 July 2006 - 05:24 PM

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

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 02 July 2006 - 06:02 PM

yes, be more specific. show your current code. 

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

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

right?

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

<?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>";
    }
  }
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 02 July 2006 - 06:07 PM

So you have link like "mypage.php?TypeID=1/2/3/4", yes?

To process,
<?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)";
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 02 July 2006 - 06:14 PM

or yeah, if you meant that instead.. you can reduce barand's code down a bit:

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

oh snap! I outdid Barand!  ;D
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 sarah_uk

sarah_uk
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 02 July 2006 - 08:47 PM

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

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users