Welcome Guest, Not a member yet? Register   Sign In
DB Query Returns
#1

[eluser]Daburn[/eluser]
Hi Guys

I'm trying to become acquainted with CodeIgniter but now facing a problem with the returns of my DB query. I want to give all the values with no content the value "keine Angaben". Because else, the table cells with no content won't be draw.

My Model:
Code:
function list_stocks() {
            $query = $this->db->query('SELECT * FROM stocks');
            foreach ($query->result() as $value) {
                if (strlen($value) == 0) $value = "keine Angaben";
            }
            return $query;
        }

My Controller:
Code:
$this->load->model('Home_model','', TRUE);
$data['query'] = $this->Home_model->list_stocks();
                    
$this->load->view('loader', $data);

Obviously, $query returns an object, but I'm not sure how to realise it else without addressing each result separately with:
Code:
if ($row->name == "") $row->name = " "
if ($row->price == "") $row->price = " "

Thanks a lot,
Daburn
#2

[eluser]ELRafael[/eluser]
[quote author="Daburn" date="1192821087"]Hi Guys

I'm trying to become acquainted with CodeIgniter but now facing a problem with the returns of my DB query. I want to give all the values with no content the value "keine Angaben". Because else, the table cells with no content won't be draw.

My Model:
Code:
function list_stocks() {
            $query = $this->db->query('SELECT * FROM stocks');
            foreach ($query->result() as $value) {
                if (strlen($value) == 0) $value = "keine Angaben";
            }
            return $query;
        }
[/quote]

change this
Code:
if (strlen($value->the_field_of_stocks_table) == 0) $value = "keine Bier heute :(";

The rest is the best :-P

[quote author="Daburn" date="1192821087"]
My Controller:
Code:
$this->load->model('Home_model','', TRUE);
$data['query'] = $this->Home_model->list_stocks();
                    
$this->load->view('loader', $data);

Obviously, $query returns an object, but I'm not sure how to realise it else without adressing each result separately with:
Code:
nameindustry == "") $row->name = " "
if ($row->price == "") $row->price = " "

Thanks a lot,
Daburn[/quote]
#3

[eluser]Daburn[/eluser]
Hi ELRafael,

Thank you for your answer, but how can i address the current "the_field_of_stocks_table"?

Regards,
Daburn
#4

[eluser]ELRafael[/eluser]
what? i didn't understand, sorry.

What the field of the table stocks? You should have fields like id, name, or something like that joe!

If a value came blank (strlen = 0), you put No Details, right? Probably your field is close to details, hum?

Sorry man, but my english is not pretty good! But i try hard to help
#5

[eluser]Daburn[/eluser]
Jup, I have different fields like id, name, symbol, price etc.

I now can do it like:
Code:
if (strlen($value->id) == 0) $value = "keine Bier heute :(";
if (strlen($value->name) == 0) $value = "keine Bier heute :(";
if (strlen($value->symbol) == 0) $value = "keine Bier heute :(";
etc.
But that's not what I want, since I don't know how the field are named nor how many I will have in the end.

So I want to solve that with a loop. Something like
Code:
foreach (go through all the fields of stocks) {
   if (strlen($value->however_the_current_field_is_named) == 0) $value = "keine Bier heute :(";
}
Now what I mean? Smile
#6

[eluser]ELRafael[/eluser]
ah! now i got it :cheese:

Are you using MySQL???

if yes, you can use a desc statement

Let'me see.

My table

TB_NAMES
+---------+-------------+
| Field | Type |
+---------+-------------+
| id | int(4) |
| name | varchar(20) |
| surname | varchar(30) |
+---------+-------------+

So, i can use a DESC
Code:
$sql = "DESC TB_NAMES";
$query = $this->db->query($sql);

//Blah blah blah

Sorry, but here i'm using MS SQL, so if i need something, i can get in the sys_ tables. But this should work.


Maybe i'll be late to answer again!!! Lunch time here now (13:00pm) and i wanna see a sport show in tv (about brazil x equador). But i'll be back!




Theme © iAndrew 2016 - Forum software by © MyBB