Forum Moderators: coopster

Message Too Old, No Replies

Looping through

Looping through a column

         

beinghuman

8:10 am on Nov 13, 2004 (gmt 0)

10+ Year Member



I have a simple script to write to a text file
<?php
require_once 'db_connect.php';

$result = mysql_query( "SELECT g.*, c.* FROM groups as g JOIN categories as c where g.group_id = c.group_id" )
or die("SELECT Error: ".mysql_error());

$entry_lines="";
while ($query_data = mysql_fetch_array($result)) {
$entry_lines .= "..¦ ".$query_data['group_name']. "¦¦¦¦¦1\n";
$entry_lines .= "...¦".$query_data['category_name']." ¦";
$entry_lines .= "display3.php?searchitem=product_name&id=".$query_data['category_id']."¦\n";

$fp = fopen("layersmenu-vertical-1.txt", "w");
fputs($fp, $entry_lines); // fwrite() alias
fclose($fp);

}

?>

I need a loop to go through the table and identify all the category names that are in a particular group name (tables are linked by group_id).
So in the end it will print out
Group Name 1
Category Name in group name 1
Another Category Name in group name 1
Another Category Name in group name 1

Group Name 2
Category Name in group name 2
Another Category Name in group name 2
Another Category Name in group name 2

etc...

At the moment I get

Group Name 1
Category Name in group name 1

Group Name 2
Category Name in group name 2

etc...

Any Help would be greatly appreciated

Thanks in advance

Salsa

1:58 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



Try adding "GROUP BY category_id" to the end of your query.

You may also want to include an ORDER BY before that.

beinghuman

2:11 am on Nov 14, 2004 (gmt 0)

10+ Year Member



Thanks for the reply but unfortunately that didn't fix the problem. Thanks for trying. Any other suggestions would be greatly appreciated.

Salsa

4:05 am on Nov 15, 2004 (gmt 0)

10+ Year Member



Well, it will fix the problem. What is unfortunate for you is that you will have to participate in writing some of your code yourself. ORDER BY or GROUP BY whatever field(s) you want your results to be ordered by or grouped by. I suggest that you go to mysql.org for details. Sorry if I sound harsh, but we don't share the same idea of what it is to be human.

You started out this thread by saying, "I have a simple script...," but, really, how simple did even that seem before I wrote it for you?

beinghuman

8:45 am on Nov 15, 2004 (gmt 0)

10+ Year Member



I apologise if I did upset you by saying it is simple. My meaning was that it was not long ( as in length) and I do thank you for writing it for me as does the community site I represent. Once again I deeply regret what I wrote in previous message. Thanks you anyways for trying your help was greatly appreciated.

beinghuman

8:48 am on Nov 15, 2004 (gmt 0)

10+ Year Member



PS. I am presuming it didn't work for me as I was putting order by before group by together and it kept giving me errors. When I did put one or the other after I wrote the response the results I was getting was doing fine. So your response was correct, it was that i was putting things in the wrong order.

Salsa

5:39 pm on Nov 15, 2004 (gmt 0)

10+ Year Member



No problem; just my version of tough love.

You're right. The ORDER BY needs to come after the GROUP BY.

Anyway, I'm glad it's working for you.