0

I am trying to conctenate/embed database value from php script into my api URL but i don't know how to get it right, I am not familiar with php

it needs to embed the varible from DB in order to access API values like this api/rfq/1 (example)

I tried this so far

php code:


<?php 
require_once('connection.php');
session_start();

if(!$con){
    die('Please Check Your Connection'.mysqli_error());
}else{
    $query="select BusinessID from business where username='".$_SESSION['User']."'";
    $result=mysqli_query($con,$query);           
    while($row =mysqli_fetch_array($result)){
          $results[]=implode($row['BusinessID']);
    }            
    echo json_encode($results);            
           // echo 'Works ASF '+$hey;
}           

?>

jQuery file:


$(function() {  
    $.getJSON('getID.php', function(data) { 
        $.each(data, function() {
            alert(data.column);
        })  
    })

    var $hello = $('#hello');   
     $.ajax({
        type: 'GET',
        dataType: "json",
        url: 'http://slimapp/api/rfq/"'+$_SESSION['User']+'"',
        success: function(hello){
            $.each(hello, function(i,order){
                $hello.append('<div class="panel panel-default"><div class="panel-thumbnail"></div><div class="panel-body"><p class="lead"><b><img src="assets/img/Profile.png" height="28px" width="28px">'+order.Title+'<a href="#postModal" class="pull-right" role="button" data-toggle="modal"><i class="glyphicon glyphicon-plus"></i>Make Bid</a></b></p><p><img src="assets/img/RFQ.png" height="28px" width="28px">'+order.Description+'</p><p><img src="assets/img/Clock.png" height="28px" width="28px">'+order.Date+'</p><hr/><p><img src="assets/img/email.png" height="28px" width="28px"><a href="https://mail.google.com/mail/?view=cm&fs=1&to=someone@example.com&su=SUBJECT&body=BODY&bcc=someone.else@example.com">'+order.email+'</a></p><p><img src="assets/img/Phone.png" height="28px" width="28px"><a href="">'+order.cellphone+'</a><p><textarea class="form-control" placeholder="Comment"></textarea><br/><li id="btn1"></li><button class="btn btn-success pull-left" type="button" style="border-radius:12px">Comment</button></p></div></div>');       
            })
        }
    }) 

});

I expect to get data from url like this http://slimapp/api/rfq/1

1 Answers1

1

You Jquery file should look like this:

function apiCall(user) {

    var url = 'http://slimapp/api/rfq/'+ user +''; // This var is creating the right URL, using the parameter USER of the function

     $.ajax({
        type: 'GET',
        dataType: "json",
        url: url, // Calling the new right URL

        success: function(hello){

            // Handle your success

        }
    }) 

};

Here you are creating a new function, that receives the parameter User that you wanna pass in the URL.

Your PHP file should look like this:

<?php

    $results = '1';

?>

<script>

    apiCall(<?php echo $results ?>); // Passing the value 1 as a parameter for the function

</script>

Here you are calling the function apiCall and passing the value of the variable $results as parameter.

Leo Lenori
  • 11
  • 3