0

I have the following code and I don't know what's wrong. Can someone please help me? I think the error is in the array, but I don't know exactly what the error is.

This is my complete code i use Codeigniter framework.

I want to do get value from text box that exist in html table and add it to the database table, and i want to use ajax for submit value to database.

if there any idea jquery code better than mine please tell me and tell me what i should write in insert_table function.

Note : My experience in this subject ZERO and this is my first project.

This is my controller code:

 <?php

class Main extends CI_Controller {

    function index($some) 
    {
        $this->load->model('main_model');
        switch ($some) {
            case 'class':
                $data['fields'] = array(
                    'ClassID' => 'class id',
                    'ClassNum' => 'class name',
                    'ClassSize' => 'class size',
                    'Floor' => 'floor'
                );
                $data['tables'] = $this->main_model->getClass();
                break;
            case 'group':
                $data['fields'] = array(
                    'GroupID' => 'id',
                    'GroupNum' => 'group name',
                    'SectionNum' => 'section',
                    'YearNum' => 'year',
                    'ProfNameAra' => 'proffetion name'
                );
                $data['tables'] = $this->main_model->getGroupSection();
                break;
            case 'hole':
                $data['fields'] = array(
                    'HoleID' => 'id',
                    'HoleNum' => 'hole name',
                    'HoleSize' => 'hole size',
                    'Floor' => 'floor'
                );
                $data['tables'] = $this->main_model->getHole();
                break;
            case 'lab':
                $data['fields'] = array(
                    'LabID' => 'id',
                    'LabName' => 'hole name',
                    'LabSize' => 'hole size',
                    'Floor' => 'floor'
                );
                $data['tables'] = $this->main_model->getLab();
                break;
            case 'lecture':
                $data['fields'] = array(
                    'LectureID' => 'id',
                    'SubjectAra' => 'subject name',
                    'Is_Lecture' => 'yes/no',
                    'YearNum' => 'year',
                    'ProfNameAra' => 'proffission',
                    'SubjectSemester' => 'semester'
                );
                $data['tables'] = $this->main_model->getLectureSubject();
                break;
            case 'prof':
                $data['fields'] = array(
                    'ProfID' => 'id',
                    'ProfNameAra' => 'prof name'
                );
                $data['tables'] = $this->main_model->getProf();
                break;
            case 'section':
                $data['fields'] = array(
                    'SectionID' => 'id',
                    'SectionNum' => 'section name',
                    'YearNum' => 'year',
                    'ProfNameAra' => 'proffission'
                );
                $data['tables'] = $this->main_model->getSection();
                break;
            case 'subgroup':
                $data['fields'] = array(
                    'SubgroupID' => 'id',
                    'SubgroupNum' => 'section name',
                    'GroupNum' => 'group name',
                    'SectionNum' => 'section name'
                );
                $data['tables'] = $this->main_model->getSubgroup();
                break;
            case 'subject':
                $data['fields'] = array(
                    'SubjectID' => 'id',
                    'SubjectAra' => 'subject name',
                    'Is_Lecture' => ' yes/no',
                    'YearNum' => 'year',
                    'ProfNameAra' => 'proffission'
                );
                $data['tables'] = $this->main_model->getSubject();
                break;
            case 'teacher':
                $data['fields'] = array(
                    'TeacherID' => 'id',
                    'TeacherName' => 'teacher name'
                );
                $data['tables'] = $this->main_model->getTeacher();
                break;
            case 'timetable':
                $data['fields'] = array(
                    'TimetableID' => 'id',
                    'YearDate' => 'date',
                    'Semester' => 'semester',
                    'YearNum' => 'year',
                    'ProfNameAra' => 'proffission',
                    'SectionNum' => 'sectin name'
                );
                $data['tables'] = $this->main_model->getTimetable();
                break;
            case 'timetabledetail':
                $data['fields'] = array(
                    'DetailID' => 'id',
                    'Day' => 'day',
                    'Period' => 'period',
                    'YearDate' => 'year date',
                    'Semester' => 'semester',
                    'YearNum' => 'year',
                    'ProfNameAra' => 'proffission',
                    'SectionNum' => 'section name',
                    'GroupNum' => 'group name',
                    'SubgroupNum' => 'subgroup name',
                    'LabName' => 'lab name',
                    'HoleNum' => 'hole name',
                    'ClassNum' => 'class name',
                    'TeacherName' => 'teacher name',
                    'SubjectAra' => 'subject name'
                );
                $data['tables'] = $this->main_model->getTimetabledetail();
                break;
            case 'year':
                $data['fields'] = array(
                    'YearID' => 'id',
                    'YearNum' => 'year'
                );
                $data['tables'] = $this->main_model->getYear();
                break;
            case 'year_prof':
                $data['fields'] = array(
                    'YearNum' => 'year',
                    'ProfNameAra' => 'proffission'
                );
                $data['tables'] = $this->main_model->getYearProf();
                break;
        }
        $data['main_content'] = 'main';
        $this->load->view('includes/template', $data);
    }
    function insert_table()
    {
      // What i should write here i don't know
    }
}

and this is my model :

    <?php

class Main_model extends CI_Model {

    function getClass() {

        $query = $this->db->select('*')
                ->from('class')
                ->get();
        return $query->result();
    }

    function getGroup() {

        $query = $this->db->select('*')
                ->from('group')
                ->get();
        return $query->result();
    }

    function getGroupSection() {
        $q = $this->db->select('group.GroupID, group.GroupNum, section.SectionNum,year.YearNum,prof.ProfNameAra')
                ->from('group')
                ->join('section', 'group.Section_id = section.SectionID')
                ->join('year', 'year.YearID = section.Year_id')
                ->join('prof', 'section.Prof_id =  prof.ProfID');
        return $q->get()->result();
    }

    function getHole() {

        $query = $this->db->select('*')
                ->from('hole')
                ->get();
        return $query->result();
    }

    function getLab() {

        $query = $this->db->select('*')
                ->from('lab')
                ->get();
        return $query->result();
    }

    function getLecture() {

        $query = $this->db->select()
                ->from('lecture')
                ->get();
        return $query->result();
    }

    function getLectureSubject() {
        $q = $this->db->select('lecture.LectureID, subject.SubjectAra, subject.Is_Lecture, year.YearNum, prof.ProfNameAra, subject.SubjectSemester')
                ->from('lecture')
                ->join('subject', 'lecture.Subject_id = subject.SubjectID')
                ->join('prof', 'prof.ProfID = subject.Prof_id')
                ->join('year','year.YearID = subject.Year_id');

        return $q->get()->result();
    }

    function getProf() {

        $query = $this->db->select('*')
                ->from('prof')
                ->get();
        return $query->result();
    }

    function getSection() {

        $query = $this->db->select('section.SectionID, section.SectionNum, year.YearNum, prof.ProfNameAra')
                ->from('section')
                ->join('year', 'section.Year_id = year.YearID')
                ->join('prof', 'section.Prof_id = prof.ProfID')
                ->get();
        return $query->result();
    }

    function getSubgroup() {

        $query = $this->db->select('subgroup.SubgroupID, subgroup.SubgroupNum, group.GroupNum, section.SectionNum')
                ->from('subgroup')
                ->join('group', 'subgroup.Group_id = group.GroupID')
                ->join('section', 'group.Section_id = section.SectionID')
                ->get();
        return $query->result();
    }

    function getSubject() {

        $query = $this->db->select('subject.SubjectID,subject.SubjectAra,subject.SubjectSemester,subject.Is_Lecture,
            year.YearNum,prof.ProfNameAra')
                ->from('subject')
                ->join('year', 'year.YearID = subject.Year_id')
                ->join('prof', 'prof.ProfID = subject.Prof_id')
                ->get();
        return $query->result();
    }

    function getTeacher() {

        $query = $this->db->select('*')
                ->from('teacher')
                ->get();
        return $query->result();
    }

    function getTimetableDetail() {

        $query = $this->db->select('DetailID,Day,Period,
            timetable.TimetableID,timetable.YearDate,timetable.Semester,
            year.YearNum,
            prof.ProfNameAra,
            section.SectionNum,
            group.GroupNum,
            subgroup.SubgroupNum,
            lab.LabName,
            hole.HoleNum,
            class.ClassNum,
            teacher.TeacherName,
            subject.SubjectAra')
                ->from('timetabledetail')
                ->join('timetable','Timetable_id = TimetableID')
                ->join('year', 'timetable.Year_id = year.YearID')
                ->join('prof', 'prof.ProfID = timetable.Prof_id')
                ->join('section', 'section.SectionID = timetable.Section_id')
                ->join('group', 'GroupID = Group_id')
                ->join('subgroup','SubgroupID = Subgroup_id')
                ->join('lab','LabID = Lab_id')
                ->join('hole', 'HoleID =Hole_id')
                ->join('class','ClassID = Class_id')
                ->join('teacher','TeacherID = Teacher_id')
                ->join('subject','SubjectID = Subject_id')
                ->get();

        return $query->result();
    }

    function getTimetable() {

        $query = $this->db->select('timetable.TimetableID,timetable.YearDate,timetable.Semester,
            year.YearNum,
            prof.ProfNameAra,
            section.SectionNum')
                ->from('timetable')
                ->join('year', 'timetable.Year_id = year.YearID')
                ->join('prof', 'prof.ProfID = timetable.Prof_id')
                ->join('section', 'section.SectionID = timetable.Section_id')
                ->get();
        return $query->result();
    }

    function getYear() {

        $query = $this->db->select('*')
                ->from('year')
                ->get();
        return $query->result();
    }

    function getYearProf()
    {
        $q = $this->db->select('year.YearNum, prof.ProfNameAra')
                ->from('year_prof')
                ->join('year', 'year_prof.Year_id = year.YearID')
                ->join('prof', 'year_prof.Prof_id = prof.ProfID')
                ->get();

        return $q->result();
    }
}

and this is my view code :

<?php echo form_open('main') ?>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.easing-               sooper.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.sooperfish.js">        </script>
<script>
$(document).ready(function() {
    $('#add').click(function() {
        var str = '#';
        var arrayFromPHP = <?php echo json_encode($fields); ?>

        var array = new Array();
        $.each(arrayFromPHP, function(key) {
            var temp = str.concat(key);
            var temp1 = $(temp).val();
            var list = [key, temp1];
            array.push(list);
        });

        $.ajax({
        url: "<?php echo site_url('main/insert_table'); ?>",
        type: 'POST',
        data: array,
        success: function(msg){
            alert('You insert data into database !!!');
        }
    });

    });
});
</script>
<div id="main">  
  <div id="header_container">
        <div class="container">
            <!--Header-->
            <div class="4u">
                <h2>Andro-CH server page</h2>
            </div>

            <nav id="main-nav" class="8u">
                <ul>
                    <li><a href="class">Class</a></li>
                    <li><a href="group">Group</a></li>
                    <li><a href="hole">Hole</a></li>
                    <li><a href="lab">Lab</a></li>
                    <li><a href="prof">Prof</a></li>
                    <li><a href="section">Section</a></li>
                    <li><a href="subgroup">Subgroup</a></li>
                    <li><a href="subject">Subject</a></li>
                    <li><a href="teacher">Teacher</a></li>
                    <li><a href="timetable">Timetable</a></li>
                    <li><a href="timetabledetail">Timetable Detail</a></li>
                    <li><a href="year">Year</a></li>
                </ul>
            </nav>
        </div>
    </div>
<table>
    <thead>
        <?php foreach ($fields as $field_name => $field_display): ?>
        <th>
            <?php echo $field_display; ?>
        </th>
    <?php endforeach; ?>
    </thead>
    <tbody>
        <tr>
            <?php foreach ($fields as $field_name => $field_display): ?>
                <td>
                    <?php echo form_input($field_display, '', "id = $field_name"); ?>
                </td>
            <?php endforeach; ?>
        </tr>
        <?php foreach ($tables as $table): ?>
            <tr>
                <?php foreach ($fields as $field_name => $field_display): ?>
                    <td>
                    <?php echo $table->$field_name; ?>
                    </td>
                    <?php endforeach; ?>
            </tr> 
            <?php endforeach; ?>
    </tbody>
</table>
</div>
<div>
<?php echo form_button('add', 'Add', 'id = "add"'); ?>
</div>
<?php echo form_close(); ?>
Dylan
  • 3
  • 2
  • 1
    Look in the [JavaScript error console](http://www.creativebloq.com/javascript/javascript-debugging-beginners-3122820) and tell what errors you see there and which lines they point to. – JJJ May 25 '14 at 11:14
  • 2
    It would also be far more helpful to see the rendered code, rather than the PHP that generates it. – lonesomeday May 25 '14 at 11:19
  • Dynamically creating js code from php is never a great idea, I assume you are pressed for time... Have you considered loading the data from the server with something like `$.get`? – William George May 25 '14 at 11:21
  • did you check if you are actuall gettung value by json_encode – M.chaudhry May 25 '14 at 11:26

1 Answers1

0

You are trying to access not existing items inside myArray array. To add new items to array, you need to use .push() function, or if you want to use specific keys, you have to use Hash.

$(document).ready(function() {
    $('#add').click(function() {
        var arrayFromPHP = <?php echo json_encode($fields); ?>
        var myArray = [];
        var str = "#";
        //var i = 0; // this is not needed since push adds everytime to the end of array
        $.each(arrayFromPHP, function(key) {
            var temp = str.concat(key);
            var temp1 = $(temp).val();
            myArray.push(temp1);
            //i = i + 1; // this is not needed then
            alert(temp1);
        });
    });
    });