将输出从数据库更改为可读输出

时间:2014-02-14 00:51:27

标签: php mysql

我有一些代码可以从数据库中获取一些表。

数据存储如下:

  • 有三个选项["1","0","0"]["0","1","0"]["0","0","1"] - 即[Yes][No][Maybe]
  • 然后它也可以存储文本,例如["Readable text"]

我希望能够将["0","0","1"]的输出更改为Maybe["Readable text"]更改为Readable text。我不知道该怎么做。

以下是我正在使用的代码:

<?php
global $wpdb;
$current_user = wp_get_current_user();
$result = $wpdb->get_results( "
SELECT stats.*
  FROM wp_wp_pro_quiz_statistic stats
       JOIN wp_wp_pro_quiz_statistic_ref refs on stats.statistic_ref_id = refs.statistic_ref_id
WHERE refs.user_id= $current_user->ID  && refs.quiz_id= 5");
foreach($result as $row) {
echo "<tr><td><b>$row->answer_data</b></td></tr>";

我希望有人可以帮助我。

这是他们使用的东西我不知道如何利用它我想要的东西。

public function statisticSave($statisticRefModel, $statisticModel) {
                $values = array();

                $refId = null;
                $isOld = false;

                if($refId === null) {

                        $refData = array(
                                        'quiz_id' => $statisticRefModel->getQuizId(),
                                        'user_id' => $statisticRefModel->getUserId(),
                                        'create_time' => $statisticRefModel->getCreateTime(),
                                        'is_old' => (int)$isOld
                        );

                        $refFormat = array('%d', '%d', '%d', '%d');

                        if($statisticRefModel->getFormData() !== null && is_array($statisticRefModel->getFormData())) {
                                $refData['form_data'] = @json_encode($statisticRefModel->getFormData());
                                $refFormat[] = '%s';
                        }

                        $this->_wpdb->insert($this->_tableStatisticRef, $refData, $refFormat);

                        $refId = $this->_wpdb->insert_id;
                }

                foreach($statisticModel as $d) {
                        $answerData = $d->getAnswerData() === null ? 'NULL' : $this->_wpdb->prepare('%s', json_encode($d->getAnswerData()));

                        $values[] = '( '.implode(', ', array(
                                        'statistic_ref_id' => $refId,
                                        'question_id' => $d->getQuestionId(),
                                        'correct_count' => $d->getCorrectCount(),
                                        'incorrect_count' => $d->getIncorrectCount(),
                                        'hint_count' => $d->getHintCount(),
                                        'points' => $d->getPoints(),
                                        'question_time' => $d->getQuestionTime(),
                                        'answer_data' => $answerData
                        )).' )';
                }

                $this->_wpdb->query(
                                'INSERT INTO
                                '.$this->_tableStatistic.' (
                                        statistic_ref_id, question_id, correct_count, incorrect_count, hint_count, points, question_time, answer_data
                                )
                        VALUES
                                '.implode(', ', $values)
                );
        }

1 个答案:

答案 0 :(得分:0)

您是否能够更改数据的存储方式?

如果是这样,你应该改为实施JSON http://nz2.php.net/manual/en/function.json-encode.php | http://nz2.php.net/manual/en/function.json-decode.php

示例:

$a = array(0,0,1); // Maybe
//$a = array("Readable Text"); // Readable Text

$a = json_encode($a); // ["0","0","1"]

// Display json as text
$b = json_decode($a, true); // array

if($b[0] == 1){ echo 'Yes'; }
if($b[1] == 1){ echo 'No'; }
if($b[2] == 1){ echo 'Maybe'; }
if(count($b) == 1){ echo 'Readable Text'; } // count items in array