How to use an array to auto fill a form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sitingduck
    New Member
    • Jan 2011
    • 1

    #1

    How to use an array to auto fill a form?

    I used this code to pull info from the database and into the array.
    Code:
    $stu_quary = "SELECT `uname` FROM `user` WHERE level = 'Normal' AND class = '".$_SESSION['class']."'"; 
    	$stu_names=mysql_query($stu_quary);
    	$stuArr=array();
    	while($get_stu_info =mysql_fetch_assoc($stu_names)) 
    	{ 
    	$n = 0;
        $stuArr[] = $get_stu_info['uname']; 
    	$_SESSION['selected_s'] = current($stuArr);
    	}
    I then use the session to auto full the form.
    When submited I want the next value in the array to be applied to th session.
    Code:
    if(isset($_POST['postComment']))
    {
    	$query = "INSERT INTO ".$_POST['student_name']."_".$_POST['class']." SET name='".$_POST['student_name']."',title='".$_POST['student_name']." Report for Week: ".$_POST['week']."',subject='".$_POST['subject']."',class='".$_POST['class']."',author='".$_POST['author']."',week='".$_POST['week']."',par='".$_POST['par']."',beh='".$_POST['beh']."',lis='".$_POST['lis']."',spe='".$_POST['spe']."',mot='".$_POST['mot']."',ret='".$_POST['ret']."',comments='".$_POST['comment']."',";
    	$query .="date_posted=NOW(),categoryID='".$_POST['select']."',artchild='0'";
    	if(!mysql_query($query))
    	{		
    		echo mysql_error();
    	}
    	else
    	{
    		$_SESSION['selected_s'] = next($stuArr);
    	}
    I use this code but it only works for the first time a form is submited afterwards the value stays the same.
    Is is next() the right function?
    What would be the bst way to achieve this?
    Thanks in advance.
  • mfaisalwarraich
    New Member
    • Oct 2007
    • 194

    #2
    i think u need to get relative values. try with this one. i have altered this function to get its relative values instead of keys.:

    Code:
    function array_key_relative($array, $current_key, $offset = 1)
    {
    	 // create key map
    	 $keys = array_values($array);
    	 // find current key
    	 $current_key_index = array_search($current_key, $keys);
    	 // return desired offset, if in array, or false if not
    	 if(isset($keys[$current_key_index + $offset]))
    	 {
    		 return $keys[$current_key_index + $offset];
    	 }
    	 return false;
    }
    //next value :
    array_key_relative($$stuArr,$current_value,1);
    //previous value
    array_key_relative($$stuArr,$current_value,-1);

    Comment

    Working...