Jump to content

Newby question - Copy row from one table to another


Red2034

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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();
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.