|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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- |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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- |
|
#5
|
|||
|
|||
|
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:
Any help is greatly appreciated. Cameron |
|
#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- |
|
#7
|
|||
|
|||
|
Here's what I changed the script to. Now it just lists all the titles of the books.
PHP Code:
Thanks, Cameron |
|
#8
|
|||
|
|||
|
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:
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:
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 |
|
#9
|
|||
|
|||
|
Kode Monkey,
You are awesome. Thanks so much. The code works perfectly. Here is the final code for those interested. PHP Code:
Thanks again, Cameron |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > PHP Loop Help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|