0

I've got a home-grown LAMP app that uses the same HTML code for the "Modify record" and "New record" pages based on whether it is passed a blank record or a non-blank record. Currently, I'm creating the blank record by selecting a single record from the MySQL table and manually setting each field (by name) to NULL. There's got to be a better way to do this, but the search terms to find what I need are just too darn generic. I've looked at using something like mysql_fetch_array or get_class_vars, but I just can't get them to work. Here's what I'm trying to replace with something a bit less manual:

// Select a row, any row... (using CodeIgniter)
$q = $this->db->get($this->config->item('db'),1);
// For each ... um ... one ... row, add it to $data
foreach ($q->result() as $row) {
    $data[] = $row;
}
// set each **KNOWN** field to NULL
$data[0]->column1 = NULL;
$data[0]->column2 = NULL;
...

I know it's a dirty hack, but it works. What's the "right" way to do this?

5
  • 1
    why are you making a blank row? Commented Sep 22, 2013 at 3:13
  • It's not clear what you are trying to achieve. What's the expected result of this code fragment and how to you intend to use it? Why don't you just create a stdClass object? Commented Sep 22, 2013 at 3:34
  • I'll try to clarify: I'm passing a record to an HTML form. If that record is non-blank, then the form assumes the user is editing an existing record. If that record is blank, then the form assumes the user is creating a new record. This allows me to have code like <?php echo $r->column1; ?> work for both scenarios instead of having two different batches of HTML code; one for "modify" and one for "new". This is all working fine. What I'm trying to avoid is having to manually create a new property or NULL out a new selected column every time I add a column to the database. Is that clearer? Commented Sep 22, 2013 at 4:01
  • Is the record number set manually or is it automaticly set via php/mysql? Commented Sep 22, 2013 at 9:19
  • Record number? I have an auto-incremented index column, but that gets set when the data is inserted into the table. Is that what you're asking about? Commented Sep 22, 2013 at 16:55

2 Answers 2

1

I believe you are doing exactly this:

$data[0] = (object) array_fill_keys($this->db->list_fields('your_table_name'), null);

Reference: CI->db->list_fields(), array_fill_keys(), type casting

Sign up to request clarification or add additional context in comments.

2 Comments

Ah ... I was looking for a general PHP solution and hadn't considered looking for a specific CodeIgniter solution. Thanks! For the sake of others who might stumble upon this question with the same question, is there a general PHP solution?
@JohnStraffin Well, general PHP solution will likely have to implement their own list_fields() of sorts. In case of MySQL it uses DESCRIBE query, then iterates through results. Definitely not a one-line solution.
0

If you're getting 1 row only, you'd better off using ->row() method instead of a resuls() & useless foreach loop

e.g:

$row = $this->db->select('*')->from('table')->get()->row();

getting to your problem now, you'll have to define the fields you're using something like this

$fields = array('id', 'name', '...etc');

then you'll do something like this to get empty if not available

foreach ( $fields as $f ) $data[$f] = isset($row->$f) ? $row->$f : NULL;

this will make all fields available in your $data, so you can pass it to a view.

3 Comments

That manual $fields array() is what I'm trying to do away with ... is there no way to use the existing structure of the retrieved $row to dynamically build the $fields array() without knowing the column names ahead of time (or having to edit the manual $fields array() every time the table is altered)?
Also, thanks for the row() method hint. I knew there had to be a better way to do that as well, but had simply re-purposed some existing code to start the function and, since it worked, never went back and cleaned it up. :-)
You can use the list_fields method if you don't like to do it manually , something like this $fields = $this->db->list_fields('table_name');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.