0

I have the array $student[]

<?php
$student['id'] = "10402";
$student['hnumber'] = "H030502";
$student['name'] = "Larry Wayne";

print_r($student);
?>

It prints out:

Array ( [id] => 10402 [hnumber] => H030502 [name] => Larry Wayne )

What I want to accomplish is storing values into an array, that will then be inserted into a database table.

So the insert statement would be:

$q = "insert into table (id, hnumber, name) VALUES ('10401', 'H030502', 'Larry Wayne')";

I want to use an array to store all the values into it, labeling each value by their table field name, because it will be about 25 fields I will be inserting data into.

If there is a better way of accomplishing that, I am all ears.

Thanks in advance.

Brad
  • 11,181
  • 42
  • 112
  • 178
  • It sounds like you're looking for an ORM for PHP... or at least something like one. Might want to check out the [Good PHP ORM Library?](http://stackoverflow.com/q/108699/15880) question. – Powerlord Nov 08 '10 at 19:03
  • i would suggest creating a class to store them all in a single object instead of a general array; and probably using a stored procedure instead of a querystring, but that's just me. – Adam Nov 08 '10 at 19:03
  • A table *is* an array. You could store a serialized object as a BLOB, but it'd be a waste in a database because you'd have to pull the BLOB out and then unserialize it before you could read the contents -- you'd never be able to search for a student with a particular name, etc. – OMG Ponies Nov 08 '10 at 19:04

2 Answers2

0

Assuming you are building an array of students from another source, how about something like this?

// data from an external source
$students = array(
    // student 1
    array(
        123,
        'h123',
        'John Smith',
    ),
    // student 2
    array(
        456,
        'h456',
        'Jane Smith',
    ),
    // ... and so on        
);

$values = array();
foreach ( $students as $student )
{
    // @todo, make sure to sanitize values!!!
    $values[] = sprintf('(%s)', implode(', ', $student));
}
// build query
$query = 'INSERT INTO `table` (`id`, `hnumber`, `name`) VALUES '.implode(', ', $values);

Please note, above code is "pseudo" or an idea if you want. Make sure to sanitize the values :)

EDIT: One more thing. Above code is good if you want a simple fix, preferably for some sort of simple data import. Better way is to create a class Student handling all this logic.

David Kuridža
  • 6,520
  • 5
  • 24
  • 25
0

I wanted to simply comment on David's example as it's pretty much the same thing I was going to suggest, but I can't add code to comments, unfortunately. One thing David forgot from your original question was that you wanted to use the table fields as part of your array - in this example, as the key fields. In the foreach, you can split the array in to key/value pairs, and then use them later on in your code itself.

<?php
    $student['id'] = "10402";
    $student['hnumber'] = "H030502";
    $student['name'] = "Larry Wayne";
    $queryFields = array();
    $queryValues = array();
    $queryString = '';

    foreach($student as $key => $value){
        $queryFields[] = '`'.$key.'`';
        $queryValues[] = $value;
    }

    $queryString = 'INSERT INTO `table` ('.implode(',', $queryFields).') VALUES ('.implode(',', $queryValues).')';
    //run_query($queryString)
?>

Since I'm not 100% familiar with CodeIgniter, it's very possible that there might be a way to map arrays and/or objects to some sort of ActiveRecord implementation. However, since you're just looking for a way to generate a query string itself, this would do the trick.

BrendonKoz
  • 434
  • 1
  • 5
  • 18
  • 1
    CodeIgniter does in fact have an ActiveRecord implementation "of sorts". From your array syntax, it's actually quite simple. http://codeigniter.com/user_guide/database/active_record.html#insert $this->db->insert('mytable', $student); – BrendonKoz Nov 08 '10 at 20:45