Tips on saving every query outcome in a separate array and delivering it back to the controller upon completion

I am currently facing an issue where I receive data in a function from my controller, and inside my model function, I need to retrieve results using a query with a dynamic value of channel. The channel ID will be coming from each checkbox on my HTML view. When I check CHECKBOX 1, it retrieves the data successfully. However, for the second checkbox, it does not return anything. I want to iterate through the query and save the results in the final query. P.S. I am a newbie in CI framework.

function get_data() {
    $serial = $this->input->post('serial');
    $chanel = $this->input->post('channel_id');

    $fi=explode(",", $chanel);

    $conditiondata=count($fi);
    $arr =array();
    for($i=0; $i<$conditiondata; $i++) {

        if($i==0) {
            $query = $this->db->query("SELECT * FROM `channels` WHERE `serial_id` = '$serial' AND `channel_name` = '$fi[$i]'");
            if ($query->num_rows() > 0) {
                $arr[$i] = $query->result();
            } else {
                return false; 
            }
        } else {
            return false;
        }
    }

    var_dump($arr);
    
    return $arr;
}

Answer №1

Hopefully this solution resolves the issue you're facing

To fix the problem, simply remove the if($i==0) statement from the foreach loop. By setting a condition for the '0 key', you are only retrieving data for the first ID.

for($i=0;$i<$conditiondata;$i++) 
{

    $query = $this->db->query("SELECT * from `channels` WHERE `serial_id` = '$serial' AND `channel_name` = '$fi[$i]'");
    if ($query->num_rows() > 0) 
    {
        $arr[$i] = $query->result();
    }
    else
    {
       return $arr = array(); 
    }            
}
var_dump($arr);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Real-time PHP cURL proxy for streaming files

My current script looks like this: <?php $filename = "http://someurl.com/file.ext"; header('Content-Type: application/octet-stream'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$filename); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); ...

Displaying an array value instead of a new value list in a React component

Situation - Initial number input in text field - 1 List of Items - 1 6 11 Upon removing 1 from the text field, the list becomes - New List Items - NaN NaN NaN Now, if you input 4 in the field. The updated List Items are - NaN NaN 4 9 14 Expected ...

Creating a circular frame for your image to use as a Facebook profile picture

In the process of developing an input feature that allows users to upload file images for avatar changes, similar to Facebook's functionality. However, I am aware that Facebook utilizes a circular area for avatars and enables users to adjust the image ...

"The service is currently unavailable. Please try again later as we work to resolve

Currently, I am utilizing a lamp set up for my project. Specifically, I have a PHP page that includes a "send message to all" button implemented with Ajax functionality. Upon clicking this button, it triggers a function on the backend which sends a message ...

I am experiencing difficulties with opening an email attachment after it was successfully sent from the SMTP server in MVC

When I send a file using Ajax in FormData to the controller, I then proceed to send an email with an attachment. The email is sent successfully, but when I attempt to view it in my email account, I encounter an error message preventing me from opening the ...

Preserve router animations using :key attribute, while ensuring that parent views do not reload

My app utilizes :key="$route.path" in the router view for transitions, but this approach leads to the child route base view being refreshed and triggering certain APIs again. Routes: { path: "/some-route", component: () => impor ...

Using prepared statements in PHP

I am currently facing an issue while trying to prepare and bind this statement. Despite the lack of errors, the prepare() call fails with a bool(false) returned, leaving me unsure of how to proceed. // establishing connection $conn = new mysqli(###, ### ...

What is the process for incorporating a Bootstrap link into a specific ReactJS file?

In my current project using React JS, I found the need to incorporate Bootstrap in certain pages. To do this, I initially placed the following code snippet in the Public folder within index.html: <link rel="stylesheet" href="https://netdna.bootstrapc ...

Contact form malfunctions with problematic Ajax functionality

I am facing issues with my ajax contact form. When I try to submit the form, I receive a "Please fill in all fields" message even though all fields are filled out. Here is my current form code: <form method="get" action="mail.php"> <inp ...

Can data be transferred within a callback to the function it encapsulates?

I am currently working on developing a user login system and I find myself in need of querying the database. Being a beginner in coding, I am grappling with the concept of callbacks and how data can be passed once the callback has been executed. My dilemm ...

How can one implement an AJAX login feature in the Yii framework?

Recently embarked on the journey of learning AJAX and Yiiframework. Can someone guide me through the process of creating an AJAX login form in Yii? Appreciate any help. Thanks! ...

Vue is now no longer displaying errors in the console

Something strange is happening while I'm debugging a Vue/Nuxt app. Suddenly, the errors in the console have disappeared whenever my Javascript references a function or variable that doesn't exist. Instead of showing an error, it just fails silent ...

Guide on invoking the callback function using the class name specified in a different JavaScript file

After attempting to invoke the callback function using a classname defined in another JavaScript file, I encountered difficulties and was unable to make the call successfully. Despite my efforts, I couldn't pinpoint the mistake I made. Kindly review t ...

How can I access dynamically created input elements without using $refs, such as getting focus?

Is there a way to handle dynamically created inputs for editing purposes without using jQuery or vanilla JS all the time? Each input element has its own ID and is displayed through v-if when editing is triggered. However, Vue does not recognize them in r ...

AngularJS is throwing an error stating that the URL ID is undefined

I am developing an application that utilizes angularjs with codeigniter as the backend. Within my URL, I have http://localhost/submit/1234, with 1234 serving as the ID. Within my angularjs setup: var singlepost = angular.module('singlepost', [ ...

The character is having trouble displaying correctly in the ajax response

I've been searching for solutions but nothing seems to help. The issue I'm facing is with reading characters from an AJAX response. How can I properly read characters that are coming from an AJAX response in the form of a JSON object? ["label" ...

Creating a seamless rotation effect on an SVG shape at its center across all browsers, even Internet Explorer

Is there a way to make an SVG figure rotate around its center? I have been trying to calculate the rotation center and scale it based on the viewBox. It seems to work perfectly fine in Chrome, Firefox, and Safari, but I just can't seem to get it to wo ...

Suggestions for incrementing a button's ID every time it is clicked

I am working on a button functionality that increases the button id when clicked. Below is the HTML code for the button: <input type="button" id="repeataddon-1" name="repeataddon" class="repeataddon" value="add" > Accompanied by the following scr ...

Develop a C# plugin for Microsoft Dynamics CRM incorporating the use of Jquery and Ajax functionalities

Here is a demonstration of my jQuery code with Ajax: JQuery and Ajax Code In this code snippet, I am fetching field values and POSTing them to a URL in JSON format. Now, I want to achieve the same functionality in a C# plugin. While I can use query expr ...

Duplicate text content from a mirrored textarea and save to clipboard

I came across some code snippets here that are perfect for a tool I'm currently developing. The codes help in copying the value of the previous textarea to the clipboard, but it doesn't work as expected when dealing with cloned textareas. Any sug ...