Problem to display array in table

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
xecvantec
Forum Newbie
Posts: 2
Joined: Fri Sep 23, 2011 3:28 am

Problem to display array in table

Post by xecvantec »

Hi every body, i help a problem with php.
I want to create form like image 1:

When click up or down, it sort name (up or down) and mark (up or down)
When click link update, information of student show in textfield and button Update to do it.
When click link delete, record will be delete. Button check all will delete all record checked box.

Database use array.


I 've already to sort by mark, but when display in table, it doesn't work. (like image 2)

Code: Select all

<!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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
	$Student = array(); 
    $Student["Nguyen Van AA"] = array("name" => "Nguyen Van A",                              
                                "mark" =>10); 
    $Student["Le Thanh BB"] = array("name" => "Le Thanh B",                              
                                "mark" =>30); 
	$Student["Tran Thi HH"] = array("name" => "Tran Thi H",                              
                                "mark" =>40);
	$Student["Do Anh CC"] = array("name" => "Do Anh C",                              
                                "mark" =>20); 
	

   
function diemTang () {
	function compare_mark($in_Stu1, $in_Stu2) 
    { 
        if ($in_Stu1["mark"] > $in_Stu2["mark"]) 
        { 
            return 1; 
        } 
        else if ($in_Stu1["mark"] == $in_Stu2["mark"]) 
        { 
            return 0; 
        } 
        else 
        { 
            return -1; 
        } 
    } 
	
}
	
	diemtang();
	uasort($Student, "compare_mark");
	foreach ($Student as $Student) 
    	{ 
       	 echo "Student: {$Student['name']} have mark: {$Student['mark']}<br/>\n"; 
    	} 
?>
<form>
  <table width="500" border="0">
    <tr>
      <td>&nbsp;</td>
      <td>Name</td>
      <td>Mark</td>
      <td>Update</td>
      <td>Delete</td>
    </tr>
    
    <?php
	foreach ($Student as $Student) 
    	{ 
		 ?>
       	  <tr>
		 <td><input name="chkStu" type="checkbox" value="" /></td>
      <td> <?php echo $Student["name"] ?></td>
      <td><?php echo $Student["mark"]?></td>
      
      <td></td>
      <td>&nbsp;</td>
    </tr> 
	 <?php 
    	} 
	?>
    
  </table>
	
</form>
</body>
</html>
How i fix it?
And how to add, delete,update, delete all?
Attachments
1.JPG
2.JPG
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Problem to display array in table

Post by McInfo »

xecvantec wrote:How i fix it?
The cause of the problem is that the same name is used for both arguments in the foreach loop.

Code: Select all

foreach ($Student as $Student)
When the loop finishes, the input array has been replaced by the last item from the array.
xecvantec wrote:And how to add, delete,update, delete all?
First, you need to store the data somewhere more permanent than an in-memory array, such as a session or database.
Post Reply