www.webdeveloper.com
Results 1 to 3 of 3

Thread: is there a way to make this code short?

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    3

    Question is there a way to make this code short?

    hello,

    i'd like to have help here with my code here i don't know if theres a way but im trying to make a form exercises code like this
    http://www.englisch-hilfen.de/en/exe...le_present.htm

    and i tried my code so far i go this

    PHP Code:

    <?php
     

    $correctSolution1 
    'do';
    $correctSolution2 'do';
    ?>
    <form  method="post" autocomplete="off">
      


            <table align=left width="700px" bgcolor="#FFCC99" border="0" cellspacing="0" cellpadding="1">
            
              <tr>
                <td>
                  <table width="100%" bgcolor="#FFFFCC" cellspacing="4" cellpadding="1">
                  
                    </td>
                    </tr>
                    
                    <tr>
                        <td>
                            <table>
                            <?php


    if(isset($_POST['add']) == TRUE) {
        
       
        
        
    $solution1 $_POST['solution1'];
        
    $solution2 $_POST['solution2'];
        
       
        if(empty(
    $solution1) == TRUE) {
        if(empty(
    $solution2) == TRUE) {
            echo 
    '<td><span style="color: blue;">"unanswered"</span> you <span style="color: blue;">"unanswered"</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td></br>';
       return;
        }}
        if(empty(
    $solution1) == TRUE) {
        if (
    $solution2 == $correctSolution2) {
            echo 
    '<td>1)  <span style="color: blue;">"unanswered"</span> you <span style="color: green;">'.$solution2.'</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
        return;}}
        if(
    $solution1 == $correctSolution1 ) {
        if(empty(
    $solution2) == TRUE) {
            echo 
    '<td>1)  <span style="color: green;">'.$solution1.'</span> you <span style="color: blue;">"unanswered"</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
        return;}}
        if(
    $solution1 != $correctSolution1 ) {
        if(empty(
    $solution2) == TRUE) {
            echo 
    '<td>1)  <span style="color: red;">'.$solution1.'</span> you <span style="color: blue;">"unanswered"</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
        return;}}
        if(empty(
    $solution1) == TRUE) {
        if (
    $solution2 != $correctSolution2) {
            echo 
    '<td>1)  <span style="color: blue;">"unanswered"</span> you <span style="color: blue;">'.$solution2.'</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
        return;}}
        
        
       
        
        if(
    $solution1 == $correctSolution1 ) {
        if (
    $solution2 == $correctSolution2) {
            echo 
    '<td>1)  <span style="color: green;">'.$solution1.'</span> you <span style="color: green;">'.$solution2.'</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
        return;}}
        if(
    $solution1 == $correctSolution1 ) {
        if (
    $solution2 != $correctSolution2) {
            echo 
    '<td>1)  <span style="color: green;">'.$solution1.'</span> you <span style="color: red;">'.$solution2.'</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
        return;}}
        if(
    $solution1 != $correctSolution1 ) {
        if (
    $solution2 == $correctSolution2) {
            echo 
    '<td>1)  <span style="color: red;">'.$solution1.'</span> you <span style="color: green;">'.$solution2.'</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
        return;}}
        
        
        if(
    $solution1 != $correctSolution1 ) {
        if (
    $solution2 != $correctSolution2) {
            echo 
    '<td>1)  <span style="color: red;">'.$solution1.'</span> you <span style="color: red;">'.$solution2.'</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
        return;}}
        
        
        
        
        echo 
    '<tr><td><a href="' $_SERVER['HTTP_REFERER'] . '"><input type="button"  value="Repeat test" /></a></td></tr>';
        
    }

    else {

        
    ?>
                            
                            


                            
                                                                <tr><td  colspan="2">1) <input type="text"  name="solution1"  /> you <input type="text"  name="solution2"  /> mineral water? <b><i>(to drink)</i></b></td></tr>

                                                        </table>
                                                        
                        </td>
                    </tr>
                    
                    <tr><td height="1"><hr></td></tr>
                    <tr>
                      <td align="left"><input class="small_button" type="submit" name="add" value="Check" />
                      
                          <hr>
                          

    </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
                                                                  <?php
                                                            
    }

    ?>
    </form>
    as well i don't know how to make that button of show answers that gives answers without taking exercise

    any help
    Last edited by NogDog; 11-21-2013 at 09:28 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    I do not have the time or inclination to go through all of your code, but here's one example of how you can get rid of some duplication:
    PHP Code:
        if ($solution1 != $correctSolution1
        {
            if (
    $solution2 == $correctSolution2
            {
                echo 
    '<td>1)  <span style="color: red;">' $solution1 '</span> you <span style="color: green;">' $solution2 '</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
                return;
            }
        }
        if (
    $solution1 != $correctSolution1
        {
            if (
    $solution2 != $correctSolution2
            {
                echo 
    '<td>1)  <span style="color: red;">' $solution1 '</span> you <span style="color: red;">' $solution2 '</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
                return;
            }
        } 
    ...could be reduced to:
    PHP Code:
        if ($solution1 != $correctSolution1
        {
            
    $color 'red';
            if (
    $solution2 == $correctSolution2
            {
                
    $color 'green';
            }
            echo 
    '<td>1)  <span style="color: red;">' $solution1 '</span> you <span style="color: '.$color.';">' $solution2 '</span> lemon?</td><td> <i>Correct answer: </i> <font color="006633"><b>Do</b></font> you <font color="006633"><b>drink</b></font> mineral water?</td> </br>';
            return;
        } 
    This is an example of applying the Don't Repeat Yourself (DRY) principle of writing cleaner, easier to maintain code. In this case, among other things, we're avoiding having virtually the same HTML code being output in to different echo statements, and getting rid of superfluous if conditions.
    Last edited by NogDog; 11-21-2013 at 09:47 AM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Nov 2013
    Posts
    3
    thank you friend for explanations and help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles