PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 13th, 2012, 11:50 AM
wayne12690 wayne12690 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 26 wayne12690 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 29 m 41 sec
Reputation Power: 0
Code Critique - Looping Problem

Hi All,

I have this code :
PHP Code:
 $connect mysql_connect("127.0.0.1""root""") or die ("Couldn't connect to database"); mysql_select_db("login") or die ("Couldn't find database"); $result mysql_query("SELECT * FROM purchased_services WHERE School_Code='$schoolcode' ORDER BY Service"); $row mysql_fetch_assoc($result); $result2 mysql_query("SELECT * FROM purchased_services GROUP BY Service"); $row2 mysql_fetch_assoc($result2); echo $row['District']; echo "<p>"; while ($row mysql_fetch_assoc($result)) { echo $row['COSER Code']; echo " - "; echo $row['Service']; echo $row['School_Code']; echo "<p>"; } echo "BREAK"; echo "<p>"; while ($row2 mysql_fetch_assoc($result2)) { if ($row2['District'] == "Baldwinsville") { echo $row2['Service']; echo " - "; echo $row2['School_Code']; echo "<p>"; } } 


Example of what it returns: BookWhere Online (SLS) - Bdj

It returns the right thing for the service(BookWhere Online) but I want it to return all of the school codes instead of just one school code(In example, Bdj). How can I do this? I have tried some things and it will either not execute or come back blank.

Please help!

David

Reply With Quote
  #2  
Old November 13th, 2012, 11:58 AM
piperpam27 piperpam27 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 47 piperpam27 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 51 m 3 sec
Reputation Power: 1
Hi wayne12690,

Debugging is much, much easier if you have your code laid out in a readable format. This is especially important when you are posting code on the forums; readable code makes it easier for us to spot your code issues.

For anyone who may be working on debugging this:

PHP Code:
 $connect mysql_connect"127.0.0.1""root""" ) or die ( "Couldn't connect to database" );
mysql_select_db"login" ) or die ( "Couldn't find database" );
$result mysql_query"SELECT * FROM purchased_services WHERE School_Code='$schoolcode' ORDER BY Service" );
$row mysql_fetch_assoc$result );
$result2 mysql_query"SELECT * FROM purchased_services GROUP BY Service" );
$row2 mysql_fetch_assoc$result2 ); echo $row['District']; echo "<p>";

while ( 
$row mysql_fetch_assoc$result ) ) {
    echo 
$row['COSER Code']; echo " - ";
    echo 
$row['Service'];
    echo 
$row['School_Code'];
    echo 
"<p>";
}

echo 
"BREAK";
echo 
"<p>";

while ( 
$row2 mysql_fetch_assoc$result2 ) ) {
    if ( 
$row2['District'] == "Baldwinsville" ) {
        echo 
$row2['Service']; echo " - ";
        echo 
$row2['School_Code']; echo "<p>";
    }

__________________
Laterna Studio - Animation, Web Design, Video Production and Print Design

Psalm 73: 25-26

Reply With Quote
  #3  
Old November 13th, 2012, 12:00 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,871 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Week 5 Days 10 h 23 m 16 sec
Reputation Power: 581
You need to edit your code so it is not all on one line. Paste the code in the textarea (properly formatted), highlight it and THEN click the wrap PHP icon.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Reply With Quote
  #4  
Old November 13th, 2012, 12:02 PM
wayne12690 wayne12690 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 26 wayne12690 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 29 m 41 sec
Reputation Power: 0
Quote:
Originally Posted by piperpam27
Hi wayne12690,

Debugging is much, much easier if you have your code laid out in a readable format. This is especially important when you are posting code on the forums; readable code makes it easier for us to spot your code issues.

For anyone who may be working on debugging this:

PHP Code:
 $connect mysql_connect"127.0.0.1""root""" ) or die ( "Couldn't connect to database" );
mysql_select_db"login" ) or die ( "Couldn't find database" );
$result mysql_query"SELECT * FROM purchased_services WHERE School_Code='$schoolcode' ORDER BY Service" );
$row mysql_fetch_assoc$result );
$result2 mysql_query"SELECT * FROM purchased_services GROUP BY Service" );
$row2 mysql_fetch_assoc$result2 ); echo $row['District']; echo "<p>";

while ( 
$row mysql_fetch_assoc$result ) ) {
    echo 
$row['COSER Code']; echo " - ";
    echo 
$row['Service'];
    echo 
$row['School_Code'];
    echo 
"<p>";
}

echo 
"BREAK";
echo 
"<p>";

while ( 
$row2 mysql_fetch_assoc$result2 ) ) {
    if ( 
$row2['District'] == "Baldwinsville" ) {
        echo 
$row2['Service']; echo " - ";
        echo 
$row2['School_Code']; echo "<p>";
    }



I am sorry for that. It is much easier to read. I wasnt sure how to do that with the PHP blocks. Also for anyone trying to debug it is the last while loop I am talking about. I have been trying many things so ignore the first while loop. Thanks

Reply With Quote
  #5  
Old November 13th, 2012, 12:14 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,871 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Week 5 Days 10 h 23 m 16 sec
Reputation Power: 581
What do you mean by "all school codes." Your query is returning all school codes based on the search criteria. If you are not getting all you expect then you need to examine your query.

That having been said, I suggest you consider rewriting this to use PDO as the MySQL extensions are depreciated.

Reply With Quote
  #6  
Old November 13th, 2012, 12:19 PM
wayne12690 wayne12690 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 26 wayne12690 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 29 m 41 sec
Reputation Power: 0
Quote:
Originally Posted by gw1500se
What do you mean by "all school codes." Your query is returning all school codes based on the search criteria. If you are not getting all you expect then you need to examine your query.

That having been said, I suggest you consider rewriting this to use PDO as the MySQL extensions are depreciated.


This is what it currently returns:
BookWhere Online (SLS) - Bdj

Brain Pop (3-8 Enrl.) - Bee

Brain Pop Jr. (K-2 Enrl.) - Bee

This is what I would like it to return:
BookWhere Online (SLS) - Bdj, Bee, Bmc

Brain Pop (3-8 Enrl.) - Bee, Bdj, Bhs

Brain Pop Jr. (K-2 Enrl.) - Bee, Bmc, Bmr

*It is only returning the first school code

I hope this helps. It may be a tough to understand. I hope I am doing a good enough job of explaining.

Reply With Quote
  #7  
Old November 13th, 2012, 12:25 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,871 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Week 5 Days 10 h 23 m 16 sec
Reputation Power: 581
I'm afraid this does not make much sense to me. If you said it returned:

BookWhere Online (SLS) - Bdj
BookWhere Online (SLS) - Bee
BookWhere Online (SLS) - Bmc

Then it would make sense. But based on what you posted, it appears that your selection criteria in the query is wrong. You need to echo the generated query string and then paste it into a mysql command line to see what it returns. Make your query adjustments accordingly.

Reply With Quote
  #8  
Old November 13th, 2012, 12:37 PM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 294 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 6 m 9 sec
Reputation Power: 5
I'm just wondering, could you possibly provide 2 or 3 or the rows in which are being viewed, along with the column headers? I think you have most everything right, just not getting your loop. I figure just have all on the table so some can see all from beginning to end.


Edit: Ummm... Wait a minute. I'm sorry. Nothing in here is requesting the school code, really, other than wishing to select items BY a previously set school code... :-S

Reply With Quote
  #9  
Old November 13th, 2012, 12:51 PM
wayne12690 wayne12690 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 26 wayne12690 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 29 m 41 sec
Reputation Power: 0
Quote:
Originally Posted by Triple_Nothing
I'm just wondering, could you possibly provide 2 or 3 or the rows in which are being viewed, along with the column headers? I think you have most everything right, just not getting your loop. I figure just have all on the table so some can see all from beginning to end.


School_Code District Service
Bdj Baldwinsville BrainPop 3-8
Bmc Baldwinsville BookWhere
Bdj Baldwinsville Library Automation

Hope this is what you were looking for.

Reply With Quote
  #10  
Old November 13th, 2012, 01:06 PM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 294 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 6 m 9 sec
Reputation Power: 5
Ah, I think I got what you are aiming for. The issue with your code is you are meerly running a full line at a time. You can't do this with what wish aim for. You are organizing your GROUP BY correctly, but will need to run a check on the next line to see if it is a matching Service or not. If it IS matching, then u want to step in and echo just the school code before wrapping that line up and stepping down to the next Service row.


Steppin out for work, so hope this helped.

Reply With Quote
  #11  
Old November 13th, 2012, 01:38 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,871 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Week 5 Days 10 h 23 m 16 sec
Reputation Power: 581
I have not done this so I can't give you the syntax but you could ask this in the MySQL forum. I think there is a way to COMBINE_GROUP to get what you want.

Reply With Quote
  #12  
Old November 13th, 2012, 01:46 PM
wayne12690 wayne12690 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 26 wayne12690 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 29 m 41 sec
Reputation Power: 0
Quote:
Originally Posted by gw1500se
I have not done this so I can't give you the syntax but you could ask this in the MySQL forum. I think there is a way to COMBINE_GROUP to get what you want.


Thank you. I will do this. I put this on here because it had to do with PHP. I wasn't really sure of where to post this question. I appreciate all the help I have gotten with this problem.

Reply With Quote
  #13  
Old November 13th, 2012, 04:59 PM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 294 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 6 m 9 sec
Reputation Power: 5
Howdy. Just wondering if you got this all figured out. I had a short day at work, n looked back into your issue. This is a simple script, but gw1500se did make a good point. You won't be able to have something like 'BookWhere Online (SLS) - Bdj, Bee, Bmc' because it seems to be only a single item. Unless you perhaps mistyped something. Were you aiming at something similar to 'Baldwinsville - Bdj, Bmc, ...'?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Code Critique - Looping Problem

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap