RE: [PHP-DB] grouping by category

2002-04-04 Thread Jonathan Hilgeman

Hi Big Al,
You're just running full-steam ahead into these mailing lists. :)

Anyhow, as olinux pointed out, you can get everything you want in one query,
but I would choose a different query (no need to GROUP BY category), and
then use a multi-dimensional array to organize the data. Here's my version
of your code:

?
// Connect to DB

// Query
$Query = SELECT url, author, name, category, num FROM hwureviews ORDER BY
num DESC;
$Result = mysql_query($Query);

// Do error-checking for 0 results somewhere in here

// Add reviews to a multi-dimensional array - START
while($Row = mysql_fetch_assoc($Result))
{
// Get pieces needed to set up array
$ReviewNum = $Row[num];
$Category = $Row[category];

// Add record info to array
foreach($Row as $ColumnName = $Value)
{
// Store all fields except for num and category, because
// we already have those.
if(($ColumnName != num)  ($ColumnName != category))
{
$Reviews[$Category][$ReviewNum][$ColumnName] =
$Value;
}
}
}
// Add reviews to a multi-dimensional array - END

/*
Now you should have an array that looks like:
$Reviews[Video Cards][1][author] = Chuck Jones;
$Reviews[Video Cards][1][url] = http://www.chuckjones.com;;
$Reviews[Video Cards][1][some_other_col_name] = col_value;

$Reviews[Video Cards][6][author] = Bill Smith;
$Reviews[Video Cards][6][url] = http://www.billsmith.com;;
$Reviews[Video Cards][6][some_other_col_name] = col_value;

$Reviews[Memory][3][author] = Joe Bob;
$Reviews[Memory][3][url] = http://www.joebob.net;;
$Reviews[Memory][3][some_other_col_name] = col_value;
*/

// Go through array
foreach($Reviews as $CategoryName = $SingleReview)
{
// Print out the category name
print b . $CategoryName . /bbr;

// List all the reviews
foreach($SingleReview as $ReviewNum = $ReviewDetails)
{
print -  . $ReviewDetails[name] .  (Written by:  .
$ReviewDetails[author] . )br;
}
}
?

Now, that should give you a real basic listing looking like:

bCategory 1/b
- Review Name (Written by: Author)br
- Review Name (Written by: Author)br

bCategory 2/b
- Review Name (Written by: Author)br
- Review Name (Written by: Author)br

There are some nice perks about the above code. If you added, for instance,
a field called rating to the database, and you wanted to display the value
in the listings, all you would need to do is to add rating to your
beginning SELECT statement. At that point, it would instantly be available
in the bottom $ReviewDetails array as $ReviewDetails[rating].

The multi-dimensional arrays are a little hard to grasp at first, but they
can make things a lot easier to read/understand, which is what I strive for
in my coding (over speed). You never know who will one day read your code...

- Jonathan

-Original Message-
From: Alex Behrens [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 03, 2002 6:16 PM
To: [EMAIL PROTECTED]
Subject: [PHP-DB] grouping by category


Hey Guys,

I'm trying to get my script to group all my reviews by category now but now
it just displays them all in a row a bunch of times and doesn't group them
how I want it to group them. I want it too look like this:

Processor Reviews
- Review
- Review

Motherboard Reviews
- Review
-Review

Heres my syntax, and the page is online here
(http://www.hardware-unlimited.com/hardware2.php) so you can see how it is
executed now:

?php
$db = mysql_connect( db32.pair.com,  net3dual_2,  ***);
mysql_select_db( net3dual_reviews,$db);

file://select cateogries
$r = mysql_query(SELECT category FROM hwureviews ORDER BY num DESC);
while($a=mysql_fetch_array($r)) {
 $category = $a[category];

 file://select reviews for the current category
$select_reviews = select url, author, name from hwureviews where
category='.$a[category].';
 $reviews = mysql_query($select_reviews);

while($rev=mysql_fetch_array($reviews)) {
 $name = $rev[name];
 $author = $rev[author];
 $url = $rev[url];
 $email = $rev[email];
file://print out review info
echo $category Reviews:br;
echo - a href=\$url\$name/a (Written by: a
href=\mailto:$email\;$author/a) - [a href='$url' onMouseOver='if
(window.event || document.layers) show(\$name\,event)'
onMouseOut='hide(\$name\)'Description/a]brbr;

 }  // end review while
} //  end category while
?

What do I need to do?

Thanks!

-Alex Big Al Behrens
E-mail: [EMAIL PROTECTED]
Urgent E-mail: [EMAIL PROTECTED] (Please be brief!)
Phone: 651-482-8779
Cell: 651-329-4187
Fax: 651-482-1391
ICQ: 3969599
Owner of the 3D-Unlimited Network:
http://www.3d-unlimited.com
Send News:
[EMAIL PROTECTED]


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP-DB] grouping by category

2002-04-03 Thread olinux

Here's a start - take a look at the results in
phpmyadmin to get an idea of how to loop the result

SELECT url, author, name, category FROM hwureviews
GROUP BY category

olinux

--- Alex Behrens [EMAIL PROTECTED] wrote:
 Hey Guys,
 
 I'm trying to get my script to group all my reviews
 by category now but now
 it just displays them all in a row a bunch of times
 and doesn't group them
 how I want it to group them. I want it too look like
 this:
 
 Processor Reviews
 - Review
 - Review
 
 Motherboard Reviews
 - Review
 -Review
 
 Heres my syntax, and the page is online here
 (http://www.hardware-unlimited.com/hardware2.php) so
 you can see how it is
 executed now:
 
 ?php
 $db = mysql_connect( db32.pair.com,  net3dual_2,
  ***);
 mysql_select_db( net3dual_reviews,$db);
 
 file://select cateogries
 $r = mysql_query(SELECT category FROM hwureviews
 ORDER BY num DESC);
 while($a=mysql_fetch_array($r)) {
  $category = $a[category];
 
  file://select reviews for the current category
 $select_reviews = select url, author, name from
 hwureviews where
 category='.$a[category].';
  $reviews = mysql_query($select_reviews);
 
 while($rev=mysql_fetch_array($reviews)) {
  $name = $rev[name];
  $author = $rev[author];
  $url = $rev[url];
  $email = $rev[email];
 file://print out review info
 echo $category Reviews:br;
 echo - a href=\$url\$name/a (Written by: a
 href=\mailto:$email\;$author/a) - [a
 href='$url' onMouseOver='if
 (window.event || document.layers)
 show(\$name\,event)'

onMouseOut='hide(\$name\)'Description/a]brbr;
 
  }  // end review while
 } //  end category while
 ?
 
 What do I need to do?
 
 Thanks!
 
 -Alex Big Al Behrens
 E-mail: [EMAIL PROTECTED]
 Urgent E-mail: [EMAIL PROTECTED] (Please
 be brief!)
 Phone: 651-482-8779
 Cell: 651-329-4187
 Fax: 651-482-1391
 ICQ: 3969599
 Owner of the 3D-Unlimited Network:
 http://www.3d-unlimited.com
 Send News:
 [EMAIL PROTECTED]
 
 
 -- 
 PHP Database Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 


__
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php