[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Extending query according to changes at address bar

Posted on 2005-05-08
10
Medium Priority
?
229 Views
Last Modified: 2010-08-05
Hi Guys

Let say we have http://www.mydomain.com/product.php?cpu=133mhz at address bar and our query is select * from product_table where cpu=133mhz and link which shows cpu, ram, price. When user clicks to ram, address bar will change to http://www.mydomain.com/product.php?cpu=133mhz&ram=512mb and query will be select * from product_table where cpu=133mhz AND ram=512mb. So I will help user to select his item which is best for him. I would like to get logic of adding &ram=512 to the end of address bar and add AND ram=512 to the end of query. What would your suggestion be?

Kind regards
0
Comment
Question by:hasozduru
  • 4
  • 4
  • 2
10 Comments
 
LVL 2

Expert Comment

by:dKasipovic
ID: 13955722
Try to use drop down boxes in a form. When the link is &cpu=500mHz the drop down box named cpu will show 500MhZ then u put another drop box called ram and let user choose how much ram they want... and so on...
You would need to code it all, but that's not too hard, and neither is too big. If you need url to show parameters, set form method to GET, otherwise set it to POST...
If you need further help on programming the actual site, let me know...
Hope I helped.
0
 

Author Comment

by:hasozduru
ID: 13955727
dKasipovic, I don't want to use drop down boxes.

Thanks
0
 
LVL 9

Assisted Solution

by:gruntar
gruntar earned 1600 total points
ID: 13955742
Hi

first you set up base query

$q_select = "SELECT field, field2";
$q_from = " FROM table1, table2";
$q_where = " WHERE cpu='" . $_GET['cpu'] . "'";

// than if you have additional field that should get included in query you just add it

if(isset($_GET['ram'])){
    $q_where .= " AND ram='" . $_GET['ram'] . "'";
}

// note .= that means you append to previous value of that var $q_where
// than you execute query

$sql_q = mysql_query($q_select . $q_from . $q_where);


cheers
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:hasozduru
ID: 13955759
Ok

How would you do for the following code.

<?
include("dbconnect.php");

$sql = mysql_query("select * from spec_palmtop group by first_field");
echo '<table border=1>';
echo '<td><table border=1>';
while ($row = mysql_fetch_object($sql)) {
      echo "<tr><td><a href=\"" . $_SERVER["PHP_SELF"] . "?first_field=" . $row->first_field . "\">$row->first_field</a></td>";
}

echo '</tr></table>';
$sql2 = mysql_query("select * from spec_palmtop group by second_field");
echo '<td><table border=1>';
while ($row2 = mysql_fetch_object($sql2)) {
      echo "<tr><td><a href=\"" . $_SERVER["PHP_SELF"] . "?second_field=" . $row2->second_field . "\">$row2->second_field</a></td>";
}
echo '</table>';
echo '</table>';
?>
0
 
LVL 9

Assisted Solution

by:gruntar
gruntar earned 1600 total points
ID: 13955772
ups, wrong answer. should have some sleep :)

You should set some default values...

if($_GET['cpu'] < 500){
 // slow machine
  echo '<a href="http://www.mydomain.com/product.php?cpu=133&ram=256">256MB ram</a>';
} elseif($_GET['cpu'] < 1500){
 // moderate machine
  echo '<a href="http://www.mydomain.com/product.php?cpu=1200&ram=512">512MB ram</a>';
} else {
 // super fast machine
  echo '<a href="http://www.mydomain.com/product.php?cpu=3000&ram=1024">124MB ram</a>';
}

something like that :)
0
 

Author Comment

by:hasozduru
ID: 13955783
gruntar

Please go to http://dealtime.co.uk/xPP-Personal_Organisers and see Price Range: Brand: Operating System: . I would like to do filtering like that. I cannot do it like your above example.

Thanks
0
 
LVL 2

Assisted Solution

by:dKasipovic
dKasipovic earned 400 total points
ID: 13957960

$filter='';

if ($_GET["cpu"]) { $filter=$filter." AND CPU=".$_GET["cpu"]; }
if ($_GET["ram"]) { $filter=$filter." AND RAM=".$_GET["ram"]; }

$sql = mysql_query("select * from spec_palmtop WHERE 1=1 ".$filter."group by first_field"); //you can add some WHERE clause which make no sense but works (i.e. WHERE id>0, or WHERE 1=1), and then you could form the filter according to the url...
0
 
LVL 9

Accepted Solution

by:
gruntar earned 1600 total points
ID: 13959805
Sorry for late response. Had to go to bed yesterday...  back to your question

if you wanna to do suggestions like have (Price Range, Brand...) than all you have to do is to select and group data (like you already did).

They select and count values (number of products) for let say Brand.

table brands
id | brand
1  | HP
2  | Hitachi
...

table products
id, product,  brand_id ...
1    , Hp 001,        1
...
13  , Hp 025,        1
400, Hitachi ppc ,  2
 

than you make query like this
SELECT COUNT(p.id) AS total, b.id AS brand_id, b.brand AS brand
FROM products p, brands b
WHERE p.brand_id = b.id
GROUP BY p.brand_id

then you echo out links

while ($row = mysql_fetch_array($sql_query)){
   echo '<a href="linktoselectbybrand.php?brand=' . $row['brand_id'] . '">' . $row['brand']  . ' (' . $row['total'] . ')</a><br>';
}

result should be something like this

<a href="linktoselectbybrand.php?brand=1">HP (2)</a><br>
<a href="linktoselectbybrand.php?brand=2">Hitachi (1)</a><br>

hope this is what you want..
0
 

Author Comment

by:hasozduru
ID: 13964106
No  gruntar, I am not looking for numbers side by brands. Try the following code and see how it works. I need something like that.

Thanks


<?
include("dbconnect.php");
$URL = $HTTP_SERVER_VARS['REQUEST_URI'];

$sql = mysql_query("select * from spec_palmtop group by first_field");
echo '<table border=1>';
echo '<td><table border=1>';
while ($row = mysql_fetch_object($sql)) {
      $link = "<tr><td><a href=\"" . $URL;
      if(!preg_match("/\?/",$URL))
        $link .="?";
      else
        $link .="&";
      $link .="first_field=" . $row->first_field . "\">$row->first_field</a></td>";
      echo $link;
}

echo '</tr></table>';
$sql2 = mysql_query("select * from spec_palmtop group by second_field");
echo '<td><table border=1>';
while ($row2 = mysql_fetch_object($sql2)) {
      $link = "<tr><td><a href=\"" . $URL;
      if(!preg_match("/\?/",$URL))
        $link .="?";
      else
        $link .="&";
      $link .="second_field=" . $row2->second_field . "\">$row2->second_field</a></td>";
      echo $link;
}

echo '</table>';
echo '</table>';

if($first_field and !$second_field) {
  $q_list = mysql_query("select * from product, spec_palmtop where product.category = 1 and product.product_id = spec_palmtop.product_id and first_field = '$first_field'");
}
elseif (!$first_field and $second_field) {
  $q_list = mysql_query("select * from product, spec_palmtop where product.category = 1 and product.product_id = spec_palmtop.product_id and second_field = '$second_field'");
}
elseif ($first_field and $second_field) {
  $q_list = mysql_query("select * from product, spec_palmtop where product.category = 1 and product.product_id = spec_palmtop.product_id and first_field = '$first_field' and second_field = '$second_field'");
}
else{
  $q_list = mysql_query("select * from product, spec_palmtop where product.category = 1 and product.product_id = spec_palmtop.product_id");
}
while ($q_row = mysql_fetch_object($q_list)) {
      echo "$q_row->p_name<br>";
}

?>
0
 
LVL 9

Expert Comment

by:gruntar
ID: 13965844
I cannot run your code... would have to create tables....

So, you are checking if there is query string in url? You have problems with double variables?

You don't have to use preg_match just use

if(empty($_SERVER['QUERY_STRING'])) {
// ?
} else {
// &
}

...so tell me where the problem is
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question