Forum Moderators: coopster

Message Too Old, No Replies

Php and certain SQL query

         

Joppiesaus

10:37 am on Jan 30, 2009 (gmt 0)

10+ Year Member



Hello,

For my websiteI use php and Mysql to create dynamic pages.

What I want to do is create a dynamic navigation menu where (among other things) all the categories are shown. Categories come and go, so making a navigation menu that keeps track of this would save a lot of time.

What I need is a query that outputs all the categories, but every unique category just one time. Its simple to output the entire list, but it needs to be limites so that every output is unique.

I've looked around but cant seem to find what I need...

(edit: all the different categories are saved in product.categorie)

[edited by: Joppiesaus at 10:42 am (utc) on Jan. 30, 2009]

wheelie34

1:31 pm on Jan 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try something like this

$query = "SELECT distinct product.categorie FROM table order by 'desc'";

Using distinct will return one of each category, use order by as you need to.

HTH

Joppiesaus

2:04 pm on Jan 30, 2009 (gmt 0)

10+ Year Member



Thanks,

I guess thats what I was looking for. I now litterally filled in this:

<?php
$query_Index = "SELECT distinct product.categorie FROM product ORDER BY product.categorie ASC";
echo "".$Index[0]."";
?>

However, nothing is echoed. Is my code faulty?

wheelie34

2:17 pm on Jan 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this


$query_Index = "SELECT distinct product.categorie FROM product ORDER BY product.categorie ASC";
$result = mysql_query($query_Index) or die("Couldnt' execute query.");
while ($newArray = mysql_fetch_array($result)){
$cat = $newArray['product.categorie'];
print "$cat";
}

Should give you a start

Joppiesaus

2:30 pm on Jan 30, 2009 (gmt 0)

10+ Year Member



Thanks Wheelie.

The code doesnt seem to work yet. I tried it on different pages (some where database connections wehere already made and other dynamic stuff already works and also on different pages. I also tryed different variables, but every time nothing is echoed (or is it printed?).

What can I do to figure out the mistake I made?

wheelie34

3:52 pm on Jan 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try adding

print "$query";

like this
$result = mysql_query($query_Index) or die("Couldnt' execute query.");
print "$query";
while ($newArray = mysql_fetch_array($result)){

and see if anything prints, if not try running the query

SELECT distinct product.categorie FROM product ORDER BY product.categorie ASC

from within phpmyadmin and see what returns.

I am not a pro by any means, most of my efforts fail first time around (lazy to read first) to eliminate most problems cut down your query to just

SELECT distinct product.categorie FROM product

from the original code

ASC

should be

'ASC'

HTH