Hi, hope you guys are doing ok,
I'm having trouble changing an array to an object in CI4, I have my working code in PHP which works fine, but I'm having difficulty writing it on CI4, below is my query for a "search" in my Database that retrieves a result and set it as a associative array(object) with a null value,
PHP Code:
if (isset($_GET['q'])) {
$query = mysqli_real_escape_string($conn, $_GET['q']);
$title = array();
$sql = "SELECT name FROM users WHERE name LIKE '%$query%'";
$results = mysqli_query($conn, $sql);
$names = mysqli_num_rows($results);
if ($names > 0) {
while ($row = mysqli_fetch_assoc($results)) {
$name[$row['name']] = null;
}
} else {
exit();
}
} else {
exit();
}
echo json_encode($name);
However in CI4. the closest thing I could muster up is below, which kind of work, but don't think it is right
Code:
$user = new UserModel();
$term = $this->request->getVar('term');
$data = $user->like('name', $term, 'both')->findColumn('name');
$object = (object)$data;
foreach($data as $key => $val) {
$object->$val = null;
}
return $this->response->setJSON($object);
How can I fix this code to get results as the raw PHP, which outputs the whole array converted to an object with its key(from array index) and value as null