![]() |
populating a dropdown dynamically - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: populating a dropdown dynamically (/showthread.php?tid=36461) |
populating a dropdown dynamically - El Forum - 12-02-2010 [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> <?php foreach ($Class_name as $class){ echo "<option value=".$class.">".$class."</option>"; } ?> </select> Select Student <Select name="list_student"> <?php foreach ($Std_name as $student){ if ( echo "[removed]getValue();[removed]" == $class) { echo "<option value=".$student.">".$student."</option>"; } } ?> </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 populating a dropdown dynamically - El Forum - 12-02-2010 [eluser]smilie[/eluser] Hi, Just put somewhere before the drop down list (in view file): Code: <?php and look what is says. Then, adjust the code accordingly. Cheers, Smilie PS. please use code brackets for readability... populating a dropdown dynamically - El Forum - 12-02-2010 [eluser]Narkboy[/eluser] Code: $query = $this->db->query(‘SELECT Class_ID, First_Name, Middle_Name, Last_Name from student’); 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’); This will give you something like: Code: $names[0] = array('Class_name' => 'ClassA' , 'Std_name' => 'Bob B Bobby'); 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! populating a dropdown dynamically - El Forum - 12-02-2010 [eluser]Crimp[/eluser] Is Bob B Bobby related to this boy? http://xkcd.com/327/ populating a dropdown dynamically - El Forum - 12-02-2010 [eluser]smilie[/eluser] @Crimp: LOL :-))) Cheers, Smilie populating a dropdown dynamically - El Forum - 12-03-2010 [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 '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){ Not sure where I have gone wrong. PS: Thank you for correcting me on the coding structure. populating a dropdown dynamically - El Forum - 12-03-2010 [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. populating a dropdown dynamically - El Forum - 12-03-2010 [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 populating a dropdown dynamically - El Forum - 12-03-2010 [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 |