Page 1 of 1

php beginner help

Posted: Fri Feb 19, 2010 4:25 pm
by Shepy87
Hi I'm new to php but if someone could please read the question below and help it would be great.

a)create an html page to enter a row number (1-6) in a textbox to choose a list of student records.

b) Create a php script to receive the chosen row number
- Assign 6 values (rows) into a 2 dimensional sequential array using the array function:
$list = array(array(42356, ‘J Smith’,55), ………,array(45243, ‘M Fisher’,39) );

- The 6 values should be those of the student number (5 figure integer), student name and
module mark for 6 different students using values of your choice

-Print out the name of the student (got from the student list array), then underneath return an html table with the complete details of the student for the row chosen (got from the student list array), along with table heading values Student Number, Student Name & Module Mark.

Below is the code I have so far, if someone could help me with the rest it would be great.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 
<title></title>
 
</head>
 
<body>
 
<form name="input" action="qu3php.php" method="post">
Enter a row number 1-6:<br/>
<input type="text" name="row" /> <br/>
<input type="submit" value="Submit" />
</form> 
</body>
</html>
 

Code: Select all

<?php
 
$list = array(
        array(42356,'J Smith',55), 
        array(45243,'M Fisher',39), 
        array(45363,'T White',77), 
        array(45676,'J Blogs',68),
        array(43212,'M Grey',45), 
        array(43576,'T Brown',49) ); 
 
print  "<table border=1><tr><th>Student Number</th> <th>Student Name</th> <th>Module Mark</th></tr>";
print  "<tr> <td> $list[0][0]</td>";
print  "<tr> <td> $list[0][1]</td>";
print  "<td> $list[0][2]</td></tr>";
print  "</table>";  // prints the first row 
 
?>

Re: php beginner help

Posted: Fri Feb 19, 2010 5:07 pm
by Weiry
Firstly, Welcome to DevNet :)

Secondly, it looks like you have the right idea about how to go about doing this, although personally when working with array's, especially ones with multiple different types of data, i always use an associative array. These are much easier to work with in your code as your are always referring to a string based location.

Example:

Code: Select all

$assocArray = array(
                array("type" => "apple", "price" => 0.59, "colour" => "green"),
                array("type" => "apple", "price" => 0.54, "colour" => "red"),
                array("type" => "orange", "price" => 0.74, "colour" => "orange")
            );
 
/* This makes things much easier if i had to loop through and display each row */
 
foreach($assocArray as $fruit){
    print "Type: ".$fruit['type']."Price: ".$fruit['price']."Colour: ".$fruit['colour']."<br/>";
}
As for getting results from user input, you will want to use either $_GET or $_POST depending on your needs of course :)

Re: php beginner help

Posted: Fri Feb 19, 2010 5:50 pm
by Shepy87
thank you for your help.

One thing I don't understand is when I type in 4 for example in the text box how would I get this to print that row e.g 45676,'J Blogs',68

Re: php beginner help

Posted: Fri Feb 19, 2010 5:58 pm
by Weiry
To receive information:
W3Schools - PHP $_GET Function
W3Schools - PHP $_POST Function

As for getting a particular row?

Code: Select all

 
if(!empty($assocArray[x])){  // Where 'x' is the value of the row
    print_r ($assocArray[x]);  // you wish to return.
}