Can anyone help me condense this WORKING code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aalbrand
Forum Newbie
Posts: 11
Joined: Tue Sep 15, 2009 12:06 am

Can anyone help me condense this WORKING code

Post by aalbrand »

Hi, I am still learning. I have completed a form but feel it is a little redundant. Can someone show me how to condense what i already have? I am not too worried about the array right now, but more about the If statements. I think i can create a function and use one foreach loop for all of the class levels but not sure. Any input would be most appreciative.

Code: Select all

<?php
 
// Start of Array for CourseList
 
$classes = array ();
    $classes[0] = array();
        $classes[0]['Prefix'] = "CTEC";
        $classes[0]['Number'] = "2350";
        $classes[0]['Title'] = "Intro to Communication Technology";
        $classes[0]['Room'] = "FAB 412";
    $classes[1] = array();
        $classes[1]['Prefix'] = "CTEC";
        $classes[1]['Number'] = "3350";
        $classes[1]['Title'] = "Website Communication";
        $classes[1]['Room'] = "FAB 411";
    $classes[2] = array();
        $classes[2]['Prefix'] = "BCMN";
        $classes[2]['Number'] = "3319";
        $classes[2]['Title'] = "Broadcast Management";
        $classes[2]['Room'] = "FAB 327";
    $classes[3] = array();
        $classes[3]['Prefix'] = "BCMN";
        $classes[3]['Number'] = "3340";
        $classes[3]['Title'] = "Electronic News";
        $classes[3]['Room'] = "FAB 112";
    $classes[4] = array();
        $classes[4]['Prefix'] = "ADVT";
        $classes[4]['Number'] = "4305";
        $classes[4]['Title'] = "Advertising Media";
        $classes[4]['Room'] = "FAB 409";
        //
        //
// End of Array
?>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Forum code</title>
</head>
 
<body>
<div id="header">
    <h3> Form Processing </h3>
    <hr>
</div>
<div id="Form">
 
 <b>Class List</b>
  <form action="" method="post">
    Please select a course level: 
 
    <select name="Number">
        <option value="ALL" <?php if ($_POST['Number'] == 'ALL') echo'selected="selected" '?>>ALL</option>
        <option value="1000" <?php if ($_POST['Number'] == '1000') echo'selected="selected" '?>>1000 Level</option>
        <option value="2000" <?php if ($_POST['Number'] == '2000') echo'selected="selected" '?>>2000 Level</option>
        <option value="3000" <?php if ($_POST['Number'] == '3000') echo'selected="selected" '?>>3000 Level</option>
        <option value="4000" <?php if ($_POST['Number'] == '4000') echo'selected="selected" '?>>4000 Level</option>
    </select>
    <input type="Submit" name="Show" value="Show Courses">
  </form>
 
<hr>
 
 
<?php
 
//Beginning of Argument
 
if ($_POST['Number'] == 'ALL') {
    echo "All courses are dislpayed.<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
        $prefix = $c['Prefix'];
        $number = $c['Number'];
        $title = $c['Title'];
        $room = $c['Room'];
            if ($number > 1000) {
                echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
            } else {
            }
    }
    echo "</table>";
} elseif ($_POST['Number'] == '1000') {
    echo "There were no matches found.<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
        $prefix = $c['Prefix'];
        $number = $c['Number'];
        $title = $c['Title'];
        $room = $c['Room'];
            if ($number >= 1000 && $number <= 1999) {
                echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
            } else {
            }
    }
    echo "<tr><td colspan='3' align='center'>There were no matches found at this course level.</td></tr>";
 
} elseif ($_POST['Number'] == '2000') {
    echo "1 course has matched your search query.<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
        $prefix = $c['Prefix'];
        $number = $c['Number'];
        $title = $c['Title'];
        $room = $c['Room'];
            if ($number >= 2000 && $number <= 2999) {
                echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
            } else {
            }
    }
} elseif ($_POST['Number'] == '3000') {
    echo "3 courses have matched your search query.<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
        $prefix = $c['Prefix'];
        $number = $c['Number'];
        $title = $c['Title'];
        $room = $c['Room'];
            if ($number >= 3000 && $number <= 3999) {
                echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
            } else {
            }
    }
} elseif ($_POST['Number'] == '4000') {
    echo "1 course has matched your search query.<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
        $prefix = $c['Prefix'];
        $number = $c['Number'];
        $title = $c['Title'];
        $room = $c['Room'];
            if ($number >= 4000 && $number <= 4999) {
                echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
            } else {
            }
    }
}
 
//end of Argument
 
?>
 
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Can anyone help me condense this WORKING code

Post by Weiry »

i realize this is kind of messy, but i did condense all if statements to one statement.

Code: Select all

 
//Beginning of Argument
 $numberArray = array("ALL",1000,2000,3000,4000);$noResults=false;
if (in_array($_POST['Number'],$numberArray)) {
    print "<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
        if ($c['Number'] >= $_POST['Number'] && $c['Number'] <= ($_POST['Number']+999)) {
            echo "<tr><td>{$c['Prefix']} {$c['Number']}</td><td>{$c['Title']}</td><td>{$c['Room']}</td></tr>";
            $noResults = true;
        }elseif($_POST['Number'] == "ALL"){
            echo "<tr><td>{$c['Prefix']} {$c['Number']}</td><td>{$c['Title']}</td><td>{$c['Room']}</td></tr>";
            $noResults = true;
        }
    }
    if(!$noResults){
            echo "<tr><td colspan='3' align='center'>There were no matches found at this course level.</td></tr>";
        }
}
print "</table>";
//end of Argument
 
For condensing your original array, you should consult this thread:
Multidimensional Array + Function
aalbrand
Forum Newbie
Posts: 11
Joined: Tue Sep 15, 2009 12:06 am

Re: Can anyone help me condense this WORKING code

Post by aalbrand »

Thanks Wiery!

Only 1 problem though with the condensed code. when i had them so they were not colapsed into one statement, I was able to tell the user how many courses were being displayed via "echo"

Ex. "1 result has matched your search query"

Code: Select all

//Beginning of Argument
 
if ($_POST['Number'] == 'ALL') {
    echo "[color=#0000FF]All courses are dislpayed[/color].<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
        $prefix = $c['Prefix'];
        $number = $c['Number'];
        $title = $c['Title'];
        $room = $c['Room'];
            if ($number > 1000) {
                echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
            } 
            else {}
    }
    echo "</table>";
} elseif ($_POST['Number'] == '1000') {
    echo "[color=#0000FF]There were no matches found[/color].<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
        $prefix = $c['Prefix'];
        $number = $c['Number'];
        $title = $c['Title'];
        $room = $c['Room'];
            if ($number >= 1000 && $number <= 1999) {
                echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
            } 
            else {}
    }
    echo "<tr><td colspan='3' align='center'>There were no matches found at this course level.</td></tr>";
 
} elseif ($_POST['Number'] == '2000') {
    echo "[color=#0000FF]1 course has matched your search query[/color].<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
        $prefix = $c['Prefix'];
        $number = $c['Number'];
        $title = $c['Title'];
        $room = $c['Room'];
            if ($number >= 2000 && $number <= 2999) {
                echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
            } 
            else {}
    }
How would i implement this in the new code?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Can anyone help me condense this WORKING code

Post by infolock »

sorry, i didn't really try to solve your problem, just kind of refactored your code a bit more...

Code: Select all

 
//Beginning of Argument
$prefix = $c['Prefix'];
$number = $c['Number'];
$title  = $c['Title'];
$room   = $c['Room'];
switch($_POST['Number']) {
  case 'ALL':
    echo "All courses are dislpayed.<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
      if($number > 1000) echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
    }
    echo "</table>";
  break;
  case '1000':
    echo "There were no matches found.<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach ($classes as $c) {
      if($number >= 1000 && $number <= 1999) echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
    }
    echo "<tr><td colspan='3' align='center'>There were no matches found at this course level.</td></tr>";
  break;
  case '2000':
    echo "1 course has matched your search query.<table border='1'><tr><thead><th>Course Number</th><th>Title</th><th>Room</th></thead></tr>";
    foreach($classes as $c) {
      if ($number >= 2000 && $number <= 2999) echo "<tr><td>$prefix $number</td><td>$title</td><td>$room</td></tr>";
    }
  break;
}
 
I have a feeling this can (and probably will around here) go on for a long time (the refactoring thing).

Good luck
Post Reply