Welcome Guest, Not a member yet? Register   Sign In
data from db to table
#1

[eluser]cesarius[/eluser]
Hello!
Im a newbie in ci and trying to do my first app...
Im looking for a way to show some records from database in a table, ut i can not get how to do this... verithing that ive tryed shows me only the last ecord and thats all/// here is my code - maybe somebody could help me?

Controller
Code:
<?PHP
class Base extends Controller {


    function base(){
        parent::Controller();
        $this->load->library('session');
        $this->load->library('basecl');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->database();
    }
    
    
    function index()
    {
        return $this->mainpage();
    }
    function mainpage(){
        $sql = 'SELECT zid, zname,zdate, zstatus,zchangedate FROM z_mainbase where zauthor="www" order by zid';
        $query = $this->db->query($sql);
        foreach ($query->result_array() as $row)
        {
        $row['zid'];
        $row['zname'];
        $row['zdate'];
        $row['zstatus'] = $this->basecl->statustype($row['zstatus']);
        $row['zchangedate'];
        }
        
        $this->load->view('baseview', $row);
        
        
    }
    
}
?>

ibrary
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Basecl {

    var $object;
    
    function Basecl(){
    $this->object =& get_instance();
    $this->object->load->database();
    }
    
    
     function statustype($statusidname){
    $sql = 'Select zstname from z_status where zstatusid= '.$statusidname.'';
    $query = $this->object->db->query($sql);
    if ($query->num_rows() == 1){
    $row = $query->row();
    return $row->zstname;
    } else {
    return 'error';
    }
    }

}
?>

and view
Code:
<html>
<table width = 100% celpadding = 0 cellspasing = 0>
<tr>
<td align = center text-align = center><h1>Система подачи заявок в отдел ИТ.</h1><td>
</tr>
<tr>
<td><h2>Поданные заявки:</h2></td>
</tr>
</tr>
<tr>
<td>
<table width = 100% cellpadding = 0 cellspacing = 0 style="border:2px solid #c0c0c0;background:#F5F4EA">
<tr>
<td width = 70px border = 0 style="border-right:1px solid #c0c0c0;border-bottom:2px solid grey;background:blue"><b>ID</b></td>
<td border = 0 style="border-right:1px solid #c0c0c0;border-bottom:2px solid grey;background:blue"><b>Краткое описание</b></td>
<td width = 100px border = 0 style="border-right:1px solid #c0c0c0;border-bottom:2px solid grey;background:blue"><b>Добавлено</b></td>
<td width = 80px border = 0 style="border-right:1px solid #c0c0c0;border-bottom:2px solid grey;background:blue"><b>Статус</b></td>
<td width = 100px border = 0 style="border-right:1px solid #c0c0c0;border-bottom:2px solid grey;background:blue"><b>Изменено</b></td>
<td width = 60px style="border-bottom:2px solid grey;background:blue">&nbsp;</td>
</tr>
<tr>
<td width = 70px border = 0 style="border-bottom:1px solid grey;border-right:1px solid #c0c0c0">&lt;?= $zid ?&gt;</td>
<td border = 0 style="border-bottom:1px solid grey;border-right:1px solid #c0c0c0">&lt;?= $zname ?&gt;</td>
<td width = 100px border = 0 style="border-bottom:1px solid grey;border-right:1px solid #c0c0c0"><font size=1>&lt;?= $zdate ?&gt;</font></td>
<td width = 80px border = 0 style="border-bottom:1px solid grey;border-right:1px solid #c0c0c0">&lt;?= $zstatus?&gt;</td>
<td width = 100px border = 0 style="border-bottom:1px solid grey;border-right:1px solid #c0c0c0"><font size=1>&lt;?= $zchangedate ?&gt;</font></td>
<td width = 60px style="border-bottom:1px solid grey;">&nbsp;</td>
</tr>
</table>
</td>
</tr>
</table>
&lt;/html&gt;

Thanks for you help.

P.s: Sorry for my bad english - im russian...
#2

[eluser]gtech[/eluser]
Hello it looks like you are overwriting your array in the loop, thats why you only see the last result.

Code:
function mainpage(){
  $sql = 'SELECT zid, zname,zdate, zstatus,zchangedate FROM z_mainbase where zauthor="www" order by zid';
  $query = $this->db->query($sql);

  //
  
  $data['vars'] = array();
  foreach ($query->result_array() as $row)
  {
    // *** you were overwriting here [] adds an element to an array to avoid having
    // *** to use a counter
    $data['vars'][] = array('zid'        => $row['zid'],
                           'zname'       => $row['zname'],
                           'zdate'       => $row['zdate'],
                           'zstatus'     => $this->basecl->statustype($row['zstatus']),
                           'zchangedate' => $row['zchangedate']);
  }
        
  $this->load->view('baseview', $data);      
}

then in the view:
Code:
&lt;?php foreach($vars as $row):?&gt;
  &lt;?=$row['zid']?&gt;<br>
  &lt;?=$row['zname']?&gt;<br>
  &lt;?=$row['zdate']?&gt;<br>
  &lt;?=$row['zstatus']?&gt;<br>
  &lt;?=$row['zchangedate']?&gt;<br>
  <hr>
&lt;?php endforeach;?&gt;


The code is untested but it looks like a solution to your problem (hope it helps).
#3

[eluser]xwero[/eluser]
[quote author="cesarius" date="1191460293"]
Controller
Code:
&lt;?PHP
class Base extends Controller {


    function base(){
        parent::Controller();
        $this->load->library('session');
        $this->load->library('basecl');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->database();
    }
    
    
    function index()
    {
        return $this->mainpage();
    }
    function mainpage(){
        $sql = 'SELECT zid, zname,zdate, zstatus,zchangedate FROM z_mainbase where zauthor="www" order by zid';
        $query = $this->db->query($sql);
        foreach ($query->result_array() as $row)
        {
        $row['zid'];
        $row['zname'];
        $row['zdate'];
        $row['zstatus'] = $this->basecl->statustype($row['zstatus']);
        $row['zchangedate'];
        }
        
        $this->load->view('baseview', $row);
        
        
    }
    
}
?&gt;
[/quote]
The problem is in the foreach you should do something like
Code:
$rows = array();
foreach ($query->result_array() as $row)
{
        $temparr = array();
        $temparr['zid'] = $row['zid'];
        $temparr['zname'] = $row['zname'];
        $temparr['zdate'] = $row['zdate'];
        $temparr['zstatus'] = $this->basecl->statustype($row['zstatus']);
        $temparr['zchangedate'] = $row['zchangedate'];
        $rows[] = $temparr;
}
$this->load->view('baseview', $rows);
#4

[eluser]xwero[/eluser]
gtech nice solution, 2 lines less than mine Smile
#5

[eluser]gtech[/eluser]
Thanks, must of been tapping the solutions out at the same time Smile
#6

[eluser]cesarius[/eluser]
sorry? could not write this earlier... but
thanks for your help)))
#7

[eluser]coffey[/eluser]
As a not dis-similar problem to that above I thought I would just ask if anyone could tell me why the code below isn't working. This is a library item that is used in multiple places throughout my app. Like the cesarius above I did get it to work but only showing my last option in this case.

Code:
$sectionTitle = $this->obj->lang->line('sections_title');
        
        $sql = "select id, section from sections";
        
        
        $query = $this->obj->db->query($sql);
        
        
        $data['vars'] = array();
        foreach ($query->result_array() as $row)
        {
            
                 $data['vars'][] = array($row['id'] => $row['section']);
        }
        
        $dropdown = form_dropdown('section_id', $data, $sectionTitle);
        
        return $dropdown;

Cheers
Mick
#8

[eluser]coffey[/eluser]
Gave up and added new dropdown function

Code:
function pform_dropdown($name = '', $options = array(), $selected = '', $extra = '')
{
    if ($extra != '') $extra = ' '.$extra;
        
    $form = '<select name="'.$name.'"'.$extra.">\n";
    
    foreach ($options as $opt)
    {
        $option = explode('|', $opt);
        $key = (string) $option[0];
        $val = (string) $option[1];
        
        $sel = ($selected != $key) ? '' : ' selected="selected"';
        
        $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
    }

    $form .= '</select>';
    
    return $form;
}


Which allowed me to set this in my library file - though could be handy in a model etc

Code:
$query = $this->obj->db->query($sql);
        
        
        
        foreach ($query->result_array() as $row)
        {    
                 $id = $row['id'];
                 $section = $row['section'];
                
                 $dd[] = $id. '|' . $section;  
        }

        $dropdown = iform_dropdown('section_id', $dd, $sectionTitle);
        
        return $dropdown;

Tempting to rewrite the normal form_dropdown helper as I am just building this app but just in case ... I won't

Hope this helps someone.

Cheers
Mick
#9

[eluser]gtech[/eluser]
The solution I provided passes data to a view, however you are passing data to a form_dropdown function so it wont work..

try this:

Code:
$sectionTitle = $this->obj->lang->line('sections_title');
        
        $sql = "select id, section from sections";
        
        $query = $this->obj->db->query($sql);
        
        
        $data = array();
        foreach ($query->result_array() as $row)
        {
            
                 $data[$row['id']] = $row['section'];
        }
        
        $dropdown = form_dropdown('section_id', $data, $sectionTitle);
        
        return $dropdown;
#10

[eluser]coffey[/eluser]
thanks gtech. Knew I had missed something but darned if I could see it anymore.




Theme © iAndrew 2016 - Forum software by © MyBB