Welcome Guest, Not a member yet? Register   Sign In
Question about Active Record
#1

[eluser]Hitesh Chavda[/eluser]
I have two question.

1) I want to select only one Value from database. How can i make this simple? ex. I must write following wherever I want only select one Value.
Code:
$this->db->select('field_name');
$this->db->where('something','something');
$query = $this->db->get('table_name');

foreach($query->result() as $row)
{
     echo $row->field_Name;
}

Now question is how I write this without using foreach?


2) And Suppose I don't know the field_name and I want to print the first result from the query, How can I do this. Please Help..


Thanks in Advance... :down:
#2

[eluser]Maglok[/eluser]
1) Uhhmm try $this->db->row()
"This function returns a single result row. If your query has more than one row, it returns only the first row. The result is returned as an object. Here's a usage example:"

2) Perhaps you can do something with result_array, see the user guide. I don't know off the top of my head.
#3

[eluser]jedd[/eluser]
Hi Hitesh Chavda and welcome to the CI forums.

You might also want to look at $this->db->row_array() (In the 'generating queries' section of the Database Class in the manual). Arrays are sometimes easier to get your head around than objects, and in this instance it looks like you want to extract a single column from a single row - so an array is a less expensive way of getting that data.

[quote author="Hitesh Chavda" date="1250528136"]
2) And Suppose I don't know the field_name and I want to print the first result from the query, How can I do this. Please Help..
[/quote]

Hmm .. not sure what you mean. You've done a select with $field_name - how would you select on something you don't know the name of?

If you mean that $field_name is a variable, then using row_array() you could then do this (though it's not substantively different from your current code, so I suspect I've misunderstood your intention)
Code:
$foo = $this->db->row_array();
return $foo[$field_name];
#4

[eluser]Hitesh Chavda[/eluser]
I mean that using loop every time field_name is changed.
#5

[eluser]jedd[/eluser]
You know that you can select more than one field at a time, yes?

Post some code showing what you've got so far, and describing what you're trying to do.
#6

[eluser]Hitesh Chavda[/eluser]
I have following table.
Name: t_table
Filds: f_login_1, f_login_2, f_login3, f_login_4, f_login_5

I want to do something like following whenever user login..

f_login_1 = date()
f_login_2 = f_login_1
f_login_3 = f_login_2
f_login_4 = f_login_3
f_login_5 = f_login_4
#7

[eluser]jedd[/eluser]
Quote:I want to do something like following whenever user login..

f_login_1 = date()
f_login_2 = f_login_1
f_login_3 = f_login_2
f_login_4 = f_login_3
f_login_5 = f_login_4

I suspect that you in fact want to do something like the reverse of that.

But more importantly, it looks like you have a schema confusion, rather than an algorithm or coding problem.

What's your actual intent here, and we'll work out a better way of doing it.




Theme © iAndrew 2016 - Forum software by © MyBB