Page 1 of 1

Multidimensional Array + Function

Posted: Tue Sep 15, 2009 12:13 am
by aalbrand
Hello, I am new to these forums and seeking help on a school assignment. I have just started using php this semester and some things are confusing to me. Here is the assignment:

* Build a multidimensional array storing class information as listed below. (Each class should be stored in an array containing at least three elements: class number, title, and room.) (10 points)
o CTEC 2350 Intro to Communication Technology FAB 412
o CTEC 3350 Website Communication FAB 411
o BCMN 3319 Broadcast Management FAB 327
o BCMN 3340 Electronic News FAB 112
o ADVT 3305 Advertising Media FAB 409

* Produce a function, showClass, which will take in an argument, $prefix, and produce a class list that matches the class prefix supplied by $prefix. For example, when you call the function showClass('CTEC'), only CTEC classes will be listed.

* To demonstrate the functionality of your script, call the showClass function with the argument set to $_GET['prefix'] at the end of your script block like this: showClass($_GET['prefix']);

Here is what i have so far

Code: Select all

<?php
 
    //multidimensional array exercise
    //associative array
 
 
 
$clists = array ();
    $clists[0] = array();
        $clists[0]['Course Number'] = "CTEC 2350";
        $clists[0]['Title'] = "Intro to Communication Technology";
        $clists[0]['Room'] = "FAB 412";
    $clists[1] = array();
        $clists[1]['Course Number'] = "CTEC 3350";
        $clists[1]['Title'] = "Website Communication";
        $clists[1]['Room'] = "FAB 411";
    $clists[2] = array();
        $clists[2]['Course Number'] = "BCMN 3319";
        $clists[2]['Title'] = "Broadcast Management";
        $clists[2]['Room'] = "FAB 327";
    $clists[3] = array();
        $clists[3]['Course Number'] = "BCMN 3340";
        $clists[3]['Title'] = "Electronic News";
        $clists[3]['Room'] = "FAB 112";
    $clists[4] = array();
        $clists[4]['Course Number'] = "ADVT 3305";
        $clists[4]['Title'] = "Advertising Media";
        $clists[4]['Room'] = "FAB 409";
 
        function showclass ($prefix){
                        
        }
            
 
            foreach ($clists as $clist){
                echo '<tr>';
                foreach ($clist as $key => $value){
                    echo "<td>$value</td>";
                    }
                    echo '</tr>';
                }
 
?>
 
Thanks!

Re: Multidimensional Array + Function

Posted: Tue Sep 15, 2009 12:28 am
by Weiry
ok il give you a bit of advice, as much as multidimentional arrays are great in the form of

Code: Select all

$yourArray[0][0] = "myVar";
$yourArray[0][1] = "foo";
$yourArray[1][0] = "yourVar";
$yourArray[1][1] = "bar";
$yourArray[2][0] = "hisVar";
You should really try using something like

Code: Select all

 
$clists = array(
   array( "courseNumber" => "CTEC 2350",
            "courseTitle" => "Intro to Communication Technology",
            "courseRoom" => "FAB 412"
   ),
   array( "courseNumber" => "CTEC 3350",
            "courseTitle" => "Website Communication",
            "courseRoom" => "FAB 411"
   )
);
 
This will essentially create your multidimensional array, but far easier to read.
You can now run through each array with a simple foreach loop, and notice the:

Code: Select all

"courseTitle" => "Website Communication",
You can now call the title of the website as an associative array rather than having to explicitly define the index of the data.
Or... $arrayName['courseTitle'] will return "Website Communication".. or the explicit, $arrayName[2] will return the same thing. The only difference is that for readability, the associative is by far easier to understand.

So your modified foreach loop would look like

Code: Select all

foreach($clist as $subject){
   print "Course Code: ".$subject['courseCode']."<br/>";
   print "Course Title: ".$subject['courseTitle']."<br/>";
   print "Room: ".$subject['courseRoom']."<br/>";
}

Re: Multidimensional Array + Function

Posted: Tue Sep 15, 2009 12:42 am
by redrocket1855
hey weiry why does my code not want to work

Code: Select all

<?php
$classes = array (
    array('CTEC 2350','Intro to Communication Technology','FAB 412'),
 
    array('CTEC 3350','Website Communication','FAB 411'),
 
    array('BCMN 3319','Broadcast Management','FAB 327'),
 
    array('BCMN 3340','Electronic News','FAB 112'),
 
    array('ADVT 3305','Advertising Media','FAB 409')
);
?>
<table border="1">
<tr>
 <th>Class Number</th>
 <th>Title</th>
 <th>Room</th>
</tr>
<?php
foreach($classes as $class)
{
 echo '<tr>';
 foreach($class as $item)
 {
  echo "<td>$item</td>";
 }
 echo '</tr>';
}
 
foreach($classes as $subject){
print "Course Code: ".$subject['0']."<br/>";
print "Course Title: ".$subject['1']."<br/>";
print "Room: ".$subject['2']."<br/>";
}
 
 
?>
</table>

Re: Multidimensional Array + Function

Posted: Tue Sep 15, 2009 12:49 am
by Weiry
because there is no field named

Code: Select all

$subject['2']
the '2' is indicating that the array is looking for an association field named '2', its not referring to the location,
in your case, if you changed:

Code: Select all

"Room: ".$subject['2']."<br/>";
to

Code: Select all

"Room: ".$subject[2]."<br/>";
it should be ok

Re: Multidimensional Array + Function

Posted: Tue Sep 15, 2009 12:59 am
by aalbrand
Sorry, i forgot to mention it was suppose to be in a table. So here is my amended code. Thanks for your help so far Weiry, i really appreciate it.

I need help on the function part of the code, I don't know how to do what he is asking.

Code: Select all

 
 <table border="1">
<tr>
 <th><b>Course Number</b></th>
 <th><b>Title</b></th>
 <th><b>Room</b></th>
 
</tr>
 
 $clists = array(
    array( "courseNumber" => "CTEC 2350",
             "courseTitle" => "Intro to Communication Technology",
             "courseRoom" => "FAB 412"
        ),
    array( "courseNumber" => "CTEC 3350",
             "courseTitle" => "Website Communication",
             "courseRoom" => "FAB 411"
        ),
    array( "courseNumber" => "BCMN 3319",
             "courseTitle" => "Broadcast Management",
             "courseRoom" => "FAB 327"
        ),  
    array( "courseNumber" => "BCNM 3340",
             "courseTitle" => "Electronic News",
             "courseRoom" => "FAB 112"
        ),
    array( "courseNumber" => "ADVT 3305",
             "courseTitle" => "Advertising Media",
             "courseRoom" => "FAB 409"
        )
    );
 
 
        /*function showclass ($prefix){ [color=#FF0000]//my trouble area!![/color]
                        
        }*/
            
 
            foreach ($clists as $clist){
                echo '<tr>';
                foreach ($clist as $key => $value){
                    echo "<td>$value</td>";
                    }
                    echo '</tr>';
                }
 
?>

Re: Multidimensional Array + Function

Posted: Tue Sep 15, 2009 1:12 am
by Weiry
well reading the requirement that your supposed to impliment $_GET['prefix'] to show more specific details about a class.
I would display the table of all classes in the format of:

Code: Select all

 
<table>
<?php
foreach($clist as $subject){
print "
<tr><form action='' method='get'>
   <td>{$subject['courseTitle']}</td>
   <td>
      <input type='hidden' name='prefix' value='{$subject['courseCode']}' />
      <input type='submit name='prefixSubmit' value='View More Info' />
   </td>
</form></tr>";
}?>
</table>
 
(But that is just my opinion)
Of course you would format the table a little better using width etc.

But on the initial page you wouldnt display any other information, then when the button "View More Info" is clicked next to the corrosponding course title, it will redirect the form to the same page you are currently on (or you can use the current filename)

From there, at the very top of your code, would be a check to see if you have any value in 'prefix'.
As the code above uses the courseCode as the prefix, all you would need to do in your foreach loop, is add a check to see if the current item's code is the same as the prefix specified, then print the course information.
Or, if the prefix is not set, then print the regular table without more specific course information

hint: you can just reuse your current foreach loop
also: http://www.php.net/isset

EDIT: you may consider changing your course codes to not include spaces

Re: Multidimensional Array + Function

Posted: Tue Sep 15, 2009 1:39 am
by aalbrand
So now i have a new problem, with the amended code implemented there are cells that are placed oddly.

Code: Select all

<table border="1">
<tr>
 <th><b>Course Number</b></th>
 <th><b>Title</b></th>
 <th><b>Room</b></th>
 
</tr>
 
<?php
 
    //multidimensional array exercise
    //associative array
 
 
 $clists = array(
    array( "courseNumber" => "CTEC 2350",
             "courseTitle" => "Intro to Communication Technology",
             "courseRoom" => "FAB 412"
        ),
    array( "courseNumber" => "CTEC 3350",
             "courseTitle" => "Website Communication",
             "courseRoom" => "FAB 411"
        ),
    array( "courseNumber" => "BCMN 3319",
             "courseTitle" => "Broadcast Management",
             "courseRoom" => "FAB 327"
        ),  
    array( "courseNumber" => "BCNM 3340",
             "courseTitle" => "Electronic News",
             "courseRoom" => "FAB 112"
        ),
    array( "courseNumber" => "ADVT 3305",
             "courseTitle" => "Advertising Media",
             "courseRoom" => "FAB 409"
        )
    );
?>
 
 
 <?php
             foreach ($clists as $clist){
                echo '<tr>';
                foreach ($clist as $key => $value){
                    echo "<td>$value</td>";
                    }
                    echo '</tr>';
                }
 
            foreach($clist as $subject){
                print "
                    <tr><form action='' method='get'>
                    <td>{$subject['courseTitle']}</td>
                    <td>
                        <input type='hidden' name='prefix' value='{$subject['courseCode']}' />
                        <input type='submit name='prefixSubmit' value='View More Info' />
                    </td>
             </form></tr>";
            }
 ?>
 </table>
I am not sure we are suppose to use forms on this assignment, it was suppose to be done with Function. We have not gone over forms yet, so I am pretty sure about this.

here is what it currently appears as: http://omega.uta.edu/~aea8532/4321/Arra ... p?prefix=A

Re: Multidimensional Array + Function

Posted: Tue Sep 15, 2009 4:22 am
by Weiry
Ok, as your code stands in the table, you have some fields which are set out like:

Code: Select all

 
<tr>
    <td>ADVT 3305</td>
    <td>Advertising Media</td>
    <td>FAB 409</td>
</tr> 
 
But when you are printing your forms using:

Code: Select all

 
<tr>
    <form action='' method='get'> 
    <td>A</td> 
    <td> 
        <input type='hidden' name='prefix' value='A' /> 
        <input type='submit' name='prefixSubmit' value='View More Info' /> 
    </td> 
</form></tr>
If you look closely between the two, the first is using 3 table cells, and your second with the buttons are only using 2 table cells ( <td></td> )

As for the function/forms part, thats my bad, usually when i code things like this, i would have something a little more, just to prove the fact that my functions are working correctly.

Yes, you could get away with just writing the function, then calling the function just through php. So if you were not to include any functionality, you would write your display function to show all the detailed information (possibly you are supposed to call this when printing your table fields out). Although you would have to call your function like:

Code: Select all

 
<!-- all code before function -->
function showClass($classCode){
   foreach
      if($classCode==
         return $arrayOfClassInformation // or depending if you have to show all things including the table here
}
<!-- rest of the code after function -->
// call display function where you need it
showClass("CTEC 3250");
 
So when you want to display all your information from your $clist array, you would have something like:

Code: Select all

 
foreach($clist as $class)
   showClass($class['courseCode']);   // Note: all your table formatting would be moved to the function.
 
Now here is the only dilemma im seeing currently. It is, how much should "showClass" print exactly, does this mean it should print the entire table and all of its contents, or show contents only. I suppose it depends on how you read it.