The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Code Critique - Looping Problem
Discuss Looping Problem in the PHP Development forum on Dev Shed. Looping Problem PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 13th, 2012, 11:50 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 26
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
|

November 13th, 2012, 11:58 AM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 47
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
|

November 13th, 2012, 12:00 PM
|
|
|
|
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.
|

November 13th, 2012, 12:02 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 26
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
|

November 13th, 2012, 12:14 PM
|
|
|
|
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.
|

November 13th, 2012, 12:19 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 26
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.
|

November 13th, 2012, 12:25 PM
|
|
|
|
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.
|

November 13th, 2012, 12:37 PM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 294
  
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
|

November 13th, 2012, 12:51 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 26
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.
|

November 13th, 2012, 01:06 PM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 294
  
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.
|

November 13th, 2012, 01:38 PM
|
|
|
|
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.
|

November 13th, 2012, 01:46 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 26
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.
|

November 13th, 2012, 04:59 PM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 294
  
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, ...'?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|