Welcome Guest, Not a member yet? Register   Sign In
populating a dropdown dynamically
#1

[eluser]Delvin[/eluser]
Hi there,

I am working on a school management system project and need some help in populating some query results in a dropdown menu.

Below is what I have done; I have queried the db and would need to pass these details to the view file in a 2 dimensional array.


$query = $this->db->query('SELECT Class_ID, First_Name, Middle_Name, Last_Name from student');
foreach ($query->result_array() as $row){
$temp = $row['Class_Name'];
$temp1 = $row['First_Name'].$row['Middle_Name'].$row['Last_Name'];
}
$data['Class_name']=$temp;
$data['Std_name']=$temp1;
$this->load->view('m_student/classlist_stdview.php',$data);

*********************************888
In the view file:

Select Class <select name="listclass" id="list_class>
&lt;?php
foreach ($Class_name as $class){
echo "<option value=".$class.">".$class."</option>";
}
?&gt;
</select>

Select Student <Select name="list_student">
&lt;?php
foreach ($Std_name as $student){
if ( echo "[removed]getValue();[removed]" == $class) {
echo "<option value=".$student.">".$student."</option>";
}
}
?&gt;
</Select>

**************************************888

When I print the value of the variable inside the for loop, it prints as 'Array'.

it would be of a great help if some one can guide me through the right path.

Regards,
Delvin
#2

[eluser]smilie[/eluser]
Hi,

Just put somewhere before the drop down list (in view file):
Code:
&lt;?php
      foreach ($Class_name as $class){
          print_r($class);
      }
  ?&gt;

and look what is says. Then, adjust the code accordingly.

Cheers,
Smilie

PS. please use code brackets for readability...
#3

[eluser]Narkboy[/eluser]
Code:
$query = $this->db->query(‘SELECT Class_ID, First_Name, Middle_Name, Last_Name from student’);
  foreach ($query->result_array() as $row){
          $temp = $row[‘Class_Name’];
          $temp1 = $row[‘First_Name’].$row[‘Middle_Name’].$row[‘Last_Name’];
      }
$data[‘Class_name’]=$temp;
$data[‘Std_name’]=$temp1;
$this->load->view(‘m_student/classlist_stdview.php’,$data);

You are setting the $data vars outside the foreach loop - they will be replaced by each loop, and you'll end up with only the data from the last $row.

Try:
Code:
$query = $this->db->query(‘SELECT Class_ID, First_Name, Middle_Name, Last_Name from student’);

$names = array(); // Declare an empty arry to recieve data in the loop

foreach ($query->result_array() as $row){
    $temp = $row[‘Class_Name’];
    $temp1 = $row[‘First_Name’].$row[‘Middle_Name’].$row[‘Last_Name’];

    $names[] = array(
        'Class_name' => $temp,
        'Std_name'   => $temp1
    );
}

$data['names'] = $names;

$this->load->view(‘m_student/classlist_stdview.php’,$data);

This will give you something like:
Code:
$names[0] = array('Class_name' => 'ClassA' , 'Std_name' => 'Bob B Bobby');
$names[1] = array('Class_name' => 'ClassB' , 'Std_name' => 'A N Other');

Also - your concat for the names includes no spaces so unless they are in the db, you'll get 'BobBBobby' and 'ANOther' in the examples above.

Lastly, your db fields are 'First_Name' but your $data vars are 'First_name'. Try to use EITHER underscore OR caps. 'FirstName' (camelcaps), or 'first_name' - cuts down on typos!
#4

[eluser]Crimp[/eluser]
Is Bob B Bobby related to this boy?

http://xkcd.com/327/
#5

[eluser]smilie[/eluser]
@Crimp: LOL :-)))

Cheers,
Smilie
#6

[eluser]Delvin[/eluser]
Thanks Similie, I have followed through your code, I didnt copy the $names array to $data, instead I passed the same array $names to the view file, but I am unable to view any data passed from controller in view file.

In controller I tried to print the data and it was successful. code below;
Code:
//copying the data into an array

foreach ($query->result_array() as $row){
        $temp = $row['Class_ID'];
        $temp1 = $row['First_Name'].$row['Middle_Name'].$row['Last_Name'];

        $names[] = array([code]
'Class_name' => $temp,
'Std_name' => $temp1
);
}

foreach ($names as $class){
echo $class['Std_name'];
}
$this->load->view('m_student/classlist_stdview.php',$names);
[/code]

In view file I tried to print the values with below code:

Code:
foreach ($Class_name as $class){
    echo $Class;
}

Not sure where I have gone wrong.

PS: Thank you for correcting me on the coding structure.
#7

[eluser]Delvin[/eluser]
Hi There,

I found the problem. It was not in the way I pass the data, but it was in the code I wrote to display the details.

thank You.
#8

[eluser]smilie[/eluser]
Hi Delvin,

Glad you solved it. I knew problem was in the view (display) - but didn't know how your array looked like :-)

Cheers,
Smilie
#9

[eluser]Delvin[/eluser]
Thank You So much Similie, Definitely I will be requiring more help as I progress with my project.
Will post more of my doubts.. :-P




Theme © iAndrew 2016 - Forum software by © MyBB