Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts

    How to improve this clumsy code with a loop

    Hello,

    I wrote this code that is working but I find it very clumsy.

    I want to print the scores of every lesson only if the score is higher than 0. If it is higher than 100 I want to highlight it. However I don't want to write the same

    PHP Code:
    if ($info['lesson1'] == 0){
                        echo 
    "<p>Lesson 1: ".$info['lesson1'] . " </p>";}
                        
                            else { if (
    $info['lesson1'] > 100){
                                    echo 
    "<h1>Lesson 1: ".$info['lesson1'] . " </h1>";}    } 
    for every lesson (I have more than 100 !!!). I'm sure there is a way to do it using a loop, but I don't know how

    Here is my code:

    PHP Code:
    <?php
                     
                    
    if ($_SESSION['username'])
                        {
                    echo 
    "<h1> welcome, ".$_SESSION['username']."</h1>";
                    
                    
    // The user data 
                    
                    
    include("connect.php");

                    
    $username $_SESSION['username'];

                    
    $data mysql_query("SELECT * FROM users WHERE username='$username'") or die(mysql_error());
            
                    
    $info mysql_fetch_array($data);

                if (
    $_SESSION['username'])
                    {
                        Print 
    " <h2>User Information</h2>
                                <p>Username: "
    .$info['username'] . "<p>
                                <hr> 
                                <p>Total points: "
    .$info['total_points'] . " </p>"
                                
                    
                        if (
    $info['lesson1'] == 0){
                        echo 
    "<p>Lesson 1: ".$info['lesson1'] . " </p>";}
                        
                            else { if (
    $info['lesson1'] > 100){
                                    echo 
    "<h1>Lesson 1: ".$info['lesson1'] . " </h1>";}    }
                        
                        
                        if (
    $info['lesson2'] == 0){
                        echo 
    "<p>Lesson 2: ".$info['lesson2'] . " </p>";}
                        
                             else { if (
    $info['lesson2'] > 100){
                                    echo 
    "<h1>Lesson 2: ".$info['lesson2'] . " </h1>";}}        
                        
                        if (
    $info['lesson3'] == 0){
                        echo 
    "<p>Lesson 3: ".$info['lesson3'] . " </p>";}
                        
                            else {if (
    $info['lesson3'] > 100){
                                    echo 
    "<h1>Lesson 3: ".$info['lesson3'] . " </h1>";}}    
                        
                        if (
    $info['lesson4'] == 0){
                        echo 
    "<p>Lesson 4: ".$info['lesson4'] . " </p>";}
                        
                            else { if (
    $info['lesson4'] > 100){
                                    echo 
    "<h1>Lesson 4: ".$info['lesson4'] . " </h1>";}     }    
                        
                                    
                    
                    }
                    
                    }        
                    else die(
    "You must log in");
                    
                    

                    
    ?>
    Could you PLEASE help me?

    Thanks a lot!
    Last edited by cast_no_shadow; 08-20-2012 at 03:16 PM.

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Use a function.


    PHP Code:
    <?php

    function output ($lesson$score) {
    if (
    $score 0) {
    echo 
    "Lesson: $lesson Score: $score<br>";
    }
    if (
    $score 100) {
    echo 
    "<h1>Lesson: $lesson Score: $score</h1>";
    }
    return;
    }


    output ('3','74');
    output ('3','0');
    output ('3','124');
    output ('3','79');
    output ('4','0');
    output ('3','0');
    ?>

    Output
    Lesson: 3 Score: 74
    Lesson: 3 Score: 124
    <h1>Lesson: 3 Score: 124</h1>
    Lesson: 3 Score: 79


    -----
    Last edited by Len Whistler; 08-20-2012 at 02:09 PM.
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    cast_no_shadow (08-20-2012)

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    The way you have the code now your explanation of what you want shown is not correct. You said

    I want to print the scores of every lesson only if the score is higer than 0.
    The way you have it now it will only show something if the lesson value is 0 or greater than 100. Also, please tell us you do not have a single table with a column for every lesson! If you do please look into normalization of your data.

    Here's an easy way to accomplish what you are trying to do based on what your stated criteria is.

    PHP Code:
    <?php
    $lessonmax 
    3;  // set this variable to the maximum number of lessons
    if ($_SESSION['username']) {
        
    $username $_SESSION['username'];
        echo 
    "<h1>Welcome, $username</h1>";
        include(
    "connect.php");
        
    $data mysql_query("SELECT * FROM users WHERE username='$username'") or die(mysql_error());
        
    $info mysql_fetch_array($data);
        echo 
    "<h2>User Information</h2>
             <p>Username: $username<p>
             <hr>
             <p>Total points: " 
    $info['total_points'] . " </p>";

        for (
    $lesson 1;$lesson <= $lessonmax;$lesson++) {
            
    $lessonvalue $info['lesson' $lesson];
            if (
    $lessonvalue 100){
                echo 
    "<h1>Lesson " $lesson ": " $lessonvalue "</h1>";
            } elseif (
    $lessonvalue 0) {
                echo 
    "<p>Lesson "  $lesson ": " $lessonvalue "</p>";
            }
        }
    } else {
        die(
    "You must log in");
    }
    ?>
    Last edited by djm0219; 08-20-2012 at 02:19 PM.
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    cast_no_shadow (08-20-2012)

  • #4
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by djm0219 View Post
    Also, please tell us you do not have a single table with a column for every lesson! If you do please look into normalization of your data.
    Thanks a lot for your help.

    I'm afraid that yes, I have a column for every lesson.... (I'm new in coding).

    I don't understand what you mean by "normalization of your data", Could you please elaborate?

    Thanks again.

  • #5
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Quote Originally Posted by cast_no_shadow View Post
    I don't understand what you mean by "normalization of your data", Could you please elaborate?
    http://www.tuxradar.com/practicalphp/9/8/0
    http://en.wikipedia.org/wiki/Database_normalization
    http://support.microsoft.com/kb/283878
    http://databases.about.com/od/specif...malization.htm

    NOW is the time to do it. It will make your life a LOT easier. It's easier to do than most people realize.
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    cast_no_shadow (08-20-2012)

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    "normalization of your data" - If your columns hold the same info then you might be better off moving that info to a new table, in your case instead of one table you would require three tables and then JOIN them during the query.


    Code:
    STUDENTS
    student_id | student_name
    00001 | Ben
    00002 | Larry
    00003 | Cathy
    00004 | Tom
    
    LESSONS
    lesson_id | lesson
    01 | boating
    02 | basket weaving
    03 | fire safety
    04 | typing
    
    
    STUDENT_SCORES
    student_id | lesson_id | score
    00002 | 02 | 56
    00002 | 04 | 0 
    00003 | 01 | 143
    00002 | 03 | 43
    00001 | 04 | 456
    00003 | 04 | 23


    -----
    Last edited by Len Whistler; 08-20-2012 at 04:01 PM.
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    cast_no_shadow (08-20-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •