Welcome Guest, Not a member yet? Register   Sign In
  Using Pager with an Array
Posted by: nneves - Yesterday, 01:55 PM - No Replies

Hi

I have some results from a Model that needs to be cached after being parsed to clean user comments.

Is there's a way to use Pager with an Array?

Thanks


  CodeIgniter 4.6.1 has been released
Posted by: InsiteFX - 05-03-2025, 03:01 AM - Replies (2)

CodeIgniter 4.6.1 has been released Download
Upgrading from 4.6.0 to 4.6.1
Changes
Bugs Fixed
I' am sorry to say that while I was cleaning up the SPAM this morning I Accidently deleted the thread.
Which I' am reporting to MyBB because the thread was not even checked a post was.
Again sorry for my mistake.

I have emailed Paul to let him know about it.


  Feature to not append database prefix in QueryBuilder
Posted by: objecttothis - 05-02-2025, 02:37 AM - Replies (5)

Currently I'm not aware of the means to prevent query builder from appending the database prefix, so which you need to run a query where you do not want the prefix appended, you either can't use QueryBuilder or you have to use acrobatics to make it work. Please consider adding a parameter to QueryBuilder turning off the appending of the database prefix.

Take this code as an example of what I mean

PHP Code:
function dropForeignKeyConstraints(array $foreignKeysstring $table): void {
    
$db Database::connect();

    
$prefixName $db->getPrefix();
    
$db->setPrefix('');
    
$database_name $db->database;

    foreach (
$foreignKeys as $fk)
    {
        
$builder $db->table('INFORMATION_SCHEMA.TABLE_CONSTRAINTS');
        
$builder->select('CONSTRAINT_NAME');
        
$builder->where('TABLE_SCHEMA'$database_name);
        
$builder->where('TABLE_NAME'$table);
        
$builder->where('CONSTRAINT_TYPE''FOREIGN KEY');
        
$builder->where('CONSTRAINT_NAME'$fk);
        
$query $builder->get();

        if(
$query->getNumRows() > 0)
        {
            
$db->query("ALTER TABLE `$table` DROP FOREIGN KEY `$fk`");
        }
    }

    
$db->setPrefix($prefixName);


To prevent the query builder from appending the dbPrefix to INFORMATION_SCHEMA.TABLE_CONSTRAINTS, which would break the query, I need to capture the prefix, set it to an empty string, then put it back after I'm done with the QueryBuilder so that code below resumes the prefix appending. While this isn't needed all the time, it's better if I don't pepper the code with these extra three lines. I could write $prefixName = removePrefix() and restorePrefix($prefixName) helper functions, but that really only saves me one line of code. It would be better if I could call something like $builder = $db->table('INFORMATION_SCHEMA.TABLE_CONSTRAINTS', false); where the false parameter in the table() function returned the query without the prefix appended.


  CI4 not loading after fresh install — blank page on localhost Post:
Posted by: sultanhassann6 - 04-30-2025, 01:13 AM - No Replies

I’m trying to set up CodeIgniter 4 on my local dev machine (Ubuntu 22.04, PHP 8.1) for a side project — a fresher job board I’m working on: https://247uaefreshers.com.
I followed the official CI4 install guide using Composer, no issues during setup. But when I navigate to

Code:
localhost:8080
, I just get a blank white page — no error, no logs, nothing in the browser console either.
I’ve tried:
• Running
Code:
php spark serve
(starts fine)
• Double-checked
Code:
.env
settings
• Permissions on writable/ seem okay
• Tried adding basic echo in
Code:
app/Controllers/Home.php
— still blank
Anyone seen this behavior before? Wondering if I missed something basic. Appreciate any tips ?


  Model Find function Changed?
Posted by: dulongc - 04-29-2025, 02:41 PM - Replies (1)

In the past when I used the find() function on a model while passing the primary key, I would get a single result as the documentation states. It seems now in v. 4.6.0 it acts like it is going to return multiple results. This requires me to call $returnedArray[0]['name'] instead of just $returnedArray[0]['name']

My Relevant Code:

PHP Code:
$transID $this->request->getPost('trans_id',FILTER_SANITIZE_NUMBER_INT);
$transModel = new \App\Models\TransactionModel();
$prevTran $transModel->find($transID);

//Make sure this payment intent isn't already paid
if($prevTran['status'] == 'paid') {
                    
    
//This transaction is already paid
    
echo json_encode(array('status'=>'alread_paid','message'=> 'This transaction is already marked as paid.'));
    exit();
                    



This results in error:
Quote:message 'Undefined array key "status"'

Upon inspecting the return from the database I can see the issue is that it's returning the row within a multidimensional array.

Expected Return:
Code:
$transModel = [
    'id'         => 1,
    'name'    => 'Registration',
    'status'    => 'paid',
]

Actual Return
Code:
$transModel = [
    [0]    =>    [
        'id'         => 1,
        'name'    => 'Registration',
        'status'    => 'paid',
    ],
]

I can do a work around by just overwriting the response. But am I wrong in believing this should already be the default response.

PHP Code:
$transID $this->request->getPost('trans_id',FILTER_SANITIZE_NUMBER_INT);
$transModel = new \App\Models\TransactionModel();
$prevTran $transModel->find($transID);

//Overwrite the return here
$prevTran $prevTran[0];

//Make sure this payment intent isn't already paid
if($prevTran['status'] == 'paid') {
                    
    
//This transaction is already paid
    
echo json_encode(array('status'=>'alread_paid','message'=> 'This transaction is already marked as paid.'));
    exit();
                    


  Option to translate uri to camel case
Posted by: massimiliano1.mancini - 04-29-2025, 10:37 AM - Replies (3)

Hi All,
I'm currently using improved auto-routing, so the option to convert URIs to camelCase caught my attention. It was introduced in version 4.5.0 and enabled by default starting from version 4.6.0. I had to disable it to avoid breaking my project's code, but I'm seriously considering refactoring the code to make use of it.
The documentation notes that "The option to disable 'Translate URI To CamelCase' exists only for backward compatibility. We don’t recommend to disable it." — but the reasons behind this recommendation aren't entirely clear to me.
Before diving headfirst into the refactor, I'd like to hear your thoughts on the pros and cons of enabling this option.
Thanks in advance for any suggestions, advice, and opinions!


  How use MATCH(action) AGAINST(?)
Posted by: motoroller - 04-28-2025, 02:46 PM - Replies (4)

How use MATCH(action) AGAINST(?) in Query Builder Class
i have found only like mode


  How to refer to 'session' in my code
Posted by: PaulC - 04-28-2025, 05:05 AM - Replies (4)

Hi Team,
Chugging my way through the 3->4 migration guide, I did as suggested and changed all my old $this->session calls to $session (and all the slightly different methods).
I then wondered how/where to initialise it as the superglobal is no more and v3 autoloading has been "improved".
Eventually I read about the BaseController and how to initialise stuff there.
However, the example in the forum (somewhere) suggests setting a private $session variable and then calling $this->session = \Config\Services:Confusedession(); in the constructor.
I think I now need to refer to the session object using $this->session.
So is is $session or $this->session?
I'm so bewildered.
cheers, Paul


  ERROR: Failed to get field data from Database
Posted by: kyanoe - 04-26-2025, 04:57 AM - No Replies

I'm creating a surveys manager feature for my website. It can create a survey, edit, and delete. It's fine for the create, but not for the edit. When I'm trying to edit the "pertanyaan" (meaning: question) and added new questions that's not exist in the database, it throws an error like this:
ERROR: Failed to get field data from database.

That's somehow, it's caused by the update batch from the edit function I created.

For more information, the things that I've inspect are:
1. The columns whether on the website or the database are already good.
2. The HTML for loading the data is also good.
3. The data sent is also correct.

Here's the full code of edit function:

PHP Code:
function editPertanyaanData($database$data)
{
  $builder $database->table('s_pertanyaan');
  $idSurvey $data['id_survey'] ?: null;
  $idPertanyaan $data['id_pertanyaan'] ?: null;
  $pertanyaan $data['pertanyaan'] ?: null;
  $jenis $data['jenis'] ?: null;
  if (!$idSurvey || !$idPertanyaan || !$pertanyaan || !$jenis) {
    return null;
  }
  if (!empty($idPertanyaan)) {
    $builder->where('id_survey'$idSurvey)
      ->whereNotIn('id'$idPertanyaan)
      ->delete();
  }
  $pertanyaanDataUpdate = [];
  $pertanyaanDataInsert = [];
  foreach ($pertanyaan as $index => $teks) {
    $jenisValue = isset($jenis[$index]) && is_numeric($jenis[$index]) ? (int) $jenis[$index] : 1;
    if (empty($idPertanyaan[$index])) {
      $pertanyaanDataInsert[] = [
        'id_survey' => $idSurvey,
        'teks' => $teks,
        'jenis' => $jenisValue,
        'is_aktif' => true,
        'urutan' => $index 1,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
      ];
      continue;
    }
    $pertanyaanDataUpdate[] = [
      'id' => $idPertanyaan[$index],
      'id_survey' => $idSurvey,
      'teks' => $teks,
      'jenis' => $jenisValue,
      'is_aktif' => true,
      'urutan' => $index 1,
      'updated_at' => date('Y-m-d H:i:s'),
    ];
  }
  if (!empty($pertanyaanDataUpdate)) {
    $builder->updateBatch($pertanyaanDataUpdate, ['id']);
  }
  if (!empty($pertanyaanDataInsert)) {
    $builder->insertBatch($pertanyaanDataInsert);
  }
  return $database->affectedRows() > 0;



And here's how I call it:
PHP Code:
public function editSurvey($idSurvey)
  {
    if ($this->request->getMethod() === "POST") {
      $data $this->request->getPost();
      $data['dokumen_pendukung_survey'] = $this->request->getFile('dokumen_pendukung_survey');
      $database Database::connect();
      $database->transStart();
      $data['id_survey'] = $idSurvey;
      editSurveydata($database's_survey', [
        'kode' => $data['kode_survey'],
        'nama' => $data['nama_survey'],
        'dokumen_pendukung' => $data['dokumen_pendukung_survey'],
        'status' => $data['status_survey'],
      ], $idSurvey);

      if (!$idSurvey) {
        $database->transRollback();
        $database->close();
        return;
      }
      $result editSurveydata($database's_pelaksanaan_survey', [
        'id_periode' => $data['id_periode'],
        'tanggal_mulai' => $data['tanggal_mulai'],
        'tanggal_selesai' => $data['tanggal_selesai'],
        'deskripsi' => $data['deskripsi_survey'],
        'created_at' => date('Y-m-d H:i:s'),
      ], $idSurvey);
      if (!$result) {
        $database->transRollback();
        $database->close();
        return;
      }
      $result editPertanyaanData($database$data);
      if (!$result) {
        $database->transRollback();
        $database->close();
        return;
      }
      $database->transCommit();
      $database->close();
      return alert('survey/manajemen-survey''success''Survey berhasil diupdate!');
    }

    $data['survey'] = $this->surveyModel->find($idSurvey);
    if (!$data['survey']) {
      return alert('survey/manajemen-survey''error''Survey tidak ditemukan!');
    }
    $data['pelaksanaan_survey'] = $this->pelaksanaanSurveyModel->where('id'$idSurvey)->first();
    if (!$data['pelaksanaan_survey']) {
      return alert('survey/manajemen-survey''error''Pelaksanaan survey tidak ditemukan!');
    }
    $data['periode'] = $this->periodeModel->findAll();
    $data['pertanyaan'] = $this->pertanyaanSurveyModel->where('id_survey'$idSurvey)->orderBy('urutan''asc')->findAll();
    echo view('layouts/header.php', ["title" => "Manajemen Survey"]);
    echo view('survey_kepuasan/manajemen_survey/edit_survey.php'$data);
    echo view('layouts/footer.php');
  


  Best way to add an anchor to pager links
Posted by: kcs - 04-25-2025, 05:56 AM - Replies (2)

Hi everyone,

I was interested in adding an anchor to my pager links to make sure that when users navigate through their results, they don't have to scroll back down to them, but rather stay at the same page level. Better user experience...
I thought I could add an anchor as a parameter maybe, but looking at the pager documentation, It does not look possible. 
I thought maybe I could pass a value to the template from my $viewData variable but when I tried, I got an 'undefined variable' error ; the template does not have access to my views variables?
In the end I made it work this way: I created 2 different templates, one to be used in my bookings page, with the #bookings anchor I need, one in my presentations page where there are reviews, with the #reviews anchor I need and I select the template to be used in my controller or my view.
It works but I feel there could be a better way to avoid having 2 template files where the only thing that changes is an anchor.
Can you share your thoughts on the best way to do this?


Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





Latest Threads
Using Pager with an Array
by nneves
Yesterday, 01:55 PM
Access of env-vars in Con...
by paulkd
Yesterday, 12:48 AM
Feature to not append dat...
by michalsn
05-03-2025, 01:21 PM
CodeIgniter 4.6.1 has bee...
by InsiteFX
05-03-2025, 11:41 AM
How use MATCH(action) AGA...
by WitER
05-01-2025, 03:46 AM
Option to translate uri t...
by massimiliano1.mancini
05-01-2025, 03:44 AM
Magic login link not work...
by taco_greco
04-30-2025, 02:41 AM
CI4 not loading after fre...
by sultanhassann6
04-30-2025, 01:13 AM
Model Find function Chang...
by dulongc
04-29-2025, 05:37 PM
How to refer to 'session'...
by InsiteFX
04-28-2025, 09:30 PM

Forum Statistics
» Members: 143,999
» Latest member: lewastepanowv
» Forum threads: 78,372
» Forum posts: 379,364

Full Statistics

Search Forums

(Advanced Search)


Theme © iAndrew 2016 - Forum software by © MyBB