gpt4 book ai didi

php - 将数据库的输出更改为可读输出

转载 作者:行者123 更新时间:2023-11-30 00:41:03 25 4
gpt4 key购买 nike

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

数据存储方式如下:

  • 共有三个选项["1","0","0"]["0","1","0"]["0","0","1"] – 即 [是][否][也许]
  • 然后它还可以存储文本,例如[“可读文本”]

我希望能够将输出从 ["0","0","1"] 更改为 Maybe["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)
);
}

最佳答案

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

如果是这样,您应该改为实现 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

关于php - 将数据库的输出更改为可读输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21768807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com