Skip to main content

New answers tagged

0 votes

How to enable my CodeIgniter controller to correctly identify ajax calls from FullCalendar4?

The is_ajax method is used to validate a request from your JS. controller if ($this->request->isAJAX()) { $email = $this->request->getPost('email'); return $this->response->...
Sistemas Pymes JC's user avatar
0 votes

codeigniter returning false from models

You should never need to return false from a method which normally returns an array of rows. Potentially returning false would mean that downstream scripts would need to check for false before trying ...
mickmackusa's user avatar
  • 49.5k
Advice
1 vote
0 replies
0 views

How to have separate ContentSecurityPolicy.php for different environments in CI4?

In my understanding in CI4 you may set in the app/Config/ContentSecurityPolicy.php by using a conditional statement, such as: Put inside the public function __construct() block if (ENVIRONMENT === '...
Ken Lee's user avatar
  • 8,657
0 votes

convert model Codeigniter into MySQL query

Your model method uses CodeIgniter's query builder methods to render the following raw SQL assuming $id is the integer value 1. SELECT * FROM `t_trx_activity_detail` WHERE `activity_detail_id` = 1 ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to declare a model method in MY_Model (from CI_Model) then allow its extending models to call that method in CodeIgniter

Having contextual models extend MY_Model which extends CI_Model has really practical benefits and this is exactly how my largest CodeIgniter application is designed (it wasn't a decision that I ...
mickmackusa's user avatar
  • 49.5k
0 votes

DELETE rows WHERE column IN a PHP array using CodeIgniter's query builder

To qualify multiple whitelisted values for a single table column with CodeIgniter's query builder pattern, prepare a flat array of values for where_in() before calling delete(). If your model method ...
mickmackusa's user avatar
  • 49.5k
0 votes

DELETE a database table row from hyperlink click using CodeIgniter's query builder

Your get_where() call is well-formed and correctly identifies that there is qualifying record ...but it's actually not needed at all because the affected rows from the delete() call will tell the tale ...
mickmackusa's user avatar
  • 49.5k
0 votes

CodeIgniter model method executing a query builder query absorbs the clauses from an unexecuted query builder object in another model method

Ordinarily, I'd just point you toward How to correct a "Column 'id' in order clause is ambiguous" error from a CodeIgniter query builder script, but that page resolves a problem with a JOIN ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to correct a "Column 'id' in order clause is ambiguous" error from a CodeIgniter query builder script

Generally, there are two ways to resolve the column ambiguity in a join query where two tables contain one or more identically named columns. As explained in 1052: Column 'id' in field list is ...
mickmackusa's user avatar
  • 49.5k
0 votes

Get MAX datetime with WHERE conditions using CodeIgniter's query builder

I'll recommend two competitive CodeIgniter query builder scripts. Performance will vary by database dialect and defined indexes. Note that I've changed the LEFT JOIN to a JOIN, because when the inbox ...
mickmackusa's user avatar
  • 49.5k
0 votes

CodeIgniter query buider SELECT IFNULL(MAX()) expression breaks on unquoted injected PHP variable

Your injected PHP variable represents a "string literal" and must be quote wrapped. More concerning, such a variable needs to be escaped in case there would be a character/substring which ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to return two separate query results from a model method

To obey the Single Responsibility Principle in OOP design, your model method should perform a solitary/atomic task that cannot be practically be broken down (abstracted) into subtasks. While you could ...
mickmackusa's user avatar
  • 49.5k
0 votes

CodeIgniter query builder script to SELECT rows WHERE a JSON column contains a property with a qualifying value

JSON_CONTAINS() is a better suited way to parse the flat JSON array of double-quoted integers and determine row qualification. public function getChildProduct(int $parent_id, ?int $limit = null, ?int $...
mickmackusa's user avatar
  • 49.5k
0 votes

SELECT rows WHERE a JSON column contains a specific property value using CodeIgniter's query builder

It's a bit cumbersome to try to shoehorn JSON accessing syntax into a WHERE clause building method because the ->> fools with CodeIgniter's parsing of the expression. Ultimately, to keep using ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to compare datetime values versus date values using CodeIgniter's query builder

date('Y-m-d h:m:s') is not what you want; that expression means: {4-digit Year}-{2-digit Month}-{2-digit Day} {12-hour Hour}-{2-digit Month}-{2-digit Second} If a PHP date was needed at all, it should ...
mickmackusa's user avatar
  • 49.5k
0 votes

SELECT SUM() of column values from rows relating to qualifying rows in another table using CodeIgniter's query builder

Your model method is riddled with flaws, so a complete refactor is warranted. To sum the column values of all rows which relate to qualifying rows in another table, use a join to avoid making ...
mickmackusa's user avatar
  • 49.5k
0 votes

CodeIgniter controller designed for pagination returns no result instead of 1 row when offset is set to 1

As you have discovered already, SQL OFFSETs are zero based. CodeIgniter's query builder is equipped to ignore a null $offset parameter (effectively behaving like a 0 offset with needing to explicitly ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to format a numeric column of each row in a CodeIgniter result set

While your coding attempt is flawed, it only carries the syntax of a CodeIgniter model script so I'll assess with that in mind. $this is the CodeIgniter's "god instance" that gives access to ...
mickmackusa's user avatar
  • 49.5k
0 votes

Passing multiple column names to CodeIgniter's select() only returns a result set containing the first nominated column

Your Model: Because your method returns potentially multiple posts, your method name should use plural grammar - getPosts. The select() method has just two parameters. select($select = '*', $escape = ...
mickmackusa's user avatar
  • 49.5k
0 votes

Count only qualifying rows per group from a JOINED table using Codeigniter

Simply add your filtering logic to the LEFT JOIN condition logic. This way only qualifying related rows will be counted. public function getCatsWithItemCounts(): array { return $this->db ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to build multiple separate queries with CodeIgniter query builder methods before any of them are executed

I honestly don't understand why you are designing your code to build three separate query builder objects at once. I mean if you are going to execute them separately, then why not build them ...
mickmackusa's user avatar
  • 49.5k
0 votes

How can I build multiple queries at once with CodeIgniter QueryBuilder?

Declare the shared clauses. Count the unlimited, filtered rows without resetting the query builder (see the second parameter). Fetch the calculated columns for the filtered, sorted, and limited rows. ...
mickmackusa's user avatar
  • 49.5k
0 votes

SELECT next and previous id relative to a searched id from a table with gapped ids using CodeIgniter's query builder

The "entirely" query builder version of your raw SQL is: public function getPrevNextRow(int $id): ?object { return $this->db ->select([ sprintf( ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to declare multiple query builder clauses once and use them with multiple executed queries using CodeIgniter's query builder

You can populate the query builder cache, then clone the db object to be used in the second query. This allows you to succinctly make two trips to the database. public function getMergedRecords(): ...
mickmackusa's user avatar
  • 49.5k
1 vote

Codeigniter passing 2 arguments to callback

CodeIgniter Version 2.1.4 When you use a callback in CodeIgniter's set_rules, the validation library is designed to automatically inject the field's current value as the first argument. Parameter 1 : ...
coding purposes's user avatar
0 votes

SELECT rows ORDERed BY two columns with CodeIgniter

Some of your table schema details are unclear, so I'll make some assumptions for the sake of completeness. Table: news Columns: source (VARCHAR), date (DATE), order (INT) public function ...
mickmackusa's user avatar
  • 49.5k
0 votes

SELECT rows ORDERed BY a column using CodeIgniter's query builder

Call order_by() with the column name of the column to sort by as the parameter. get_where() will ignore the second parameter if it is null, so conditionally populate a $where array if the $brandId is ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to declare a library class with a method calling a model method in CodeIgniter

Library classes don't actually need to "extend" the CodeIgniter core controller class. You can just access the "instance" from the constructor of your library. Whenever you need ...
mickmackusa's user avatar
  • 49.5k
0 votes

SELECT SUM() of values in a single column for rows with a timestamp from last month with CodeIgniter's query builder

CodeIgniter's query builder can support what you need. Prepare the date range boundaries in PHP and the summing query will perform well. I never recommend using false as a fallback return value from a ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to submit a value from a select field which is different from the visible option tag text in CodeIgniter

Controller: Unconditionally pass the options arrays to the view. Pass the results to the view if a submission occurred. Only pass a non-null parameters to searchBikes() when a non-Any value is ...
mickmackusa's user avatar
  • 49.5k
0 votes

Get values from array inside another array for later storing them in db

$this->input->post('address') will return the array of numerically keyed rows. Simply loop over that payload and write your model method calls inside of that loop. Controller: $rows = $this->...
mickmackusa's user avatar
  • 49.5k
0 votes

CodeIgniter gives "No tables used" error when get() is called after get_compiled_select()

You are calling $this->db->get_compiled_select() as a what seems to be a debugging step. That method will return the build query as a string and then reset the query builder cache. To prevent ...
mickmackusa's user avatar
  • 49.5k
0 votes

Where to trim the time from datetime values in CodeIgniter other than in the view layer

You could modify your model, but then that would limit the potential utility of your model method (in case other call sites might want the full datetime value). Pass in all variables instead of ...
mickmackusa's user avatar
  • 49.5k
0 votes

INSERT a new row for each word in a submitted comma-separated string using CodeIgniter

Chances are that there is no benefit to storing duplicate keywords, so I'll recommend a schema which will makes a single trip to the database, rejects duplicates, contains primary keys, and auto-...
mickmackusa's user avatar
  • 49.5k
0 votes

How i can retrive data from database with the conditions the data should be added before 6 month and the count of product should be above 200

Judging by your answer that eventually returns the num_rows(), you shouldn't be SELECTing any columns; instead you should be SELECTing COUNT() and returning that number. You had a redundant join() ...
mickmackusa's user avatar
  • 49.5k
0 votes

SELECT rows with datetime value in the next 2 days using CodeIgniter's query builder

Your query involves only one table, so there is not need to qualify the table of origin when you mention columns. select('*') can be safely omitted because that is default behavior of a SELECT query ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to round the AVG() value from a CodeIgniter query

You can call number_format() on the lone returned value from your query if you want a formatted string with thousands placeholders otherwise use round() to return a float value. public function ...
mickmackusa's user avatar
  • 49.5k
0 votes

CodeIgniter's field_field() list is fetching table field names from all schemas instead of just one

My CodeIgniter 3.1.13 project does not have a method called field_list(), but it does have field_data() which returns an array of objects with the following properties: name, type, max_length, default....
mickmackusa's user avatar
  • 49.5k
0 votes

Display only the last 5 records from the database but in ASC order using CodeIgniter's query builder

All of the sorting logic can be baked into a single SQL query (making it slightly more portable and coherent versus spreading processing logic across SQL and PHP). Use a subquery to isolate the ...
mickmackusa's user avatar
  • 49.5k
0 votes

How to add an ORDER BY clause using CodeIgniter's query builder

A from() call is not needed if the table name is declared in the get() call. The default sorting direction in SQL is "ascending"; it is not necessary to explicitly state 'asc' as the ...
mickmackusa's user avatar
  • 49.5k
0 votes

CodeIgniter's order_by() incorrectly applies backticks to a CASE expression

I don't know about the first two major versions of CodeIgniter, but certainly the third offers an order_by() method which has 3 parameters. public function order_by($orderby, $direction = '', $escape =...
mickmackusa's user avatar
  • 49.5k
0 votes

How to close a jQuery fancybox from button click

On fancybox 5.0, you can use this code in the fancybox to add a button to close it: <button type="button" onclick="window.parent.Fancybox.close()">
mazo0012's user avatar
0 votes

How to separately join a table to multiple columns from a parent table with CodeIgniter's query builder

Your where() call appears to be a mistaken attempt to implement like(). Your attempt to filter the non-normalized table data will unintentionally filter out rows that only have a qualifying category ...
mickmackusa's user avatar
  • 49.5k
0 votes

SELECT rows WHERE one of two columns fall inside a range of dates using CodeIgniter's query builder

You will need to check if the database date ranges are even partially within your input date range (using OR logic). Your original query is checking for database date ranges which are wholly within ...
mickmackusa's user avatar
  • 49.5k

Top 50 recent answers are included