Solved

Slideshow doesnt work if DB code inside <script> tags.

Posted on 2006-06-09
14
282 Views
Last Modified: 2013-12-12
Hello,

I am trying to implment a photo gallery in my php-mysql app. Basically when a person clicks on a listing they'll see bunch of data being pulled from a site and also an image slideshow. The images have to be pulled from a table and put inside one of the "while ($row = mysql_fetch_array($resultphoto, MYSQL_ASSOC))" loops to pull all the images associated with a listingid from the photos table(listingid is foreign key in it).

MY problem is that the slideshow code which is in javascript is all inside the <script></script> tags, and the DB code is also inside and it doesnt work. Nothing inside the script tage will run, even a simple echo. Is there any way around it. I am breaking my head over this one because most of the scripts that i found are like this. As soon as i place it outside script its fine, but i obviously need it inside the JavaScript.

Here is a code snippet of what i mean. Let me know if you need to see more:

<script>
.................
//configure the below images and description to your own.';

      $queryphoto = "SELECT listings.*, photos.* FROM listings INNER JOIN photos ON listings.listingid = photos.listingid  WHERE listings.listingid = $id";
      $resultphoto = @mysql_query ($queryphoto); // Run the query.
      $row = mysql_fetch_array($resultphoto, MYSQL_ASSOC);
      while ($row = mysql_fetch_array($resultphoto, MYSQL_ASSOC)) {
      echo $row['photo_name'];}
      
      g_ImageTable[g_imax++] = new Array ("./customers/uploads/this is where my .$row[photo_name]. goes.jpg", "caption");
                              
      echo '
      //extend the above list as desired
      g_dwTimeOutSec=2
                 .................
</script>
0
Comment
Question by:stevegingell
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 5

Expert Comment

by:aminerd
ID: 16875291
PHP code has to be contained within PHP tags (i.e., <?php ... ?>). If you want to echo out JavaScript, that's entirely possible:

<script>
.................
//configure the below images and description to your own.';

<?php

     $queryphoto = "SELECT listings.*, photos.* FROM listings INNER JOIN photos ON listings.listingid = photos.listingid  WHERE listings.listingid = $id";
     $resultphoto = @mysql_query ($queryphoto); // Run the query.

     while ($row = mysql_fetch_array($resultphoto, MYSQL_ASSOC)) {
     
          echo('g_ImageTable[g_imax++] = new Array ("./customers/uploads/'.$row['photo_name'].'.jpg", "caption");');

     }
                         
     echo '
     //extend the above list as desired
     g_dwTimeOutSec=2';

?>
</script>
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 16883648
As you are inside <script> tags, the PHP code (inside <?php ... ?> as mentioned by aminerd) has to output valid javascript!

So ...

<html>
<head>
<title>Bad</title>
<script type="text/javascript">
<?php
echo 'Hello, World!';
?>
</script>
<body>
No "Hello, World!"
</body>
</html>

this won't work, as the PHP code will simply output the famous text, resulting in ...

<html>
<head>
<title>Bad</title>
<script type="text/javascript">
Hello, World!
</script>
<body>
No "Hello, World!"
</body>
</html>

Which is NOT valid javascript.

And having just re-read aminerd's answer, this is what he is saying.

Ok. I'll shut up now.
0
 

Author Comment

by:stevegingell
ID: 16886407
I tried aminerd's solution and it didnt work...it just outputs nothing...it doesnt read the php code at all.

What would i need to do to make it work then? Any ideas?
0
 
LVL 5

Expert Comment

by:aminerd
ID: 16888890
Can you show more of the source code?
0
 

Author Comment

by:stevegingell
ID: 16889130
sure...i am going to put the code which includes your modification.


<?php #dealer_info.php

// Include the configuration file for error management and such.
//require_once ('includes/config.inc');

// Set the page title and include the HTML header.
$page_title = 'Listing Details';
require_once ('../mysql_connect.php'); ?>
<?php include("includes/header.html") ?><h1>Listing Details</h1><span class="content">

<?php
// Check for a valid user ID, through GET or POST.
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // Accessed through view_users.php
      $id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form has been submitted.
      $id = $_POST['id'];
} else { // No valid ID, kill the script.
      echo '<h1 id="mainhead">Page Error</h1>
      <p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
      include ('includes/footer.html');
      exit();
}

// Make the query.
$query = "SELECT customers.*, categories.*, listings.* FROM customers INNER JOIN categories ON customers.categoryid = categories.categoryid INNER JOIN listings ON customers.customerid = listings.customerid WHERE listings.listingid = " .$id;            
$result = @mysql_query ($query); // Run the query.
$num = mysql_num_rows ($result); // How many users are there?

// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
 echo '<table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
            <td width="286" valign="top">
            <span class="contentheader2">Business Name: </span>'.$row['business_name'].'<br>
            <span class="contentheader2">Category: </span>'.$row['category_name'].'<br><br>
            <span class="contentheader2">Title: </span>'.$row['title'].'<br>
            <span class="contentheader2">Price: </span>$'.$row['price'].'<br>
            <span class="contentheader2">Description: </span>'.$row['description'].'<br></td>';}
            echo '<td width="15" valign="top"></td>
            <td valign="top" width="300"><p align="right"><!--slideshow code begins here -->
                              <p align="center"><input type="button" id="btnPrev" value="<< Previous" onclick="Prev();"> <input type="button" id="bntPlay" value="Play - Stop" onclick="Play()"> <input type="button" id="btnNext" value="  Next >>  " onclick="Next();"></p>

                              <p align="center"><img id="_Ath_Slide" onload="OnImgLoad()"> </p>
                              <p align="center"><b>Description:</b> <SPAN id="_Ath_FileName"> </SPAN> <br>
                              <b>Number of Pictures: <SPAN id="_Ath_Img_X"> </SPAN> of <SPAN id="_Ath_Img_N"></SPAN></b></p>

                              <p align="center">

                              <script language="JavaScript1.2">

                              /*
                              Interactive Image slideshow with text description
                              By Christian Carlessi Salvadó (cocolinks@c.net.gt). Keep this notice intact.
                              Visit http://www.dynamicdrive.com for script
                              */


                              g_fPlayMode = 0;
                              g_iimg = -1;
                              g_imax = 0;
                              g_ImageTable = new Array();

                              function ChangeImage(fFwd)
                              {
                              if (fFwd)
                              {
                              if (++g_iimg==g_imax)
                              g_iimg=0;
                              }
                              else
                              {
                              if (g_iimg==0)
                              g_iimg=g_imax;
                              g_iimg--;
                              }
                              Update();
                              }

                              function getobject(obj){
                              if (document.getElementById)
                              return document.getElementById(obj)
                              else if (document.all)
                              return document.all[obj]
                              }

                              function Update(){
                              getobject("_Ath_Slide").src = g_ImageTable[g_iimg][0];
                              getobject("_Ath_FileName").innerHTML = g_ImageTable[g_iimg][1];
                              getobject("_Ath_Img_X").innerHTML = g_iimg + 1;
                              getobject("_Ath_Img_N").innerHTML = g_imax;
                              }


                              function Play()
                              {
                              g_fPlayMode = !g_fPlayMode;
                              if (g_fPlayMode)
                              {
                              getobject("btnPrev").disabled = getobject("btnNext").disabled = true;
                              Next();
                              }
                              else
                              {
                              getobject("btnPrev").disabled = getobject("btnNext").disabled = false;

                              }
                              }
                              function OnImgLoad()
                              {
                              if (g_fPlayMode)
                              window.setTimeout("Tick()", g_dwTimeOutSec*1000);
                              }
                              function Tick()
                              {
                              if (g_fPlayMode)
                              Next();
                              }
                              function Prev()
                              {
                              ChangeImage(false);
                              }
                              function Next()
                              {
                              ChangeImage(true);
                              }


                              ////configure below variables/////////////////////////////

                              //configure the below images and description to your own.';?>
      
      <?php
      //This query has nothing to with the pagination and is just needed for displaying the total listings
      $queryphoto = "SELECT listings.*, photos.* FROM listings INNER JOIN photos ON listings.listingid = photos.listingid  WHERE listings.listingid = $id";
      $resultphoto = @mysql_query ($queryphoto); // Run the query. ( has nothing to with the pagination )
      $row = mysql_fetch_array($resultphoto, MYSQL_ASSOC); //pull specific columns like business_name
      while ($row = mysql_fetch_array($resultphoto, MYSQL_ASSOC)) {
      echo $row['photo_name'];
      
            echo('g_ImageTable[g_imax++] = new Array ("./customers/uploads/'.$row['photo_name'].'.jpg", "caption");');

     }            
                              echo '
                              //extend the above list as desired
                              g_dwTimeOutSec=2

                              ////End configuration/////////////////////////////

                              if (document.getElementById||document.all)
                              window.onload=Play

                              </script>
                              </p>
                              <!--slideshow code ends here --></td>
      </tr>
</table>';

      mysql_free_result ($result); // Free up the resources.      
      mysql_close(); //close database connection
 ?>
<?php include_once("includes/footer.html") ?>
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 16892301
I would remove all the ...

?>whitespace<?php

This can result in things like spaces appearing in the output which could break headers and sessions if you are not careful.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:stevegingell
ID: 16895101
Rquadling - i viewed it in source code, and the comments in the JS were extending out out to include the php too. thats why nothing was showing up.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 16895196
Can we see the site?
0
 

Author Comment

by:stevegingell
ID: 16895218
Here's the section of the code which was messing it up:

//This is the comment which was extending out}';?>
<?php
      $queryphoto = "SELECT listings.*, photos.* FROM listings INNER JOIN photos ON listings.listingid = photos.listingid  WHERE listings.listingid = $id";
      
      $resultphoto = @mysql_query ($queryphoto) or die('//Query failed'); // Run the query. // Run the query. ( has nothing to with the pagination )
      $row = mysql_fetch_array($resultphoto, MYSQL_ASSOC); //pull specific columns like business_name
      while ($row = mysql_fetch_array($resultphoto, MYSQL_ASSOC)) {
            echo('g_ImageTable[g_imax++] = new Array ("./customers/uploads/'.$row['photo_name'].'", "");');
     }echo $id;
                              echo '
                              //extend the above list as desired
                              g_dwTimeOutSec=2

                              ////End configuration/////////////////////////////

                              if (document.getElementById||document.all)
                              window.onload=Play

                              </script>
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 16895280
Ah. the // was being seen in PHP as a comment and therefore not closing the '; ?>

0
 

Author Comment

by:stevegingell
ID: 16895291
or was seen by the javascript as a comment
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 16895323
Yes.
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 16983266
Closed, 500 points refunded.
PashaMod
Community Support Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now