Solved

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

Posted on 2006-06-09
14
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Richard Quadling
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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:Richard Quadling
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
 

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:Richard Quadling
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:Richard Quadling
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:Richard Quadling
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 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