Jump to content

Recommended Posts

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 :(

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 by mattnyc
code update

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 by maxxd
  • Great Answer 1
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.  ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.