943,021 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1075
  • PHP RSS
Jun 8th, 2010
1

URGENT HELP! - PHP while loop

Expand Post »
Hi there,
I have been scratching my head for a few days now! with this problem:
I am trying to echo out some data from a mysql table.
I have a column name Country which has the following Countries: India, Malaysia Austaralia etc.
I also have a column name Town which has the following Towns: Varkala, Cochin, Kula Lumpar, Sydney, Cairns etc.

Each town is linked with the accosiated countries eg:
India - Varkala
India - Cochin
Malaysia - Kula Lumpar
Australia - Sydney
Australia - Cairns

In php I can echo out each row using while loop, but the problem is I want to group the countries so it will display the country once and all the towns related underneath. For example:
India
- Varkala
- Cochin
Malaysia
- Kula Lumpar
Australia
- Sydney
- Cairns

Please have a look at the problem by visiting the following site:
http://www.jatinpatel.co.uk/blog
If you look on the right hand side where it says "Other Blogs".

If you can help I will be very greatful!

Thank you
Jatin
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jatpatel1 is offline Offline
11 posts
since Mar 2010
Jun 8th, 2010
0
Re: URGENT HELP! - PHP while loop
OK, so your MySQL Select should be ordering by country and then town and you probably need some while loops to detect when the town changes and when the country changes. You will have to get beyond just echoing what is in the database and write some code to determine when a break occurs and do the appropriate thing. You need to write the code and if you have difficulty making it work, someone here may be willing to help you. If you're lucky, someone may have a code snippet that does something similar that they can give you. If you don't have the PHP / programming knowledge to do it, then you have a problem and you need to do some learning or you need to find someone with the skills who can do it for you.
Reputation Points: 210
Solved Threads: 228
Nearly a Posting Virtuoso
chrishea is offline Offline
1,389 posts
since Sep 2008
Jun 8th, 2010
0
Re: URGENT HELP! - PHP while loop
pls ignore this - can't delete this post
Last edited by jatpatel1; Jun 8th, 2010 at 11:52 pm. Reason: mistake
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jatpatel1 is offline Offline
11 posts
since Mar 2010
Jun 8th, 2010
0
Re: URGENT HELP! - PHP while loop
Click to Expand / Collapse  Quote originally posted by chrishea ...
OK, so your MySQL Select should be ordering by country and then town and you probably need some while loops to detect when the town changes and when the country changes. You will have to get beyond just echoing what is in the database and write some code to determine when a break occurs and do the appropriate thing. You need to write the code and if you have difficulty making it work, someone here may be willing to help you. If you're lucky, someone may have a code snippet that does something similar that they can give you. If you don't have the PHP / programming knowledge to do it, then you have a problem and you need to do some learning or you need to find someone with the skills who can do it for you.
Chris thank you for your reply!
I have a good programming knowledge but cant seem to be working this one out.
I have tried using GROUP BY in the SQL Query but still not what I want.
Im preety sure its simple enough to do but if someone can point me in the right directions then I will take it from there.

This is what my code looks like:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $sql_list = mysql_query("SELECT * FROM tblblogs");
  3. while($row_list=mysql_fetch_array($sql_list)){
  4. echo $row_list['BCountry'];
  5.  
  6. echo'<li><a href="?blog=' . $row_list['BId'] . '">' . $row_list['BTown'] . '</a></li>';
  7.  
  8. }
  9. ?>
As you can see that it will echo out the country and town from each row which is not what I'm looking for. I want it to echo out the town from each row but the country only once. So basically I want to group the countries and display the towns foe that country.

I hope that this is a better insight of the problem.

Thanks Jat
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jatpatel1 is offline Offline
11 posts
since Mar 2010
Jun 9th, 2010
0
Re: URGENT HELP! - PHP while loop
There may be a better solution to this but a really quick way to get around it (I think) would be to nest one loop inside of the other (wouldn't be my first choice).

PHP Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3.  
  4. $sql_list = mysql_query("SELECT DISTINCT BCountry FROM tblblogs");
  5.  
  6. while($row_list=mysql_fetch_array($sql_list)){
  7. echo $row_list['BCountry'];
  8.  
  9. $get_town = mysql_query("SELECT DISTINCT BTown FROM tblblogs WHERE BCountry = ''.$row_list['BCountry'].'' ");
  10.  
  11. while($town=mysql_fetch_array($get_town))
  12. {
  13. echo'<li><a href="?blog=' . $town['BId'] . '">' . $town['BTown'] . '</a></li>';
  14. }
  15. }
  16.  
  17. ?>

It's worth a shot anyhow
Reputation Points: 21
Solved Threads: 31
Posting Pro in Training
CFROG is offline Offline
405 posts
since Jul 2009
Jun 9th, 2010
0
Re: URGENT HELP! - PHP while loop
Thanks CFROG! your a genius lol
Although I was on the right lines. I tried doing that but I put the second SELECT query outside the while loop and not inside. I have learnt somthing new today!
I thought if you put a while into a while it does it four times.
I completley understand your code but just one thing. What does DISTINCT do in the query?

Thanks again!

Jat
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jatpatel1 is offline Offline
11 posts
since Mar 2010
Jun 9th, 2010
0
Re: URGENT HELP! - PHP while loop
In so many words DISTINCT eliminates the retrieval of duplicated data.

If your table had entries like

USA
USA
India
Canada
India
Canada
USA

DISTINCT would return

USA
India
Canada

You had multiple entries for the same country and DISTINCT ensured that only one was returned. Without it your results will not be what you expect. Remove it from the query to see what it does. 8-)
Reputation Points: 21
Solved Threads: 31
Posting Pro in Training
CFROG is offline Offline
405 posts
since Jul 2009
Jun 9th, 2010
-1
Re: URGENT HELP! - PHP while loop
write a c++program that displays mid exam,final exam of 100 students and also displays total mark,average,maximum mark and name of students by ascending order depending on there total mark
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ali mebratu is offline Offline
1 posts
since Jun 2010
Jun 21st, 2010
0

i am also having same problem

i am also having same problem. Please hekp me out if any once know the solution.

I want to display the output like
India
- Varkala
- Cochin
Malaysia
- Kula Lumpar
Australia
- Sydney
- Cairns

but it shoe like this

India
- Varkala
- Cochin
- Kula Lumpar
- Sydney
- Cairns
Malaysia
- Varkala
- Cochin
- Kula Lumpar
- Sydney
- Cairns
Australia
- Varkala
- Cochin
- Kula Lumpar
- Sydney
- Cairns
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingredyard is offline Offline
1 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Query string help
Next Thread in PHP Forum Timeline: htaccess is not working





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC