如何将mysql_fetch_array转换为CodeIgniter

I have a problem,im trying to convert php standard to CodeIgniter, But I dont know how to convert ths code, please help, and thanks a lot.

    <?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("ardefa");
    $borneo=mysql_query("select* from borneo");

    while($row=mysql_fetch_array($borneo))
    {
       ?>
         <a href="#"><li><img src="
         <?php 
         $page = isset($_GET['page']) ? ($_GET['page']):"";

         if ($page =='borneo')
         {
            echo $row["img"];
         }
         ?>">
         </li></a>
    <?php
    }
    ?>
douliang2087
douliang2087 感谢您的评论,我知道结构,关于控制器,模型和视图,如何在CI中使用wihile和mysql_fetch_array?
2 年多之前 回复
duanpanyang1962
duanpanyang1962 $这->DB->选择('');$data=$this->db->get('borneo')->result_array();//所有行$this->db->select('');$data=$this->db->get('borneo')->result_array();//单行
2 年多之前 回复
dongluanban3536
dongluanban3536 你需要首先学习实际的框架本身,并且它使用控制器,模型和视图来解决问题。为什么不首先尝试框架,开始构建它,我确定有很多资源在那里,它不是新的东西反正
2 年多之前 回复

2个回答

Hope this will help you :

You don’t need to use db_select if you have single database, if multiple database you only need to use a different database on the same connection. You can switch to a different database when you need to using this $this->db->db_select('ardefa');

You can do like this :

//$this->db->db_select('ardefa');
$this->db->select('*');
$this->db->from('borneo');
$query = $this->db->get();
if ($query->num_rows() > 0 )
{
   /*for multiple array*/

   $result = $query->result_array();
   /*print here to see the result
   print_r($result);
   */
}

Use $result like this :

foreach($result as $row)
{
    echo $row;
}

Or can also do it like this :

//$this->db->db_select('ardefa');
$query = $this->db->get('borneo');
if ($query->num_rows() > 0 )
{
   /*for multiple array*/

   $result = $query->result_array();
   /*for single array
   $row = $query->row_array();
   */
}

For more : https://www.codeigniter.com/user_guide/database/

Try this hope it will help you

MODEL

public function your_function(){
    return $this->db->get('borneo')->reslut_array();
}

CONTROLLER

<?php

$this->load->model('model-name');
$data = $this->model-name->model_function();
foreach($data as $row){
    if(isset($_GET['page']) && $_GET['page'] == "borneo"){ ?>
        <a href="#"><li><img src="<?php echo $row['img']?>" /></li></a>
<?php } } ?>
doutuo3575
doutuo3575 用这段代码解决了你的问题?
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问