Member Avatar for hisabness

I am using PHP to create and index of the files contained in a directory. I then echo the files by including the index function in another php script. How can I change where the PHP shows up? It currently shows up at the top of the page but I would like to inject it into a specific DIV. I've tried using innerHTML in JavaScript but have had little success.

code is below:

<?php function Indexer($indexer) { switch ($indexer) { case "A": $indexLocation="gloss/A"; return $indexLocation; break; case "B": $indexLocation="gloss/B"; return $indexLocation; break; case "C": $indexLocation="gloss/C"; return $indexLocation; break; case "D": $indexLocation="gloss/D"; return $indexLocation; break; case "E": $indexLocation="gloss/E"; return $indexLocation; break; case "F": $indexLocation="gloss/F"; return $indexLocation; break; case "G": $indexLocation="gloss/G"; return $indexLocation; break; case "H": $indexLocation="gloss/H"; return $indexLocation; break; case "I": $indexLocation="gloss/I"; return $indexLocation; break; case "J": $indexLocation="gloss/J"; return $indexLocation; break; case "K": $indexLocation="gloss/K"; return $indexLocation; break; case "L": $indexLocation="gloss/L"; return $indexLocation; break; case "M": $indexLocation="gloss/M"; return $indexLocation; break; case "N": $indexLocation="gloss/N"; return $indexLocation; break; case "O": $indexLocation="gloss/O"; return $indexLocation; break; case "P": $indexLocation="gloss/P"; return $indexLocation; break; case "Q": $indexLocation="gloss/Q"; return $indexLocation; break; case "R": $indexLocation="gloss/R"; return $indexLocation; break; case "S": $indexLocation="gloss/S"; return $indexLocation; break; case "T": $indexLocation="gloss/T"; return $indexLocation; break; case "U": $indexLocation="gloss/U"; return $indexLocation; break; case "V": $indexLocation="gloss/V"; return $indexLocation; break; case "W": $indexLocation="gloss/W"; return $indexLocation; break; case "X": $indexLocation="gloss/X"; return $indexLocation; break; case "Y": $indexLocation="gloss/Y"; return $indexLocation; break; case "Z": $indexLocation="gloss/Z"; return $indexLocation; break; } } function renderIndex(){ if(isset($_GET['gloss'])) { $html=indexer($_GET['gloss']); $handle = opendir($html); while (false !== ($file = readdir($handle))) { $exploded = explode(".", $file); $mang="<a id='gloss' href='#'".$file."'>".$exploded[0]."</a><br>"; } closedir($handle); } else { } }

and this in the html file where i want the code to appear:

include_once 'renderIndex.php';
Member Avatar for Krstevski

First, shorten the code :)

function Indexer($indexer) { return "gloss/" . $indexer; }

Second, in html file you can't include file :)

Change the extension html to php and then you will can include renderIndex.php

Member Avatar for hisabness

Right, I changed the code to the following:

<?php function Indexer($indexer) { return "gloss/".$indexer; } function renderIndex(){ if(isset($_GET['gloss'])) { $html=Indexer($_GET['gloss']); $handle = opendir($html); while (false !== ($file = readdir($handle))) { $exploded = explode(".", $file); echo "<a id='gloss' href='#'".$file."'>".$exploded[0]."</a><br>"; } closedir($handle); } else { } }

I then have something like the following on my actual index page:

include_once 'renderIndexer.php' echo 'XXXXX<div></div>'.renderIndexer.php.'more html here';

the problem is that the indexer puts the data before the XXXX instead of after the DIV. Any help?

Member Avatar for Krstevski
<div id="yourIndexer"> include_once ('renderIndexer.php'); </div>

With this code, between div tags will be content of the "renderIndexer.php".

In your case use:

echo 'XXXXX'; echo '<div>'; include_once('renderIndexer.php'); echo '</div>'; echo 'more html here';
Member Avatar for hisabness
<div id="yourIndexer"> include_once ('renderIndexer.php'); </div>

With this code, between div tags will be content of the "renderIndexer.php".

In your case use:

echo 'XXXXX'; echo '<div>'; include_once('renderIndexer.php'); echo '</div>'; echo 'more html here';

your solution does not work.

Member Avatar for Krstevski

What happens, error, missing data or... ?

I tried this:

index.php

<html> <body> <p> Test 1 </p> <?php include_once("test.php"); ?> <p> Test 2 </p> </body> </html>

test.php

<?php echo "This is a message 1 <br/>"; echo "This is a message 2 <br/>"; echo "This is a message 3 <br/>"; ?>

And the output was:

Test 1

This is a message 1
This is a message 2
This is a message 3

Test 2

Member Avatar for hisabness

What happens, error, missing data or... ?

I tried this:

index.php

<html> <body> <p> Test 1 </p> <?php include_once("test.php"); ?> <p> Test 2 </p> </body> </html>

test.php

<?php echo "This is a message 1 <br/>"; echo "This is a message 2 <br/>"; echo "This is a message 3 <br/>"; ?>

And the output was:

I think the issue might be that im also echoing the html to index.php. So im including renderIndex.php in index.php, so it's not as straight forward as what you propose.

Member Avatar for ko ko

Your function return value, not echo value. You need to echo the value. Example:

include_once('renderIndexer.php'); echo renderIndex();
Member Avatar for hisabness

Your function return value, not echo value. You need to echo the value. Example:

include_once('renderIndexer.php'); echo renderIndex();

thats what i did and it places it in a weird location. Please see the following: http://www.abinomics.com/?gloss=A

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.