mattnyc Posted April 22, 2022 Share Posted April 22, 2022 Hi all, Hoping for a little assistance. I run a query: $query = "SELECT * FROM table WHERE criteria = 'photo'"; $result = mysqli_query($dbc, $query); while ($row = mysqli_fetch_array($result)) { echo "'url(\"" . $row['col'] . "\")',<br />"; } and this gives me the following output: Quote 'url("images/photos/1.jpg")', 'url("images/photos/2.jpg")', 'url("images/photos/3.jpg")', I have a couple of issues because ultimately the output needs to be used inside some Javascript. The first issue is that while the output above is almost correct/complete, it has a comma at the very end and I'm curious of a way to remove it. The second issue is that I think I may need to store the entire output (in this case three rows) in a php variable so that I can use it inside the JS <script></script>. If anyone can assist with this I'd really appreciate it! Really - I've been working on it for days now :( Quote Link to comment https://forums.phpfreaks.com/topic/314720-php-query-rowsresult-into-a-variable-comma-separated/ Share on other sites More sharing options...
mattnyc Posted April 22, 2022 Author Share Posted April 22, 2022 (edited) Just a follow-up because I'm getting closer I think! I tried this: $query = "SELECT * FROM table WHERE criteria = 'photo'"; $result = mysqli_query($dbc, $query); while ($row = mysqli_fetch_array($result)) { $rez .= "'url(\"" . $row['col'] . "\")',<br />"; } $rez = rtrim($rez, ",<br />"); echo $rez; and this gives me the correct output: Quote 'url("images/photos/1.jpg")', 'url("images/photos/2.jpg")', 'url("images/photos/3.jpg")' Where the final comma has been stripped - Is this the correct way to remove that comma? I'm unsure how to "implode" Also the result is now in a variable ($rez) which I think I can use inside my Javascript. Which I'm about to try. Edited April 22, 2022 by mattnyc code update Quote Link to comment https://forums.phpfreaks.com/topic/314720-php-query-rowsresult-into-a-variable-comma-separated/#findComment-1595566 Share on other sites More sharing options...
maxxd Posted April 22, 2022 Share Posted April 22, 2022 (edited) Don't try to make the string yourself - javascript isn't actually expecting a string; it wants an encoded array or object. Given that, instead of concatenating a string and then finding a way to remove the final comma in order to return said string (let alone escaping and other things you'd need to deal with), make your life easier by building a simple PHP array in your while loop and then json_encode that array when you echo it. In javascript you can then JSON.parse() the returned value and use it. Edited April 22, 2022 by maxxd 1 Quote Link to comment https://forums.phpfreaks.com/topic/314720-php-query-rowsresult-into-a-variable-comma-separated/#findComment-1595567 Share on other sites More sharing options...
mattnyc Posted April 22, 2022 Author Share Posted April 22, 2022 Thanks Maxxd - I'll definitely take a look. I now have another issue with js setInterval delaying the initial run of the function. Lot's of solutions online but they aren't working for me! Quote Link to comment https://forums.phpfreaks.com/topic/314720-php-query-rowsresult-into-a-variable-comma-separated/#findComment-1595568 Share on other sites More sharing options...
gizmola Posted April 23, 2022 Share Posted April 23, 2022 21 hours ago, mattnyc said: Thanks Maxxd - I'll definitely take a look. I now have another issue with js setInterval delaying the initial run of the function. Lot's of solutions online but they aren't working for me! Great advice from @maxxd. Best practice for any ajax call is to return json. Frequently people have an issue with understanding the Event loop and how that impacts javascript execution. Whatever problem you have, we can't help you understand it, if you don't post your code, and describe the issue. Quote Link to comment https://forums.phpfreaks.com/topic/314720-php-query-rowsresult-into-a-variable-comma-separated/#findComment-1595612 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.