CodeIgniter Active Record - return a non-associative array as result - codeigniter

Is there a way to return a non-associative array as a result of a CI db query?
Say, I have this table:
id | Name
1 | Name1
2 | Name2
3 | Name3
4 | Name4
I want the names to be returned as a non-associative array.
$this->db->select('tablename')->result_array() returns an associative array.
Is this even possible in CI? I don't want to have to add an extra foreach statement if it is.
Please shed some light.

You could just use array_values(), which returns an array containing the values of your associative array.

There is no built in function for this (or at least i have never seen any), I would suggest using another function for this using your foreach loop method.
There may be other ways I would suggest looking on the CI forums, im sure some else has had this problem before.
Sorry i can't be anymore help.

Related

Best way to call functions while iterating through a foreach loop in Laravel Blade Template

I apologize if this is a stupid question – but I haven't found any great answers from searching online.
I have a database table of test scores, that looks something like this:
user | q1 | q2 | q3 | q4 | q5
-----------------------------------
user1 3 3 2 1 5
user2 4 2 1 4 5
user1 4 3 3 2 5
Any given user can have multiple entries in the table.
In my blade file, I am iterating through all of the rows in the table:
#foreach($scores as $score)
<tr>
<td>{{$score->user}}</td>
<td>{{$score->q1}}</td>
<td>{{$score->q2}}</td>
<td>{{$score->q3}}</td>
<td>{{$score->q4}}</td>
<td>{{$score->q5}}</td>
<td>{{$score->getTotalScore()}}</td>
</tr>
#endforeach
It is the function "getTotalScore()" in the last table cell that is causing the problems. I want to perform a slightly complex calculation based on all of the scores of the table – but I prefer not to do it inside the blade file.
I DO have a working version where I make use of #php / #endphp inside the blade file, and do the calculations that way – but it tends to go against my aim to have as little "calculating" in my view as possible.
Trying to put a function in the Model doesn't work, because I am not really returning a relationship. So really, I'm just not sure how to call a function, while in the middle of a #foreach() loop, iterating through returned results.
Can someone please point me in the right direction here?
Maybe you can use += in order to sum?

Creating advanced SUMIF() calculations in Quicksight

I have a couple of joined Athena tables in Quicksight. The data looks something like this:
Ans_Count | ID | Alias
10 | 1 | A
10 | 1 | B
10 | 1 | C
20 | 2 | D
20 | 2 | E
20 | 2 | F
I want to create a calculated field such that it sums the Ans_Count column based on distinct IDs only. i.e., in the example above the result should be 30.
How do I do that?? Thanks!
Are you looking for the sum before or after applying a filter?
Sumif(Ans_Count,ID) may be what your looking for.
If you need to always return the result of the sum, regardless of the filter on the visual, look at the sumOver() function.
You can use distinctCountOver at PRE_AGG level to count unique number of values for a given partition. You could use that count to drive the sumIf condition as well.
Example : distinctCountOver(operand, [partition fields], PRE_AGG)
More details about what will be visual's group by specification and an example where there duplicate IDs will help give a specific solution.
It might even be as simple as minOver(Ans_Count, [ID], PRE_AGG) and using SUM aggregation on top of it in the visual.
If you want another column with the values repeated, use sumOver(Ans_Count, [ID], PRE_AGG). Or, if you want to aggregate via QuickSight, you would use sumOver(sum(Ans_Count), [ID]).
I agree with the above suggestions to use sumOver(sum(Ans_Count), [ID]).
I have yet to understand the use cases for pre_agg, so if anyone has concrete examples please share them!
Another suggestion would be to do a sumover + partition by in your table (if possible) before uploading the dataset, then checking if the results matche with Quicksight's aggregations. I find Quicksight can be tricky with calculated fields, aggregations, and nested ifs so I've been doing calculations in SQL where possible before bringing it in to quicksight to have a better grasp of what the outputs should look like. This obviously is an extra step, but can help in understanding how quicksight pulls off calcs and brings up figures (as the documentation doesn't always give much), and spotting things that don't look right (I've had a few) before you share your analysis with a wider group.

Referencing a table given in column as text

this should be an easy question. I'm fairly new to Power Query.
I have a report table, there is a column "Queries" which are names of queries i have in my workbook.
I wish to add a column to count the number of rows in the queries.
The formula i use is =Table.AddColumn(Source, "RowCount", each Table.RowCount([Query]))
My report table would looks like below:
| Queries | RowCount |
| Qry Apple | |
| Qry Orang | |
However I am getting the error:
Expression.Error: We cannot convert the value "Qry Apple" to type Table.
Details:
Value=Qry Apple
Type=Type
Does anyone know how to solve this?
Thanks!
= Table.AddColumn(Source, "Row Count", each Table.RowCount(Expression.Evaluate([Query],#sections[Section1])))
It seems like this is one of those things that requires random obscure knowledge about the structure of PQ. Expression.Evaluate needs to know the "environment" to resolve the string in, and it appears tables in PQ are sitting in a record called [Section1] in a global query called #sections.
I found a solution to this from Chris Webb's BI Blog: Expression.Evaluate() In Power Query/M.
Basically, we need to use Expression.Evaluate in order to read the text in the [Query] column as a table. Note also that you need to include the #shared parameter so it has access to the necessary environment. (For more details, see the linked blog and the references it gives.)
= Table.AddColumn(Source, "RowCount", each Table.RowCount(Expression.Evaluate([Query], #shared)))
Finally, found the answer, after a year of practicing,
I tried using Expression.Evaluate as suggested but to no avail, hence I don't think the function can properly convert a text into a #table. I stand to be corrected.
The solution do make use of #sections, thanks so much for the brilliant idea by #Alexis Olson and #Wedge!
I used the Record.Field function to "Get" the tables into a column as table objects, then finish it off with the Table.RowCount function. For clarity, I split them into two steps.
So here it is:
let
Source = Excel.CurrentWorkbook(),
MyTables = Source{[Name="MyTables"]}[Content],
GetTblObj = Table.AddColumn(MyTables, "MyTables", each Record.Field(#sections[Section1],[Query])),
RowCount = Table.AddColumn(GetTblObj, "RowCount", each Table.RowCount([MyTables]))
in
RowCount

Get matching records in Laravel

I'm not sure if this is obvious and I don't see it because it is late here, but right now I'm struggling with the following:
I'm trying to find out if there is a match somewhere. So, profile 2 liked profile 1 and also, profile 1 liked profile 2. That would be a match.
I tried combining arrays but that that ran nowhere. ._. How could I archive this in Laravel queries?
$likey = DB::table('likes AS liker')
->join('likes AS liked', 'liker.liked_id', '=', 'liked.liker_id')
->select('liked.liker_id', 'liked.liked_id')
->where('liker.liker_id', '=', 'liked.liked_id')
->get();
Something along those lines.
EDIT: Just to clarify this solution so you don't get into temptation of copy pasting this and never figuring out what just happened here;
we are joining (using INNER JOIN, very important) this table to itself simpy because (just like you've said it) we have to check it twice. First for the liker (the one who liked someones profile first), than for the liked (the one who responded with a like in return) user. Having that in mind, we join this table checking liked_id from the first table on liker_id on the second table.
Which should give us joined result looking like:
liker.liker_id | liker.liked_id | liked.liker_id | liked.liked_id
-----------------------------------------------------------------
2 | 1 | 1 | 2
1 | 2 | 2 | 1
Mind you this will give us duplicates! (VERY IMPORTANT).
Having that in mind I would think about redisigning your table. For example adding boolean column named "liked_back" will give you much cheaper and cleaner queries rather than doing whatever this is...

laravel lists is giving wrong ids for the column values

I am fetching a list of rosters
$rostersList = Roster::where('school_id', $this->schoolId)->get()->lists('id', 'name');
$rostersList->prepend('Select Roster');
but the ids always start from 0,1,2 why is that? However according to the where condition the returned list should not be starting from 0 or 1 but from 4. What can be the possible issue here?
When you prepend a single value without assigning a key with it, the collection gets re-keyed.
Laravel prepend lets you pass a second parameter to use as the key.
So you would want something like:
$rostersList->prepend('Select Roster', '');

Resources