PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP 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:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old July 21st, 2004, 10:23 PM
Cameron Roat Cameron Roat is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 6 Cameron Roat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PHP Loop Help

Hello,

I have a page listing books by various authors. Each book has its own row in a table in my MySQL database, with fields such as author, title, etc. I query the database and list the author and title of the book with PHP. However, if I have multiple works by one author, the page, of course, displays the author's name above every book that he's written. Is there a way I can change the code so it only shows the author's name once?

For example, here is a sample list.

Author, Bill
Bill's First Book

Author, Bill
Bill's Second Book

Writer, Tom
Tom's Book

I'd like to change it to read like below.

Author, Bill
Bill's First Book
Bill's Second Book

Writer, Tom
Tom's Book

If you need a portion of my code, let me know.

Thanks in advance,

Cameron Roat

Reply With Quote
  #2  
Old July 22nd, 2004, 03:03 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Order the results by author then keep track of whatever the author was in the last row and compare it to the current one before output. If its different then output the author name with whatever formatting. Otherwise just output the book name and move on to the next row.

Hope this helps,

-KM-

Reply With Quote
  #3  
Old July 22nd, 2004, 11:26 AM
Cameron Roat Cameron Roat is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 6 Cameron Roat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I understand what you're saying to do; however, I am still new to this, so could you provide me with a sample code?

Thanks so much,

Cameron

Reply With Quote
  #4  
Old July 22nd, 2004, 01:42 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Give it a go yourself and then ask questions when you get stuck. People on this forum will help you out but we're not here to do it for you. There are loads of tutorials all over the net have a look around and see what you can find.

-KM-

Reply With Quote
  #5  
Old July 23rd, 2004, 08:01 PM
Cameron Roat Cameron Roat is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 6 Cameron Roat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I know how to write the code, with the exception of the retrieval of data from the previous record.

Below is what I've tried, but it still displays the author each time.

PHP Code:
 $querymysql_query("SELECT * FROM resources WHERE category != 3 ORDER by 
authorlast,authorfirst,sorttitle ASC"
);
while (
$row mysql_fetch_array($query))
if (
$row[authorlast] == $prev_authorlast) { echo "<br><u>$row[title]</u>"; } 
else {echo 
"<p><strong>$row[authorlast], $row[authorfirst]</strong><br><u>$row[title]</u>";} 
$prev_authorlast $row[authorlast]; 


Any help is greatly appreciated.

Cameron

Reply With Quote
  #6  
Old July 24th, 2004, 07:07 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Does this script give you errors when you run it?

I'm sure you can't do $row[authorlast], doesn't it need to be $row['authorlast'] instead?

You also need to use string comparison functions instead of just == so have a look for strcmp in some php reference.

Hope this helps,

-KM-

Reply With Quote
  #7  
Old July 24th, 2004, 12:18 PM
Cameron Roat Cameron Roat is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 6 Cameron Roat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Here's what I changed the script to. Now it just lists all the titles of the books.

PHP Code:
 $querymysql_query("SELECT * FROM resources WHERE category != 3 ORDER by 
authorlast,authorfirst,sorttitle ASC"
);
while (
$row mysql_fetch_array($query))
if (
strcmp($row["authorlast"], $prev_authorlast)) { echo "<br><u>$row[title]</u>";

else {echo 
"<p><strong>$row[authorlast], $row[authorfirst]</strong><br><u>$row[title]</u>";} 
$prev_authorlast $row["authorlast"]; 


Thanks,

Cameron

Reply With Quote
  #8  
Old July 25th, 2004, 06:14 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Ok my first tip is to learn to space your code out a bit better so you can see whats going on easier. Don't be afraid to insert whitespace in there so you have a good, easy to follow layout. So you're code above could look like -

PHP Code:
 $querymysql_query("SELECT * FROM resources WHERE category != 3 ORDER by
  authorlast,authorfirst,sorttitle ASC"
);

  while (
$row mysql_fetch_array($query))
    if (
strcmp($row["authorlast"], $prev_authorlast))
    {
      echo 
"<br><u>$row[title]</u>";
    }
    else
    {
      echo 
"<p><strong>$row[authorlast],
      $row[authorfirst]</strong><br><u>$row[title]</u>"
;
    }
  
$prev_authorlast $row["authorlast"]; 


This makes it a lot easier to see that your bottom line setting the $prev_authorlast variable is only run after the mysql_fetch_array loop so you need to insert another set of braces for your while loop as follows -

PHP Code:
 $querymysql_query("SELECT * FROM resources WHERE category != 3 ORDER by
  authorlast,authorfirst,sorttitle ASC"
);
  while (
$row mysql_fetch_array($query))
  {
    if (
strcmp($row["authorlast"], $prev_authorlast))
    {
      echo 
"<br><u>$row[title]</u>";
    }
    else
    {
      echo 
"<p><strong>$row[authorlast],
      $row[authorfirst]</strong><br><u>$row[title]</u>"
;
    }
    
$prev_authorlast $row["authorlast"];
  } 


Also, the php reference manual (www.php.net) says that strcmp returns <0 if the first string is less than the second, >0 if the first string is greater than the second and 0 if they are equal. Since you are trying to use this to decide whether an if statement should be run or not you need to compare the result returned with a numeric value, in this case testing if strcmp (blah, blah) != 0 (ie they are not equal and you have your next author).

Hope this helps,

-KM-

Last edited by kode_monkey : July 25th, 2004 at 06:15 AM. Reason: Missed php tag

Reply With Quote
  #9  
Old July 25th, 2004, 11:10 AM
Cameron Roat Cameron Roat is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 6 Cameron Roat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Kode Monkey,

You are awesome. Thanks so much. The code works perfectly. Here is the final code for those interested.

PHP Code:
 $querymysql_query("SELECT * FROM resources WHERE category != 3 ORDER by
   authorlast,authorfirst,sorttitle ASC"
);
   while (
$row mysql_fetch_array($query))
   {
     if (
strcmp($row["authorlast"], $prev_authorlast) != 0)
     {
       echo 
"<p><strong>$row[authorlast],
       $row[authorfirst]</strong><br><u>$row[title]</u>"
;
     }
     else
     {
       echo 
"<br><u>$row[title]</u>";
     }
     
$prev_authorlast $row["authorlast"];
   } 


Thanks again,

Cameron

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > PHP Loop Help


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 3 hosted by Hostway