Is it possible to have an include function several times on the same page pointing to the same php file?

Here is my setup:

I have a page (form.php) which uses forms to run a query in another (results.php).

This page displays several tables based on how many records are selected the query. I then want an 'include ('pictures.php') in a row of each table.

The pictures.php diplays the variables of the record so will display different information from table to table.
---------------------------------------------------------------------------------

Currently I have include/images.php within a table column which works fine for the first record but the others do not respond. I'm guessing I need to create a new images.php object to achieve this but not sure on how to do so.

Sorry if my description seems confusing.

Thanks

Nick

It is perfectly feasible to use the include function more than once. Any inline code in the included file will be run as though the included file were run. This can sometimes cause unpredictable results and requires certain amount of care, particularly with variables.

Maybe post some of the code in the images.php file so we can see what is being included? We may be able to give you some pointers...

sure its basically just an image swapper with some javascript:

<?php $imagea= $row[3]?>;
   
     <?php $imageb= $row[4]?>;
     
       <?php $imagec= $row[5]?>;
       
         <?php $imaged= $row[6]?>;
   

       
  
      <script type="text/javascript">

      /* <![CDATA[ */
 

      function changeImage(ref) {

      var img = (document.all) ? document.all.imageSample : document.getElementById('imageSample');
 
      var myImage = [];
	  
	  var imagea = "<?php echo $imagea; ?>";
	  	  var imageb = "<?php echo $imageb; ?>";
		  	  var imagec = "<?php echo $imagec; ?>";
			  	  var imaged = "<?php echo $imaged; ?>";
	  
	 // var bluesource= 'http://localhost/personal_trainer_system/images/Bench.jpg';
	  
	 
      myImage['red'] = new Image();
 
      myImage['red'].src = imagea;
 
       
 
      // Same process must be applied as mentioned above.

      myImage['green'] = new Image();

      //myImage['green'].src = 'http://localhost/personal_trainer_system/videos/9_shoulder_pressbgifa.gif';
	  myImage['green'].src = imageb;
 
       
	  myImage['blue'] = new Image();
	 // myImage['blue'].src =  $url;
	 // myImage['blue'].src=blue source;
	myImage['blue'].src = imagec;
      
  
      myImage['black'] = new Image();
 
      myImage['black'].src = imaged;

---------------------------------------------------------
basically I want each images.php to act like a seperate object on the same page so it can show the different variables

Ok, I have to admit that javascript is not my strong suit, but I will try to help. Does the changeImage function get called by pictures.php? What gets passed to the function when it is called? It doesn't appear that the parameter is actually used in the function though, so I might be barking up the wrong tree...

pictures.php is the image swapper. I'm just using includes so I can get everything in pictures.php into seperate tables in results.php.

So basically result.php includes pictures.php once which includes images.php multiple times? Or is pictures.php included multiple times and it includes images.php once? This might help identify where the problem lies...

I'm sorry pictures.php and image.php are the same thing(my mistake), lets just call it pictures.php for arguments sake.
So results.php includes pictures.php multiple times. The problem I'm having is only the first table in results.php is showing a functioning pictures.php. I'm guessing I have to use pictures.php as a new object each time or something so they operate independentally. Does this make sense?

Thanks for your help

Nick

Is pictures.php a class file? If not then you don't need to create an object, all of the php code that isn't inside a function (called inline code) will be run when the file is included. Code inside functions or outside php (such as the javascript you posted earlier) will be run if called in the inline code.

If the code in the pictures.php file IS in a class, then yes, you will need to create a new object, but it depends on the name of the class (as opposed to the file). For example, if the pictures.php file contains the following line:

class PictureManager

then results.php would need to create a new PictureManager object like so:

$pictures = new PictureManager();

However from what you posted earier I am assuming your code is not in a class.

pictures.php has one function:

function changeImage(ref) {

this is how the images are swapped. There is no mention of class whatsoever. So will a new changeImage be necessary?

At present the first pictures.php is fully functioning. However the rest will show one image which doesn't swap. Clicking on the links to swap the image results in the first pictures.php swapping

Can you post code from results.php where you include pictures.php please? I am mostly interested in any difference between the first time and second time they are included (as one works and the other doesn't). I am fairly certain that the image swapping function must be called to actually be executed, so you must be doing that the first time correctly but then not for subsequent attempts (I think!)

pictures.php is called for every record generated from the mysql query. I only make one reference to it in my code.

Here is the code for results.php

<?php
 $page_title = 'results
 include ('includes/header.html');
 
	require "connect.php";
	
	$musclea = $_GET['muscle'];
	$stressa = $_GET['stress'];
	$timea = $_GET['time'];
	//$password = $_GET['password'];
	$query =  "select * from exercise where muscle_name = '".$musclea."' && stress > '".$stressa."' ORDER BY RAND() LIMIT ".$timea." ";
//"' and pass = '".$password."'";
	$result = @mysql_query($query, $connection) 

	

	
	
or die ("Unable to perform query<br>$query".mysql_error());
	while ($row=mysql_fetch_array($result)) {
	

	  {
	  
	  
	 
  	
  echo "<tr>";
  echo "<td><h1>" . $row[1] . "  </td> </h1> ";
  //echo "<img src=$row[3]></a>";
  echo "<td>" . $row[1] . "</td> <br> " ;
  include ('pictures.php');
  
  echo "</tr>";
  }
	echo "</table>";

	}	
	
	
?>

So your loop code is equivalently:

echo "<tr>";
echo "<td><h1>" . $row[1] . " </td> </h1> ";
echo "<td>" . $row[1] . "</td> <br> " ;
$imagea= $row[3];
$imageb= $row[4];
$imagec= $row[5];
$imaged= $row[6];
echo "</tr>";

Sorry, just reformatting so I can see what is going on, will reply again in a sec after I have looked at it...

In JavaScript it will stop running the scripts when it finds an error.

The error here is that you're creating the same function multiple times, that's why it's working the first time, but then not working after that.

You want to include a file at the top of your page with the JavaScript function in it, and then when you're looping you just want to call from the function.

Ah, I think your final </table> tag needs to go outside the final } brace. Because it is inside that brace, it is part of the loop, so will close the table after the first row.

I've tried putting the </table> outside the final } brace but it doesnt make a difference.

peter can you give me an idea of the syntax required? Am confused how I would call a function that is javascript and located in another page.

Thanks

Nick

Ok so in the file "pictures.php" you have a JavaScript function.

The function can only be defined once, and so you want to include this outside of the loop.

Looking at your code - the function isn't actually called upon, but is just used to carry out some events.

Have you tried taking the code out of the function, and using the code but not defining the function?

Sorry I have omitted the end of the code. The function is called when the different links are clicked on pictures.php:

<tr>
      <td title="Red"><a href="javascript:void(0);" onClick="changeImage('red');">Image</a></td>
  
      </tr>
      <tr>
      <td title="Green"><a href="javascript:void(0);" onClick="changeImage('green');">Video Demo</a></td>
 <tr>
 <tr>
      <td title="Blue"><a href="javascript:void(0);" onClick="changeImage('blue');">3D Demo</a></td>
 </tr>
 <tr>
      <td title="Black"><a href="javascript:void(0);" onClick="changeImage('black');">3D Extended</a></td>

 </tr>
      </table>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.