Having problems mixing PHP with javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Having problems mixing PHP with javascript

Post by craigwg »

As always, I think my problem is pretty straight forward, but I can't pin it down. I have a table in my database that has two columns, an ID and column called filename. The filenames will be fed into a php loop that builds a javascript that displays images in a slide show. Here is my current code, then I will post my questions:

Code: Select all

 
<?php
 
    // Count the number of images
    $query = "select Count(*) from images order by id ASC";
    $result = mysql_query ($query) or die(mysql_error());
    $row = mysql_fetch_array ($result, MYSQL_NUM);
    $num_pics = $row[0];
    
    // Get filename
    $query = "select (select count(*) from images) as count, filename from images order by id ASC";
    $result = mysql_query ($query) or die(mysql_error());
    $row1 = mysql_fetch_array ($result, MYSQL_NUM);
 
$i=0;
while ($i<$num_pics) {
    echo "Pic[".$i."] = 'Images/SlideshowMain/".$row1['filename']'";
    $i++;
};
?>
...(the rest of the javascript and the rest of the page follows...)
 

Here are my questions:
I realize the main problem is that the apostrphes are not right. Close but not right. I think I need to put a backslash in there to cancel the double quote or forward slash or something. I played with it but can't quite get it.
Also, I think doing a double query is messing this up. I played with a few ideas but couldn't find another way to get the two pieces of information (the count of images which will change each time and the filenames).

The goal is to get it to look something like this:

Code: Select all

 
Pic[0] = 'Images/SlideshowMain/frogimage.png'
Pic[1] = 'Images/SlideshowMain/brittanywall.png'
Pic[2] = 'Images/SlideshowMain/craigriver.png'
Pic[3] = 'Images/SlideshowMain/destinyboat.png'
Pic[4] = 'Images/SlideshowMain/sarahface.png'
Pic[5] = 'Images/SlideshowMain/destinytracks.png'
Pic[6] = 'Images/SlideshowMain/edbrittany.png'
Pic[7] = 'Images/SlideshowMain/hannaharm.png'
Pic[8] = 'Images/SlideshowMain/hannahtree.png'
Pic[9] = 'Images/SlideshowMain/judithsmile.png'
Pic[10] = 'Images/SlideshowMain/judithtree.png'
Pic[11] = 'Images/SlideshowMain/londonbw.png'
Pic[12] = 'Images/SlideshowMain/sarahhand.png'
Pic[13] = 'Images/SlideshowMain/londonflower.png'
Pic[14] = 'Images/SlideshowMain/londonyard.png'
Pic[15] = 'Images/SlideshowMain/samdock.png'
Pic[16] = 'Images/SlideshowMain/samflower.png'
Pic[17] = 'Images/SlideshowMain/samhat.png'
Pic[18] = 'Images/SlideshowMain/sampurple.png'
Pic[19] = 'Images/SlideshowMain/samreflection.png'
Pic[20] = 'Images/SlideshowMain/samrock.png'
Pic[21] = 'Images/SlideshowMain/samsmile.png'
Pic[22] = 'Images/SlideshowMain/samthrow.png'
 
Help is appreciated!
mrcoffee
Forum Commoner
Posts: 31
Joined: Tue Nov 10, 2009 3:03 pm
Location: Wyoming, USA

Re: Having problems mixing PHP with javascript

Post by mrcoffee »

I realize the main problem is that the apostrphes are not right. Close but not right. I think I need to put a backslash in there to cancel the double quote or forward slash or something. I played with it but can't quite get it.
If you change $row1['filename']'"; to $row1['filename'] . " ' " ; (spaces added for clarity) it would parse correctly, but your script as-is won't do what you want (see below).
Also, I think doing a double query is messing this up. I played with a few ideas but couldn't find another way to get the two pieces of information (the count of images which will change each time and the filenames).
Your script calls mysql_fetch_array only once, so $row1 will only be the first row of the result set. You'd have to call it in each iteration to advance to the next row. However, a much easier method would be to use a while statement, which would take care of the counting for you.

Something like this should give you the results you're looking for:

Code: Select all

$query = "SELECT * FROM images";
$result = mysql_query($query);
 
$i = 0;
while($row = mysql_fetch_assoc($result)) {
     echo "Pic[$i]='{$row['filename']}';\r\n";
     $i++;
}
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Re: Having problems mixing PHP with javascript

Post by craigwg »

That's great, thanks. I always struggle with the quotes, apostrophes, and backslashes. I think this is a step towards AJAX for me, but I'm not sure. I know AJAX is a combination of PHP and Javascript. If this isn't AJAX, I don't know what is!

Thanks again
Craig
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Re: Having problems mixing PHP with javascript

Post by craigwg »

For anyone else reading this post, I revisited this and it should be said that using an asterik (*) in a production query is not ideal. It works, but can cause problems in the future. Best to name the columns you want directly.
Post Reply