PDA

View Full Version : PHP & MySQL


PreciseGirl
7-9-04, 05:35 PM
I'm still new to PHP. I can't write much on my own, but I can usually look at code and edit it until I get it right! lol

I'm trying to make a module for my PHP Nuke. I want the page to read from a table in my database that contains different editors, their titles, and the school they are at. There are several different groups of editors (co-editors, research & book review editors, etc) and in each group there may be several names.

I can get the php to list all of the information, but not in a very practical way. I want to group all of the co-editors togeter under one heading Co-Editors, rather than listing co-editors beside every name.

So, rather than having it listed like I have it:

Co-Editors, Name, school
Co-Editors, Name, school
Co-Editors, Name, school
Research & Book Review Editors, Name, school
Research & Book Review Editors, Name, school
etc

I would like it to be something like:


Co-Editors
.....Name, school
.....Name, school
.....Name, school
Research & Book Review Editors
.....Name, school
.....Name, school
etc


my code for this part right now is:

$query = "SELECT * FROM ".$prefix."_editorialboard_text";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
while ($line = mysql_fetch_object($result)) {
echo "$line->title, $line->text, $line->school<br> \n";
}

What do I need to do to make it look nicer?

Thanks in advance, and sorry if this is a ridiculous question... I'm still learning!

Fabian
7-9-04, 06:46 PM
No ridiculous question :)

You could store the previous editor-type in a variable, so like this:


$query = "SELECT * FROM ".$prefix."_editorialboard_text";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

$previous_title = '';

while ($line = mysql_fetch_object($result)) {
if($previous_title != $line['title']) echo "$line[title]<br>";
echo "$line->text, $line->school<br> \n";
$previous_title = $line['title'];
}

It comes to this: only when the previous and the current editor type are different the editor type is printed.

PreciseGirl
7-9-04, 06:57 PM
Sounds like a great idea, but when I copy your code into my file, it only shows the names and schools. It doesn't show the title... what could be the problem?

Thanks,
Sandy

Fabian
7-9-04, 07:28 PM
Hmm, I guess mixing two ways of calling variables is not allowed.

$query = "SELECT * FROM ".$prefix."_editorialboard_text";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

$previous_title = '';

while ($line = mysql_fetch_object($result)) {
if($previous_title != $line->title) echo "$line->title<br>";
echo "$line->text, $line->school<br> \n";
$previous_title = $line->title;
}

maybe this will work :)

PreciseGirl
7-10-04, 02:45 PM
Yay!! It worked! Thank you very much!

I really need someone to just come help me learn PHP LOL But I'm too busy with college and work :P I don't even think my college offers classes in PHP... lol

Bree
7-10-04, 02:58 PM
I really need someone to just come help me learn PHP LOL

I've been thinking the same thing over and over and over LOL
Thank Goodness there are nice people on here willing to help a girl out..
over and over and over LOL Or I'd be doooooooooomed!

PreciseGirl
7-10-04, 04:17 PM
Another question... lol

Lets say later I have to come back and add another Co-Editor, so I add another row to my table. Well, it adds another listing for Co-Editors at the very bottom, not with the rest of the co-editors. How do I get it to list them together?

Also, if the list gets edited (names deleted, names added) then I still want the names to be in alphabetical order. How can I get it to list them in alphabetical order by the last name? I know I will have to have separate fields for first and last names.

Thanks again!

Pig
7-10-04, 09:33 PM
It is important to understand that grouping and association should NEVER EVER EVER EVER rely on the position in the database. To order how something sorts, you use ORDER BY in your query.
eg: SELECT * FROM table ORDER BY table_field

In order to group edotirs and co-editors together, you need to create table to contain that relation ship if co-editors can be associated with multiple editors, or if co-editors are exclusive to editors, you need to create a new field within the table.

As an example where the co-editors are exclusive:
Let's say that Bob is an editor and has a table id of 3. Dick and Jane are co-editors for Bob. In their table there would a field called 'editor' or something similar. Dick and Jane would have '3' in their editor column because the 3 would refer to Bob's ID.

For an example where co-editors are shared, you can read this tutorial.
http://www.webhead.cc/main.php?inv=63