Red2034 Posted February 7, 2014 Share Posted February 7, 2014 on one page I have a btn for each row of data (in my case I am showing all the courses available from 'class_master'): <td width="50" align="center"> <?php echo form_open('student/classes/enroll' , array('class_master' => 'form-horizontal validatable','target'=>'_top'));?> <div class="form-actions"> <button type="submit" class="btn btn-gray"><?php echo get_phrase('enroll');?></button> </div> </form> </td>... and on the controller page I am firing the case for the 'enroll' call listed above. Now what I would like to happen is this: I want some of the info from that row selected to INSET into the 'class' table, and here is the code on the controller page: function classes($param1 = '', $param2 = '') { if ($param1 == 'enroll') { $classes = $this->db->get_where('class_master', array( 'title'))->row(); $data['title'] = $classes['title']; $this->db->insert('class', $data); redirect(base_url() . 'index.php?student/classes/', 'refresh'); } if I replace the bold code above with "any text"; it works fine, but the way it is I get this error when I click the btn... Error Number: 1054 Unknown column '0' in 'where clause' SELECT * FROM (`class_master`) WHERE `0` = 'title' Filename: /public_html/Site/controllers/student.php Line Number: 97 does anybody have any suggestions about what may be wrong Quote Link to comment https://forums.phpfreaks.com/topic/286029-newby-question-copy-row-from-one-table-to-another/ Share on other sites More sharing options...
.josh Posted February 8, 2014 Share Posted February 8, 2014 Your classes function doesn't know anything about $classes; it's not within its scope. You need to pass it as an argument to the function. Quote Link to comment https://forums.phpfreaks.com/topic/286029-newby-question-copy-row-from-one-table-to-another/#findComment-1468131 Share on other sites More sharing options...
Red2034 Posted February 8, 2014 Author Share Posted February 8, 2014 do you mean I need to add something like this outside my classes function on the controller page: <?php $classes = $this->db->get('class_master')->result_array(); foreach($classes as $row):?> unsure how to define $classes in PHP i guess? Quote Link to comment https://forums.phpfreaks.com/topic/286029-newby-question-copy-row-from-one-table-to-another/#findComment-1468133 Share on other sites More sharing options...
.josh Posted February 8, 2014 Share Posted February 8, 2014 okay, somewhere in your code you are calling classes(). And inside classes() you have this: $data['title'] = $classes['title']; The problem is that your classes() function doesn't have access to $classes['title']. So you need to expose it to your function. Normally you do this by passing it as a parameter to the function, though there are other ways to do it, depending on when/where you are actually setting $classes['title'] vs. when/where you are calling classes(). For example: $foo = 'bar'; function something() { echo $foo; } something(); This won't echo anything, because something() doesn't have access to $foo. If your error reporting is turned on and set to appropriate levels, you will actually get a notice or warning saying that you are attempting to use a variable that doesn't exist. So instead, you would do something like this: $foo = 'bar'; function something($foo) { echo $foo; } something($foo); Now 'bar' will echo, because you are passing it to the function. Alternatively, you can do this: $foo = 'bar'; function something() { global $foo; echo $foo; } something(); That declares that you want the function to look for $foo in the global context. So it will find it there and reference that. However, it is a bad idea to do it this way. This is considered scope creep, and can lead to unexpected behavior. Another alternative is if everything including the function is actually within a class. Then you could do something like this: class something { var $foo; function doSomething() { echo $this->foo; } } $blah = new something(); $blah->foo = 'bar'; $blah->doSomething(); I show this example because this is essentially what you seem to be doing already with $this->db in your function. Though in practice, you should have getter and setter methods for it instead of directly accessing the value, something like this: class something { var $foo; function get_foo() { return $this->foo; } function set_foo($foo) { $this->foo = $foo; } function doSomething() { echo $this->get_foo(); } } $blah = new something(); $blah->set_foo('bar'); $blah->doSomething(); Quote Link to comment https://forums.phpfreaks.com/topic/286029-newby-question-copy-row-from-one-table-to-another/#findComment-1468135 Share on other sites More sharing options...
Red2034 Posted February 8, 2014 Author Share Posted February 8, 2014 ah! $foo = 'bar'; is not global - this is my issue - let me now play around with these ideas... Quote Link to comment https://forums.phpfreaks.com/topic/286029-newby-question-copy-row-from-one-table-to-another/#findComment-1468140 Share on other sites More sharing options...
Red2034 Posted February 8, 2014 Author Share Posted February 8, 2014 but honestly I thought I was declaring $classes with this: $classes = $this->db->get_where('class_master', array( 'title'))->row(); Quote Link to comment https://forums.phpfreaks.com/topic/286029-newby-question-copy-row-from-one-table-to-another/#findComment-1468143 Share on other sites More sharing options...
.josh Posted February 8, 2014 Share Posted February 8, 2014 well, you are assigning something to $classes with that. The problem is your classes() function doesn't know anything about it. So you have to expose it to your classes function. Quote Link to comment https://forums.phpfreaks.com/topic/286029-newby-question-copy-row-from-one-table-to-another/#findComment-1468146 Share on other sites More sharing options...
Red2034 Posted February 10, 2014 Author Share Posted February 10, 2014 unfortunately, I was not able to pass along the para. into that classes function... But I did realize that those para already exists on the first page that contains the btn - so I just added the code to the btn itself... <?php $classesMaster = $this->db->get('class_master')->result_array(); foreach($classesMaster as $row):?> <tr><td width="50" align="center"> <button type="submit" class="btn btn-gray"> <?php echo get_phrase('enroll'); $classes = $this->db->get('class_master', ('title'))->row(); $data['title'] = $row['title']; $this->db->insert('class', $data);?></button> so this is adding the title to the 'class' table correctly, but it adds all titles that exists in the 'class_master' table everytime I refresh the page?!?!? and I thought that I would call '$classes' where I have $row listed above but it throws an error. "DataTables warning (table id = 'DataTables_Table_1'): Requested unknown parameter '1' from the data source for row0" any suggestion with this new approach? Quote Link to comment https://forums.phpfreaks.com/topic/286029-newby-question-copy-row-from-one-table-to-another/#findComment-1468407 Share on other sites More sharing options...
Red2034 Posted February 11, 2014 Author Share Posted February 11, 2014 I am passing a single paramater in, but still trying to figure out how to pass the array in:btn display page code: <?php $classesMaster=$this->db->get('class_master')->result_array(); foreach($classesMaster as $row):?> <tr><td width="50" align="center"> <?php echo form_open('student/classes/enroll' , array($classesMaster));?> <div class="form-actions"> <button type="submit" class="btn btn-gray"> <?php echo get_phrase('enroll');?></button></div></form> controller page code: function classes($param1 = '', $param2 = '', $classesMaster) { if ($param1 == 'enroll') { $data['title'] = $classesMaster['title']; $data['status'] = 'not attempted'; $data['enroll_date'] = date("Y-m-d"); $this->db->insert('class', $data); redirect(base_url() . 'index.php?student/classes/', 'refresh'); } title is still coming up NULL... I get what you are saying about passing the value of $classesMaster into this controller page function (I DO) but I just dont know php well enough to know how to pass and parse the array... I started this project by grabbing some code from several places online and have been piecing them together and for the most part I have made it work but this is the only thing that is not...can you help me a bit more (anyone)? Quote Link to comment https://forums.phpfreaks.com/topic/286029-newby-question-copy-row-from-one-table-to-another/#findComment-1468530 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.