0

I wrote a simple function that takes one of my database tables and outputs a full JSON array.

function tableJSON ($table, $orderby) {
    $array = array();
    $sql = Nemesis::select("*", $table, NULL, $orderby);
    if ($sql) {
        if ($sql->num_rows > 0) {
            while ($row = $sql->fetch_assoc()) {
                // push row values to array
                array_push($array, $row);
            }
            return json_encode($array);
        } else {
            echo 'Query returned empty';
        }
    } else {
        echo 'Query error';
    }
}

From this array a table is generated using a method outlined here. Then I apply table sorter to the table. My question is that, currently, this script outputs all rows and columns. Example:

[{"id":"109225488","project_name":"One on One Interview the Dean of RSM","project_bold":"Interview","project_content":"Interview with the Dean of Erasmus University Rotterdam School of Management. Interviewer: Joost Kammermans.","project_image_1":"\/images\/uploads\/projects\/109225488\/m_109225488_1.jpg","project_image_2":"","project_image_3":"","project_image_4":"","youtube_link":"http:\/\/www.youtube.com\/watch?v=9rsR3FcLAxI","published":"1","created":"2013-05-29 14:07:49","created_by":"1","last_modified":"2013-07-22 19:43:15","last_modified_by":"1"}

How would I exclude this script from outputting an array of excluded columns?

For example:

$excluded = array('created_by', 'project_image_1');

I have tried array_diff, with no luck.

2 Answers 2

2
$sql = Nemesis::select("*", $table, NULL, $orderby);

Change the * to a list of only the columns you want output and you won't have to worry about fussing with the array on the backend.

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

2 Comments

interesting point, but in my own interest i am curious. ill upvote in any case
I agree - dealing with only the columns you need is your best bet - you don't have to sift through the data in a strange way.
0

I would suggest DevIshOne's answer, but...

function tableJSON ($table, $orderby, $excluded = array()) {
    $array = array();
    $sql = Nemesis::select("*", $table, NULL, $orderby);
    if ($sql) {
        if ($sql->num_rows > 0) {
            while ($row = $sql->fetch_assoc()) {
                $newrow = array();

                foreach($row as $col => $val)
                    if(!in_array($col, $excluded))
                        $newrow[$col] = $val;
                // push row values to array
                array_push($array, $newrow);
            }
            return json_encode($array);
        } else {
            echo 'Query returned empty';
        }
    } else {
        echo 'Query error';
    }
}

Comments

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.