Include variable in json output

Asked

Viewed 89 times

0

if($_GET['operacao'] == 'banners'){
    header('Content-Type: application/json; charset=utf-8'); 
    $rs = $mysqli->query("SELECT foto AS image FROM banners");
    $registros = mysqli_num_rows($rs);

    $arr = array();
    $url = "http://www.site.com.br/";
    while($row = $rs->fetch_assoc()) { 
        $arr[] = $row;
    } 

    $json_encode = json_encode($arr); 
    echo $json_encode; 
}

This code above generates a json like this:

[
  {"image": "freightliner.jpg"},
  {"image": "sailing-ships.jpg"},
  {"image": "taxi-cab.jpg"
  }
]

How to make the URL appear and stay like this:

[
  {"image": "http://www.site.com.br/freightliner.jpg"},
  {"image": "http://www.site.com.br/sailing-ships.jpg"},
  {"image": "http://www.site.com.br/taxi-cab.jpg"
  }
]
  • Focus on the client side, with javascript, instead of doing issno by the server with php. To avoid unnecessary data traffic and process.

  • @Danielomine How do you do it?

  • post the javascript code that reads the json result, if you have.

  • I’m doing like this: <div class="galleria image-gallery-size widget uib_w_8 d-margins" data-uib="media/image_gallery" data-ver="0" data-sm-trigger="true" images-json="http://www.site.com.br//testeConexaoMobileJson.php?operacao=banners">

  • not that.. post the script that makes the ajax request. There must be some code there with something like this: $.ajax({ ..... and this success: function(response){.. This thing usually goes between javascript tags. It’s a bit like this: <script type="text/javascript">. Get it? Edita puts the question. Do not put here in the comment.

  • I don’t use it because the file testeConexaoMobileJson.php generates json on the screen as in the format demonstrated in the topic.

  • ?? How do you order the testConexaoMobileJson.php file? After all, something has to order and read the result of this script.

  • Thus <div class="galleria image-gallery-size widget uib_w_8 d-margins" data-uib="media/image_gallery" data-ver="0" data-sm-trigger="true" images-json="http://www.site.com.br//testeConexaoMobileJson.php?operacao=banners‌​"> Straight up.

  • vc has already posted this... Just understand that this is not enough. There must be some jquery plugin, javascript script that reads this url and makes the request.. That’s what you don’t understand. Post javascript code.

  • Buddy, if I should actually post it, but there isn’t. Let’s let this concatenate part, you can help me with the $url turn up?

  • I am not your "friend". Without your cooperation there is no help. You don’t seem to understand that you need to requisition and read the result of json. This I have commented more above, but I do not understand why you insist on not paying attention..

  • @Danielomine, I’m sorry for the expression, and I’m sorry I can’t be of your assistance, but I really appreciate it. All the best for you, but if you want to help me only with including the URL, it will be of great help.

  • @Danielomine please help me with the while, include the variable url.

Show 8 more comments

1 answer

1

Just do the concatenation of the variables.

$url = "http://www.site.com.br/";
while($row = $rs->fetch_assoc()) { 
    $arr[] = $url.'/'.$row['image'];
} 

In the variable $row, amid colchetes you need to put the column name foto.

  • It was a mistake, it appeared like this: ["http://www.site.com.br//Array","http://www.site.com.br//Array","http://www.site.com.br//Array","http://www.site.com.br//Array","http://www.site.com.br//Array","http://www.site.com.br//Array","http://www.site.com.br//Array"]

  • I updated the Post.

  • "image": does not appear

  • Oh bro... I didn’t even see it. You gave another name to the column on SELECT. Seria imageinstead of foto.

  • I know, I had already changed, but it still doesn’t appear.

  • How are you doing ? Edit your question with your current code.

  • is exactly like this in the topic. Only changes its last update.

  • You’re showing up like this ["http:\/\/www.site.com.br\/670x294.jpg","http:\/\/www.site.com.br\/monte.jpg","http:\/\/www.site.com.br\/girasol.jpg","http:\/\/www.site.com.br\/sol.jpg","http:\/\/www.site.com.br\/sopro.jpg","http:\/\/www.site.com.br\/sol2.jpg","http:\/\/www.site.com.br\/115403.jpg"]

  • must appear: {"image": "http://www.site.com.br/freightliner.jpg"},

  • 2

    The way it appeared with backslashes is correct. These are escape characters. ..

  • @Zoom Any more suggestions?

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.