CodeIgniter Forums
[Newbie] Need help to display results. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forum-10.html)
+--- Thread: [Newbie] Need help to display results. (/thread-75728.html)



[Newbie] Need help to display results. - g4blin - 03-10-2020

Hi i am new in using codeigniter ihave this codes

This is my model
Code:
function select_qnum_query(){

        //SELECT qnum FROM tblkiosk WHERE transaction = "landtax"
        $this->db->SELECT('qnum');
        $this->db->FROM('tblkiosk');
        $this->db->WHERE("transaction", "landtax");
        $query = $this->db->get();

        return $query->result();

this is my controller
Code:
function select_qnum(){
    $qnumber = $this->json_model->select_qnum_query();
    echo json_encode($qnumber);
    }

and this is my view
Code:
<script src ="<?php echo base_url("assets/js/jquery-3.4.1.min.js"); ?>"></script>
<html>
testing
<br>
<button>Payment</button>
<div>
<h5>Your Priority number: </h5>
<span id = "qnum"></span>
</div>
</html>
<script>
$("button").click(function(){
        $.ajax({
            url: "<?php echo base_url() ?>json_controller/select_qnum",
            type: "GET",
            dataType: 'json',
            success: function(data){
                $('#qnum').html(data);
            },
            error: function(){
                alert('Error....');
            }
        });


    });
</script>
it does not display any output. please help me


RE: [Newbie] Need help to display results. - tweenietomatoes - 03-10-2020

PHP Code:
<?php echo base_url() ?>

missing ; so make it base_url();

or use

PHP Code:
<?=base_url()?>



RE: [Newbie] Need help to display results. - zahhar - 03-10-2020

Your model uses result() that does not exis on $query object. 
You should use getResult() instead. I also simplified your code a bit: 
PHP Code:
function select_qnum_query()
{
    
$qb $this->builder();
    $query $qb->select('qnum')->where("transaction""landtax")->get();
    
    
return $query->getResult();


You could also benefit from AP Response Trait in your controller to get rid of echo, https://codeigniter4.github.io/userguide/outgoing/api_responses.html


RE: [Newbie] Need help to display results. - tweenietomatoes - 03-10-2020

return $qb->select('qnum')->where("transaction", "landtax")->get()->getResult();


RE: [Newbie] Need help to display results. - jvandemerwe - 03-11-2020

Code:
<script>
$("button").click(function(){

Are you sure that you should not have a ready in your JavaScript?

Code:
$(document).ready(function () {
 
    $("button").click(function(e) {
      e.preventDefault();
     
      ... etc ...

    });

});

You could put a var_dump('<pre>', $queryResult, '</pre>') in your model to see if you have any records. Even when using Ajax the browser will show the result for test.


RE: [Newbie] Need help to display results. - g4blin - 03-11-2020

(03-10-2020, 03:40 PM)zahhar Wrote: Your model uses result() that does not exis on $query object. 
You should use getResult() instead. I also simplified your code a bit: 
PHP Code:
function select_qnum_query()
{
    $qb $this->builder();
    $query $qb->select('qnum')->where("transaction""landtax")->get();
    
    
return $query->getResult();


You could also benefit from AP Response Trait in your controller to get rid of echo, https://codeigniter4.github.io/userguide/outgoing/api_responses.html

Still it doesn't run. Does it need ->from('tblkiosk')? I tried to put it but still no result. :(


RE: [Newbie] Need help to display results. - g4blin - 03-12-2020

(03-11-2020, 10:23 AM)jvandemerwe Wrote:
Code:
<script>
$("button").click(function(){

Are you sure that you should not have a ready in your JavaScript?

Code:
$(document).ready(function () {
 
    $("button").click(function(e) {
      e.preventDefault();
    
      ... etc ...

    });

});

You could put a var_dump('<pre>', $queryResult, '</pre>') in your model to see if you have any records. Even when using Ajax the browser will show the result for test.
Still no result. Sad


RE: [Newbie] Need help to display results. - InsiteFX - 03-12-2020

Where are your <head> and <body> tags your JavaScript should go just above your </body></html>

unless it is called first in your html then the scripts need to be in the <head></head> tags.

How does jQuery know which button is being clicked?

Code:
<button id="btnSubmit">Payment</button>

$('#btnSubmit').click(function(){
    $.ajax({
        url: "<?php echo base_url() ?>json_controller/select_qnum",
        type: "GET",
        dataType: 'json',
        success: function(data){
            $('#qnum').html(data);
        },
        error: function(){
            alert('Error....');
        }
    });
});