我尝试在CodeIgniter中创建一个动态变量。我有一个模型从数据库中获取数据并在视图中显示它。通常它会是这样的 $ data ['query'] = $ this-> mdl_store_accounts-> get('id'); 并使用$ query访问数据对象。
但这是我想在下面实现的目标。我在一个循环中传递变量(a到e)代替(查询)。
function manage()
{
$query = $this->mdl_store_accounts->get('id');
$sport = $query->row('sport');
$slots = $query->row('slots');
$_start = 1;
$_end = 4;
$q = 1;
for ($z='a'; $z <= 'e' ; $z++) {
if ($sport == $q) {
// echo $q; die();
$available_slot = $slots * ($sport - 1) ; //slots used and available slot calculation to get next range
$_start = $available_slot + 1 ; //slots used and available slot calculation to get next range
$_end = $available_slot + 3;
// echo $start . ' and ' . $end; die();
$data[$z] = $this->mdl_courses->_custom_query('SELECT * from courses WHERE id BETWEEN '.$_start.' AND '.$_end.' ORDER BY rand()');
} elseif($q == 1 and $q !== $sport) {
$_start = 1 ; //slots used and available slot calculation to get next range
$_end = 4;
$data[$z] = $this->mdl_courses->_custom_query('SELECT * from courses WHERE id BETWEEN '.$_start.' AND '.$_end.' ORDER BY rand()');
} elseif($q == 2 and $q !== $sport) {
$_start = 5 ; //slots used and available slot calculation to get next range
$_end = 9;
$data[$z] = $this->mdl_courses->_custom_query('SELECT * from courses WHERE id BETWEEN '.$_start.' AND '.$_end.' ORDER BY rand()');
}
$q++;
} }
在我的视图中使用动态变量访问它,就像这样;
$query = "$";
${$query . 'a'} = $a;
foreach ($a->result() as $rows) {
$query++;
$a++;
我已经尝试了数组和这个$$ a。但没有工作。是收到错误 类CI_DB_mysqli_result的对象无法转换为字符串。任何帮助将不胜感激