Thread: Looping Problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    26
    Rep Power
    0

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    48
    Rep Power
    3
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    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.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    26
    Rep Power
    0
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    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.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    26
    Rep Power
    0
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    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.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    721
    Rep Power
    7
    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
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    26
    Rep Power
    0
    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.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    721
    Rep Power
    7
    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.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    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.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    26
    Rep Power
    0
    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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    721
    Rep Power
    7
    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, ...'?

IMN logo majestic logo threadwatch logo seochat tools logo