MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old March 21st, 2004, 10:58 PM
pentapenguin pentapenguin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 51 pentapenguin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 7 sec
Reputation Power: 6
Problem sorting/grouping/displaying MySQL data

Hi everybody,

I have a rather (hopefully ) simple question, but I can't figure this out even though I've worked on it way over an hour.
I'm pretty good at PHP and MySQL (by the way the data is ok), but like I said I just can't figure this out.
I've tried for's,while's, and other things but no luck.

We have a newsletter table.
Each issue of the newsletter has 1 or more tips, and each tip is stored in a separate row.

Here's the sample table:

id,newsletter_id,title,content
-----------------------------------------------------
1,1,item 1,tip1
2,1,item 2,tip2
3,2,item 1,tip1
4,3,item 1,tip1
5,3,item 2,tip2
6,3,item 3,tip3
7,3,item 4,tip4
..........
-----------------------------------------------------

I would like to display the page like this:

Newsletter 1
--item 1
--item 2

Newsletter 2
--item 1

Newsletter 3
--item 1
--item 2
--item 3
--item 4
..........

Sample code:
PHP Code:
 $connection mysql_connect($database_host$database_user$database_password) or die ("cannot connect");
mysql_select_db($database_dbname) or die ("cannot select db");
$query "SELECT id, newsletter_id, title FROM newsletters ORDER BY newsletter_id";
$result mysql_query($query) or die ("error");
if (
mysql_num_rows($result2) > 0)
{
while (
$row mysql_fetch_object($result2))
{
$id $row->newsletter_id;
$title unescape_data($row->title);
}
}

else
{
print (
"Error: no newsletters are available.");



Can I please get some help?!?!
I'm about to go crazy!
Thanks a lot!

Reply With Quote
  #2  
Old March 24th, 2004, 09:04 AM
tobycloud tobycloud is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Near Albany NY
Posts: 27 tobycloud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to tobycloud Send a message via AIM to tobycloud
Well, a couple of tips. Hope they help.

Tip 1:
Why don't you store the data in two tables?
Table 1 (newsletters)
newsletter_id, id
Table 2 (news_items)
newsletter_id, title, content
PHP Code:
 $query "SELECT id, newsletter_id FROM newsletters ORDER BY newsletter_id;"
$result mysql_query($query) or die ("error");
 
while (
$myrow mysql_fetch_array($result) {
  
$news_temp $myrow["newsletter_id"];
  echo 
$news_temp;
 
  
$query2 "SELECT title, content FROM news_items WHERE newsletter_id='$news_temp';";
  
$result2 mysql_query($query2) or die ("error");
  
  while (
$myrow2 mysql_fetch_array($result2) {
    
$title_temp $myrow2["title"];
    
$content_temp $myrow2["content"];
 
    echo 
"--$title_temp";
  }



Yeah it's f-ugly, but hey, it works. Well maybe not. I didn't test this code but I think you understand the concept and I've used this concept before.
Tip 2:
If you're intent on using one table try this.

PHP Code:
 $query "SELECT DISTINCT newsletter_id FROM newsletters ORDER BY newsletter_id;"
$result mysql_query($query) or die ("error");
 
while (
$myrow mysql_fetch_array($result) {
  
$news_temp $myrow["newsletter_id"];
  
  
$query2 "SELECT title, content FROM newsletters WHERE newsletter_id='$news_temp';";
  
$result2 mysql_query($query2) or die ("error");
 
  while (
$myrow2 mysql_fetch_array($result2) {
    
$title_temp $myrow2["title"];
    
$content_temp $myrow2["content"];
 
    echo 
"--$title_temp";
  }


Let me know how you make out.

Reply With Quote
  #3  
Old May 11th, 2004, 02:15 PM
Gman Gman is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 3 Gman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
tobycloud,
I tried to use your grouping code listed above but cannot seem to get it to work without parse errors. I modified it a littel to match what we need. Please take a look and tell me what I am missing.
PHP Code:
 $query "SELECT ClientID, BusinessName, LogoFileName, InternetURL FROM clientrecord ORDER BY ClientID;"

if (
$link_identifier mysql_pconnect(DB_HOSTDB_USERDB_PASS)) {

$result mysql_query($query$link_identifier) or die ( mysql_error());

while (
$myrow mysql_fetch_array($result) {

$news_temp $myrow["ClientID"];



echo 
$news_temp;

$query2 "SELECT ClientID, PDFURL, Name FROM PDFS WHERE ClientID='$news_temp';";

if (
$link_identifier mysql_pconnect(DB_HOSTDB_USERDB_PASS)) {

$result2 mysql_query($query2$link_identifier) or die ( mysql_error());

while (
$myrow2 mysql_fetch_array($result2) {

$title_temp $myrow2["PDFURL"];

$content_temp $myrow2["Name"];

echo 
"--$title_temp";

}

}

}



?> 


Thank you ahead of time.

G

Reply With Quote
  #4  
Old May 12th, 2004, 11:29 AM
pentapenguin pentapenguin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 51 pentapenguin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 7 sec
Reputation Power: 6
Here's the updated code I use.
Not very efficient but it works.

Table "tips":
id,newsletter_id,title,content

Each newsletter tip is stored in the tips table.
There's a "newsletter_id" field because not all the tips in the table are newsletters.

Confusing?
Yes - but here's the finished product (similar not exact code): http://www.acctadv.com/newsletters.php

PHP Code:
 $query1 "SELECT MAX(newsletter_id) AS total FROM tips";
$result1 mysql_query($query1);
$row1 mysql_fetch_object($result1);
(int) 
$totalnewsletters $row1->total;

for (
$i $totalnewsletters$i >= 1$i --)
{
$query2 "SELECT newsletter_id, title FROM tips WHERE newsletter_id = $i";
$result2 mysql_query($query2);

print (
"<tr>\n<td valign='top'>\n<img src='images/smalllogo.gif'>\n</td>\n<td>\n<a href='nl.php?id=$i'>Newsletter $i</a>\n<ul>\n");

    while (
$row mysql_fetch_object($result2))
    {
    
$title $row->title;
    print (
"<li>$title</li>\n");
    }

print (
"</ul>\n</td>\n</tr>\n\n");



Basically, the total number of newsletters are retrieved, then a reverse "for" loop gets the specific newsletter and prints it out.

Reply With Quote
  #5  
Old May 12th, 2004, 12:44 PM
Pheifel Pheifel is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Denmark
Posts: 174 Pheifel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 46 sec
Reputation Power: 5
Send a message via MSN to Pheifel
Hi. I am pondering, why not just use the "group by" function in the mysql_query(); ??

- Pheifel

Reply With Quote
  #6  
Old May 12th, 2004, 11:12 PM
Gman Gman is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 3 Gman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Pheifel,
I was doing some reading and wondered the same thing, but I am just a novice at php.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Problem sorting/grouping/displaying MySQL data


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway