User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » simple PHP/MySQL question displaying result groups Page [1]  
vertigo
Veteran
135 Posts
user info
edit post

I know this is probably very easy, but I have searched Google and not found what I'm looking for. I'm probably not searching for the right terms. I have a small table that looks like this

+----+------+-------+
| ID | TYPE | COLOR |
| 1 | dell | black |
| 2 | acer | green |
| 3 | dell | red |
| 4 | dell | blue |
| 5 | hp | white |
| 6 | hp | brown |
+----+------+-------+

and I want to run a query that gets everything from the table, but displays it like this
Acer
1. green

Dell
1. black
2. blue
3. red

HP
1. brown
2. white

Is there a way to do this with just one query or do I have to make a separate query for each type?

Thanks!

5/4/2009 8:28:47 AM

Ernie
All American
45943 Posts
user info
edit post

http://us3.php.net/manual/en/mysqli-result.fetch-assoc.php

Examples, explanations, etc.

5/4/2009 9:22:55 AM

Ernie
All American
45943 Posts
user info
edit post

And here's a slightly ugly, untested solution:

<?php

// Connect
$mysqli = new mysqli('localhost', 'root', 'PASSWORD', 'tww');

// Query string
$query = 'SELECT type, color FROM vertigo ORDER BY type, color';

// Get the data
$result = $mysqli->query($query);

// Loop through the data (as an associative array)
while ($row = $result->fetch_assoc())
{
// Echo type and open ordered list if type is different than previous itteration
if (strcmp($row['type'], $prev_type))
{
echo '</ol>' . "\n";
echo $row['type'] . "\n";
echo '<ol>' . "\n";
}

// Echo color
echo '<li>' . $row['color'] . '</li>' . "\n";

// Previous type flag
$prev_type = $row['type'];
}


--

After another look, that closing OL tag should probably go somewhere else. You get the idea, though.

[Edited on May 4, 2009 at 10:06 AM. Reason : ]

5/4/2009 10:05:50 AM

quagmire02
All American
44225 Posts
user info
edit post

^^ what's the advantage of that over using a foreach loop? also, it won't help if he's not using PHP 5.0.7+/MySQL 4.1.13+ (which he probably is, but it's something to think about)

i have no experience with the "improved" functions...are they just as fast?

[Edited on May 4, 2009 at 10:06 AM. Reason : arrows ... i need to look at what you posted]

5/4/2009 10:06:12 AM

Ernie
All American
45943 Posts
user info
edit post

Quote :
"what's the advantage of that over using a foreach loop?"


You can still use a foreach loop, foreach and mysqli aren't mutually exclusive or anything.

(Some of) The benefits of mysqli are discussed here: http://us3.php.net/manual/en/mysqli.overview.php (see "What is PHP's mysqli Extension?" section).

Quote :
"it won't help if he's not using PHP 5.0.7+/MySQL 4.1.13+"


Very true, I don't really think about PHP 4 anymore, though. If you're using PHP 4, google any combination of PHP and MySQL and you'll find a billion results.

5/4/2009 10:11:33 AM

Noen
All American
31346 Posts
user info
edit post

^way to mix your data and presentation.

5/4/2009 3:47:01 PM

Ernie
All American
45943 Posts
user info
edit post

Hmm?

5/4/2009 4:05:23 PM

qntmfred
retired
40819 Posts
user info
edit post

Quote :
"way to mix your data and presentation"


business as usual for php

5/4/2009 4:17:28 PM

Noen
All American
31346 Posts
user info
edit post

*cry*

5/4/2009 5:14:41 PM

Ernie
All American
45943 Posts
user info
edit post

I don't see what you did there.

5/4/2009 5:18:55 PM

philihp
All American
8349 Posts
user info
edit post

Quote :
"business as usual for php"


QFT

5/4/2009 5:18:58 PM

vertigo
Veteran
135 Posts
user info
edit post

Thanks, Ernie!

5/5/2009 7:40:21 AM

 Message Boards » Tech Talk » simple PHP/MySQL question displaying result groups Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.