PHP - Problem With Include
Ok. So I.m trying to learn the language.
I have this program: Code: [Select] <?php require_once("fillarray.inc"); ?> <html> <head> <title>PHP Class Lesson 2 - AJ</title> </head> <body> <h1>The Circumference of a Circle</h1> <br> <table border="1"> <thead> <tr> Circle Number:</tr> <tr> Diamater</tr> <tr> Circumference</tr> <tr> radius</tr> <tr> Area</tr> </thead> <?php foreach($arr as $diameter){ echo ("<td>"); echo("<tr align=\"center\"> 1 </tr>"); echo ("<tr> $diamater </tr>"); $circumference = $diameter * M_PI; echo ("<tr> $circumference </tr>"); $radius = $diameter/2; echo ("<tr> $radius </tr>"); $area = pow($radius,2)*M_PI; echo ("<tr> $area </tr>"); echo ("</td>"); } ?> </table> </body> </html> and in fillarray.inc I have: Code: [Select] $totalnum = rand(0,25); $arr = array(); for($i=0;i<$totalnum;i++){ $arr[$i] = rand(1,100); } But when I run it, this is the display i get: Quote $totalnum = rand(0,25); $arr = array(); for($i=0;i<$totalnum;i++){ $arr[$i] = rand(1,100); } The Circumference of a Circle Circle Number: Diamater Circumference radius Area Any help on whats going on? Similar TutorialsIm having a scope problem when creating a library. This is the test file: Code: [Select] <?php include_once 'lib/lib_inventario.php'; session_start(); hacerFormulario('Memoria Ram'); ?> And this the library importer: Code: [Select] <?php $lib_usada = 'Inventario'; include_once 'registroglobal.php'; $reg = RegistroGlobal::GetInstance(); include 'errores.php'; include_once 'test.php'; include 'sesiones.php'; include 'bd.php'; include 'seguridad.php'; include 'usuarios.php'; include 'web_inventario.php'; $reg->setConf('Libreria usada', 'Basica'); ?> Well, i explain what the second code does. It imports a few modules that have function or object definitions in they. So the idea is that just importing the lib_whatever file you import all the functions you need, and also set some configuration settings. My problem with the first code is that it says that the function is undefined, when it sure exist in web_inventario file... I get this error when trying to include header.php and sidebar.php Code: [Select] hParse error: syntax error, unexpected '/' in /home/ucherllo/public_html/index.php on line 26 Here are is index.php and includes code. INDEX.PHP Code: [Select] <html> <head> <title>Cher LLoyd - UltimateCherLloyd.net - Your newest fan site for everything Cher Lloyd. News, media, photos, and more. </title> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta name="description" content="Newest fan site for everything Cher Lloyd" /> <meta name="keywords" content="cher lloyd, cher lloyd fansite, cher lloyd x-factor, fansite, fan site, swagger jagger, playa boi, dub on the track, 2010, 2011, cher lloyd photos, cher lloyd news, cher lloyd media, cher lloyd fan site" /> <meta name="author" content="D" <meta name="robots" content="index, follow" /> <!-- PUT THIS TAG IN THE head SECTION --> <script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js"> </script> <script type="text/javascript"> GS_googleAddAdSenseService("ca-pub-7892222271864999"); GS_googleEnableAllServices(); </script> <script type="text/javascript"> GA_googleAddSlot("ca-pub-7892222271864999", "FlauntDarkLeader"); </script> <script type="text/javascript"> GA_googleFetchAds(); </script> <!-- END OF TAG FOR head SECTION --> <link href="style.css" rel="stylesheet" type="text/css"> </head> <?PHP include(/home/ucherllo/public_html/header.php); ?> <td height="265" align="left" valign="top" bgcolor="#FFFFFF"> <?PHP $number=10; include("/home/ucherllo/public_html/news/show_news.php"); ?> </td> <?PHP include(/home/ucherllo/public_html/sidebar.php); ?> SIDEBAR.PHP Code: [Select] <td width="247" align="left" valign="top" bgcolor="#FFFFFF">testing</td> </tr> </table> <!-- End Save for Web Slices --> </body> </html> HEADER.PHP Code: [Select] <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> <table width="800" align="center" border="0" cellspacing="0" cellpadding="5"> <tr> <td bgcolor="#FFFFFF"><center><!-- PUT THIS TAG IN DESIRED LOCATION OF SLOT FlauntDarkLeader --> <script type="text/javascript"> GA_googleFillSlot("FlauntDarkLeader"); </script> <!-- END OF TAG FOR SLOT FlauntDarkLeader --></center></td> </tr> </table> <!-- Save for Web Slices (ucherlloyd.psd) --> <table width="801" height="359" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01"> <tr> <td colspan="11"> <img src="images/index_01.png" width="800" height="46" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="46" alt=""></td> </tr> <tr> <td colspan="7"> <img src="images/index_02.png" width="509" height="3" alt=""></td> <td rowspan="2"> <img src="images/index_03.gif" width="24" height="28" alt=""></td> <td colspan="3" rowspan="3"> <img src="images/index_04.png" width="267" height="179" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="3" alt=""></td> </tr> <tr> <td colspan="6" rowspan="3"> <img src="images/index_05.png" width="485" height="271" alt=""></td> <td> <img src="images/index_06.gif" width="24" height="25" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="25" alt=""></td> </tr> <tr> <td colspan="2" rowspan="4"> <img src="images/index_07.png" width="48" height="285" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="151" alt=""></td> </tr> <tr> <td rowspan="3"> <img src="images/index_08.png" width="13" height="134" alt=""></td> <td> <img src="images/index_09.gif" width="241" height="95" alt=""></td> <td rowspan="3"> <img src="images/index_10.png" width="13" height="134" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="95" alt=""></td> </tr> <tr> <td rowspan="2"> <img src="images/index_11.png" width="23" height="39" alt=""></td> <td><a href="index.php"><img src="images/index_12.gif" alt="" width="51" height="22" border="0"></a></td> <td> <img src="images/index_13.gif" width="88" height="22" alt=""></td> <td> <img src="images/index_14.gif" width="71" height="22" alt=""></td> <td> <img src="images/index_15.gif" width="93" height="22" alt=""></td> <td rowspan="2"> <img src="images/index_16.png" width="159" height="39" alt=""></td> <td rowspan="2"> <img src="images/index_17.png" width="241" height="39" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="22" alt=""></td> </tr> <tr> <td colspan="4"> <img src="images/index_18.png" width="303" height="17" alt=""></td> <td> <img src="images/spacer.gif" width="1" height="17" alt=""></td> </tr> </table> <table width="799" border="0" align="center" cellspacing="0" cellpadding="5"> <tr> This is my website http://ucherlloyd.net/ PS: on Index.php there is also a Cutenews include that worked before i included sidebar.php & header.php HELP? Thanks in advance! I have a code where i include file (on submit). if isset(... include "example.php"; This example.php contains a variable $message which i later print in original code. How do i prevent getting undefined variable errors? (before submit button is pressed) Thanks Hi Ive only just started using PHP so go easy I have a PHP include for a menu on a site I run. <body> <html> <ul class="avmenu"> <li><a class="current" href="index.php">Home</a></li> <li><a href="about.php">About</a></li> <li><a href="services.php">Services</a></li> <li><a href="projects.php">Projects</a> <ul> <li><a href="highcroft_house.php">Highcroft House</a></li> <li><a href="barn_conversion.php">Barn Conversion</a></li> <li><a href="sunroom_extension.php">Sunroom Extension</a></li> <li><a href="dance_school.php">Dance School</a></li> </ul> </li> <li><a href="testimonials.php">Testimonials</a></li> <li><a href="offers.php">Special Offers</a></li> <li><a href="links.php">Links</a></li> <li><a href="contact.php">Contact</a></li> </ul> As you can see, on the first link, the index link, it has the class="current" property which displays a little tab next to the menu to show what link is currently selected. With using the include, obviously the class="current" is passed on to every page. Is there a way that I can tell the include file to change according to what page its on? Any help appreciated! Thanks Tom Haven't posted here for quite a while but I'm working on my first php driven website and am having a few problems with some includes that I'm sure you'll find to be fairly basic and obvious. What I'm trying to do is get my "home" page to load into a division when you initially visit my site. The header and footer had an include that contains various links which include this home page. The navigation links to the start page work perfectly and as expected, however when the site is visited initially all I get is a blank space in the main content division. The site in question is linked here... http://valvetech.byethost14.com/ The code that I'm using is as follows and I'm at a complete loss as to what I'm doing wrong. Code: [Select] <div id="center_column"> <?php $url = ''; if (!empty($_GET['category'])) { $url .= $_GET['category'] . '/'; } if (!empty($_GET['page'])) { $url .= $_GET['page'] . '.php'; } include $url; ?> </div> Thanks in advance for any assistance anybody can give me because I'm at the end of my rope on what should be a fairly easy problem to fix. -Carl i am running a file in the following folder on my hosting server /httpdocs/folder1/folder2/script.pjp how do i include a file that is located at the root of the hosting server /httpdocs/globalFunctions.php iv tried $maindir = dirname(__FILE__) . DIRECTORY_SEPARATOR; require $maindir . DIRECTORY_SEPARATOR . "globalFunctions.php"; include('http://www.domainname.com/globalFunctions.php'); and include('../../globalFunctions.php'); but none of these work any ideas Hello, I have a simple issue with a code that uses includes. What I want to do is to include a bit of php code I made to my main pages so I don't have to edit each file that will contain the same included code. The problem with that included code is that contains variables that only the main files would know. Let me show you basically what I'm talking about. mainfile1.php Code: [Select] <?php $cat = "meow"; include "content.php"; ?> mainfile2.php Code: [Select] <?php $cat = "mew"; include "content.php"; ?> (mainfile3.php and so on...) content.php Code: [Select] echo '<center>My cat said '.$cat.' today!</center>'; I was expecting to see: My cat said meow today! and My cat said mew today! But when I loaded both pages it rendered like this: My cat said '.$cat.' today! HTML works properly but the variables are ignored and displayed as plain text. What I need to do so those variables don't get ignored? Thanks. Hello, I have a small and annoying problem. Imagine that I have a main folder on my FTP server which has index.php and I then have a php folder for all of my small scripts and an includes folder for misc files. So, for instance http://www.somedomain.com/index.php http://www.somedomain.com/php/somescript.php http://www.somedomain.com/includes/somefile.html Inside somescript.php, I need to use one of the files from the includes folder, like this for example: require('../includes/somefile.html'); This is all fine, but the problem is when I include the PHP file on my index.php page. Then I am telling PHP to go one step back to find the includes folder, but the problem is that it only needs to go one step back if it PHP script is not included on some page outside of the php folder. If I run php/somescript.php, there is no problem, but if I include php/somescript.php on my index.php, then there is trouble! I hope you guys understand what I mean. Thanks in advance! PS - I would appreciate if the solution is not about moving php/somescript.php or using full URLs in my includes. Hey there, I guess, theres something wrong with this page, 'cause it isnt loading the page I'm trying to include, please take a look: <? require "/backend/functions.php"; $con = mysql_connect('localhost', 'mdown', 'e5r8htt9'); mysql_select_db("actual", $con); dbconn(true); ?> <html> <body BGCOLOR="#000000"> [...] </style> <div class="wrapper"> <div class="rec"> <img src=test/recs.png border=0> </div> <div class="hormenu"> <img src=test/hormenu.png border=0> </div> <div class="cpanel"> <img src=test/cpanel.png border=0> </div> <div class="menu"> <img src=test/menu.png border=0> </div> <div class="baresq"> <img src=test/baresq.png border=0> </div> <div class="table"> <? include 'home/newtable.php'; ?> </div> <div class="bardir"> <img src=http://mdown.org/tretas/teste.V4/bardir.png border=0> </div> </div> </body> </html> Now the page with this code isnt showing anything inside the TABLE div and I guess it should display the content of newtable.php. The newtable.php is working perfectly and the is just the data selected from a table. Any idea about where did I messed up? thanks I am simple using include to include a file within the webpage. include $file; $file does exist and is a string. This works fine locally but when testing on my live server it isnt included and nothing below the include is executed, not even plain html. The error log shows nothing, and there are no errors on screen even when using E_ALL. So i decided to make $file point to the file on the server to see if it was picking it up and it does. So why doesnt the live server want to include this file? The file consists of php and html but there arent any errors. It wouldnt work locally if there were. Any ideas? Hey guys! Well this is actually kind of a weird problem I have been having, and not too sure why I'm getting this problem. I've tried a lot of stuff, and it doesnt seem like I've been able to find a solution. I've been trying for about a month. Anyways, I have a website created with some OOP. Well, once I write an include statement to include a file, which then includes all my classes, it messes up my font, moves my website content down, and well.. it just does weird stuff. However, once I comment the include out, the website looks normal again. so basically, I can comment out the file itself and everything is just fine. But it only happens when I include my class files. Any ideas or suggestions would be amazing! Thank you! Hi, I am trying to include my menu and header on my website. It seems to be picking up the pictures but not showing them. My code is: <?php include ("..\head.html"); ?> <table border="0" width="100%" cellspacing="0" cellpadding="2"> <tr> <td valign="top" align="left" width="90"> <?php include ("..\menu.html"); ?> My file structure is like this: FILE menu.php FILE head.php FOLDER blog - FILE index.php(where the code came from) FOLDER Images The menu and head files all link to the images folder, it is showing me the name of the image but just a little red cross. I hope that explains it ok. I don't knwo if i'm just missing something simple. Can anyone help? It's just a simple question... <?php include ('beginning.txt'); ?> Why can't I include the whole url, eg. ... include ('http://www.mywebsite.com/random/beginning.txt') ... ? It just shows a blank page if I type in the whole url name. I would like to include the file beginning.txt into several pages that are under the folder random. And another question... does it matter if I use ' or ". Some php guides say I should use include ('beginning.txt') some say I should use include ("beginning.txt"). Hi, This is an odd one (to me anyway). I've created an API for deploying gig information in either XML or JSON format. It works fine as it is, both formats of output giving me what I want. When I take out the mySQL login information and replace it with "include 'connect_include.php';" (where the file connect_include.php contains the exact login code that I replced). The JSON output works fine but the XML output doesn't run. What could be causing this?? JSON API: -http://www.api.the-guards.co.uk/jsontest.php?apikey=123456&format=json XML API: - http://www.api.the-guards.co.uk/jsontest.php?apikey=123456&format=xml API code: Code: [Select] <?php if (($_GET['apikey'])==123456 and isset($_GET['format'])){ //Set variables $format = strtolower($_GET['format']); //Connect to the Database include 'connect_include.php'; //Run our query $result = mysql_query(' SELECT * FROM tg_gig_list where gl_date >= curdate() and gl_publish = 1 order by gl_date '); //Preapre our output if($format == 'json') { while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $array[] = array($row['gl_date']=>($row['gl_venue'].', '.$row['gl_city'])); }; $output = json_encode($array); //Output the output. echo $output; header('Content-type: application/json'); } elseif($format == 'xml') { header('Content-type: text/xml'); $output = "<?xml version=\"1.0\"?>\n"; $output .= "<tggiglist>\n"; for($i = 0 ; $i < mysql_num_rows($result) ; $i++){ $row = mysql_fetch_assoc($result); $output .= "<giglist> \n"; $output .= "<giglist_date>" . $row['gl_date'] . "</giglist_date> \n"; $output .= "<giglist_venue>" . $row['gl_venue'] . "</giglist_venue> \n"; $output .= "<giglist_city>" . $row['gl_city'] . "</giglist_city> \n"; $output .= "</giglist> \n"; } $output .= "</tggiglist>"; } } else { die ('no way'); } //output the output echo $output; ?> Contents of 'connect_include.php: Code: [Select] <?php mysql_connect("database.lcn.com", "dbname", "password") or die(mysql_error()); mysql_select_db("db") or die(mysql_error()); ?> .....this is the same code that I replaced. Obviously, the 'connect' works ok as the JSON version runs fine. There's something being added to the scripting when I use the include file that isn't there when the code goes straight in.....I think???? I use my desktop (Windows) to make websites and then once it's completed transfer to my Linux server but I've come across a weird issue. On Windows the file location is like this: C:\Server\Apache\htdocs\RSCEmulation On Linux the file location is like this: /home/rscemulation/public_html Now in those folders I have a folder called 'include', in that I have 2 files: header.php, functions.php In the base folder I have index.php. I then include header.php by using: include "include/header.php"; Then I include functions.php (from header.php) by using: include "functions.php"; Now on Linux this chucks an error, even with "./functions.php" -- I have to use include "include/functions.php" even though I'm already in that directory from the file I'm calling it from, although admittedly the file is being included from the directory below but this works fine on Windows with a base PHP install. This Linux box was given to me and I'm not sure if any PHP settings have been changed or not. I also have a config.php that I keep below the public directory (htdocs or public_html) but it won't let me include it on the Linux server by using "../../config.php" (I've tried all levels ranging from 0-5 with no luck) Can anyone shed some light on this please? I need it to work the same both on the Windows and Linux boxes as I copy and paste the files regularly and it's annoying changing them! Cheers. This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=332328.0 Hi, My first post here is a cry for help I have a Windows 2003 server running IIS6/PHP5, the server hosts multiple web sites. The problem is include files that are for site A are showing on site B (each site having its own includes as part of the site files in its own site folder), though not every time, its very random, sometimes the correct includes show, sometimes ones from another site on the same server. This only occurs where the include files for both sites have the same name, such as 'inc-header.php' for example. I can only assume PHP is caching includes and because they have the same name is showing the wrong one on other sites sometimes, if I rename them to something unique then the problem goes away, but its not a practical solution to rename all include files to unique names so I find myself looking for a 'real' fix. I have a feeling its to do with the include_path in the php.ini, but right now its disabled with a semi-colon, and I don't want to set one as I have no global includes, all includes are site specific. Any help would be very much appreciated! Phil Hi Guys.. Merry Christmas Im kinda new to this fantastic world of PHP and i have a little problem i hope you can help me with. Im trying to build a website where I use include() to genereate my content. On my index.php i have a menu which includes content in a content div from external .php pages, my structure kinda goes like this.. (simplified) site/ index.php content/ fronpage.php products.php contact.php The HTML looks like this. <div id="menu"> <ul> <li><a href="index.php?page=frontpage">frontpage</a></li> <li><a href="index.php?page=products">frontpage</a></li> <li><a href="index.php?page=contact">frontpage</a></li> </ul> </div <div id="main"> <?php include('/content/'.$_GET['page'].'.php'); ?> </div> This all works very fine, but my problem is, can I have a include inside an already included page? I would like to have a menu on my products.php site, but that page is already included from above, and i would like the menu on the products.php site to stay as the content from the nested include changes with input from the /products folder. my idea was something like this. site/ index.php content/ frontpage.php products.php contact.php products/ product1.php product2.php the HTML on the index.php is the same as above and then i would add the include() on the products.php page, so its kinda the same thing, but one inside the other. HTML inside the products.php folder <div id="sub_menu"> <ul> <li><a href="#">frontpage</a></li> <li><a href="#">frontpage</a></li> <li><a href="#">frontpage</a></li> </ul> </div <div id="sub_main"> <?php include('/content/products'.$_GET['#'].'.php'); ?> </div> I dont know how to link to the new files so they will be included while the first include still stays on the page. Any of you know how and if this can be done? Or maybe at better way to do it? Hope this made sense, my first PHP question Thanks Hi Still a new comer when it comes to PHP. I have a situation where I want to use an include within an include and I am having trouble with my file paths. My main header include, includes everything for each page of my site, beyond the opening of the body to incorporate my navigation etc conditionally loading in css, and loading in titles and meta data etc depending on the page in question. This header needs to reference another include called the-pod.php which is required for every page, the only trouble is I want to use / to reference the root of the server and this is breaking my code. I can't use ../../ etc as its a different path depending on where the master file is located. So my question is how do I get around this? Can the root of the server or path to the root be stored in a variable? and if so how would I write this. Any tips / advice will be greatly appreciated. |