PHP - Newbie Help Including File Based On Url Var
See if I can explain this right.
I am currently using static files for a very basic set of pages.
Right now i have a main file that has three includes. a header include, body and footer.
Due to needing to add pages, i don't want to keep adding more and more files. instead I want to use php to change one of the includes based on the url.
so thissite.com/index.php?lesson1 would change this
<?php include("header.php"); ?> <?php include("content-lessons.html"); ?> <?php include("footer.php"); ?>to this <?php include("header.php"); ?> <?php include("lesson1.html"); ?> <?php include("footer.php"); ?>or whichever way would be the best way to set the second include. my guess was that if the second line had something like this? if(isset($_GET['lesson1'])){ include('include/in-'.$_GET['lesson1.html']can someone give me a starting place here? i am pretty sure i can pick ti up if someone gets me going in the right direction. thanks a ton. Edited by story97, 12 January 2015 - 09:03 PM. Similar TutorialsHow can i find out parent (including) file of included file? Lets imagine that we want to for example auto_prepend file to each file on our server that would write out the name of file its being executed. I know, i can use PHP_SELF but what if i want to write name of file which is already included? I have a web page and i want on it show its name even if it was included, but not the included page itself is showing name but auto_prepend file to each php file, its duty of this auto_prepend_file to write it out i dont want myself to echo it on each page? do you feel me? Hi everyone.... I have a simple search.php And I want to include that in my html output page. I tried using <?php include ('search.php'); ?> Below is my HTML code which is part of a php file...what am I doing wrong here? Code: [Select] // Final Output echo <<<__HTML_END <html> <head> <title>Photography</title> </head> <body> <table width='100%' border='0' align='center' style='width: 100%;'> $result_final <?php include("search.php");?> </table> </body> </html> __HTML_END; how to get the name of the file including a file from the included file, This one has me mixed up a bit.. I am trying to record site activity information from a common.php file using a user object. But since the file is included into different php files based on different situations I need a dynamic way of finding the file name that is including it. I could be over complicating things but right now this seems like the best solution other wise I'll have to rewrite the code on every page i write. Is there a function for doing this? Or if someone gets what I'm trying to do if they could point me to the direction of some more information on it. Thanks. I don't know if this relevant to php or css in my template folder I have file named (index.template.php). It contains this code <?php echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="Keywords" content="BHL-SimpleTool Content Manager System" /> <meta name="Author" content="BHL-SimpleTool is coded by Othman A. Taha" /> <meta name="Description" content="Site Powered by BHL-SimpleTool CMS" /> <title>BHL-SimpleTool v1.0 - Alpha</title> <link rel="stylesheet" href="themes/default/style.css" media="all" type="text/css" /> <link rel="stylesheet" href="themes/default/forms/forms.css" media="all" type="text/css" /> <link rel="stylesheet" href="modules/forum/Themes/default/style.css" type="text/css" /> <link rel="stylesheet" href="inc/jq/css/style.css" type="text/css" /> <script language="javascript" type="text/javascript" src="js/bbcodes.js"></script> <script language="javascript" type="text/javascript" src="js/smileys.js"></script> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/easySlider1.5.js"></script> </head> <body> <div id="wrapper"> my website run here </div> </body> </html>'; ?> It works fine( I made the top-margin to be 0px). But when I include index.template.php in my main (index.php) the top margin show a big margin from the top Note: I included (index.template.php) in my main (index.php) like this: <?php function __autoload($class_name) { require_once 'classes/Sessions' . '.php'; } $sessInfo = new Sessions(); $sessInfo->RunAllBhlSimpeToolStuff(); ?> The root directory:
header.php
stylesheet.css
In the following example I am trying to include the header.php file in a sub folder.
When I include the header.php like in the following example then the stylesheet.css file will not work anymo
<?php include("../header.php"); ?>The stylesheet.css file is included in the head tags of the header.php file. Is the above example the right way to do it? If yes, how can I do it so the stylesheet.css file will work too. Suppose, i have created a sub.domain.com and i want to include a file 'config.php' from root directory i.e domain.com . While i tried include("../config.php"); i wasn't successful. Anyone can help me please?? I need the solution very urgently, so please help me soon. pracasify@yahoo.com facebook.com/agptj Or is it all pieced together into one file then executed? I ask because I'm thinking of a weird way to handle errors on my site that would integrate with the interface smoothly without much work on my part. Every page on my site requires template_top.php at the start of the script and template_bottom.php at the bottom. I believe this is a pretty common thing to do, and it standardizes all my menus and interfaces and whatever else, plus it gives me a nice place to deliver messages to the user. What I want to do is something like this simplified example: template_top.php: <?php // layout stuff here try { ?> index.php: <?php require_once('template_top.php'); // a wild error appears! throw new Exception('oh no!'); require_once('template_bottom.php'); ?> template_bottom.php: <?php } catch (Exception $e) { echo 'An error occured! Message: ' . $e->getMessage(); } // layout stuff here ?> So that's where my question comes in. Both template files contain code that, if executed on it's own, is invalid. Pieced together in my pages though, it forms correct code. I'd really like to do this since all I'd have to do if my script encounters a problem is throw an Exception and everything is instantly handled without me having to fight with display issues. If this isn't possible, does anybody have any better suggestions? Hi, I'm an experienced developer in desktop development, but am completely new to web technologies (except Silverlight 4). I need help uploading a file from a user's machine to my web hosting server. Here's my scenario: I have a Silverlight application which saves a temporary image to the My Documents folder. I need a PHP script which will take this image (a JPG image) and copy it to a server folder (without any use of a web form). Can anyone point me to a good tutorial that shows this? I cannot find one. They all use Form/Submit buttons, and I can't have that. Also, does this functionality require both client-side and server-side scripts, or just server-side? Thank you for your time! Max I have been following a tutorial to create my first php application, a file (image) uploading system. I am hoping to add it to my site, http://www.kalam1ty.com/upload.php I have read through the code multiple times and cannot find out what is wrong. I have tried to fix this for about half an hour now and cannot seem to find where the code is wrong. It does not generate any errors and passes all of the checks that I wrote, it just does not upload the image. Nothing in the upload part of the script looks off to me, but I am a PHP newbie, and am not 100% sure. Here is the whole page's code: Code: [Select] <?php if(isset($_FILES['image'])) { $errors = array(); $allowed_ext = array('jpg', 'jpeg', 'png', 'gif'); $file_name = $_FILES['image']['name']; $file_ext = strtolower(end(explode('.', $file_name ))); $file_size = $_FILES['image']['size']; $file_tmp = $_FILES['image']['tmp']; if(in_array($file_ext, $allowed_ext) === false) { $errors[] = 'Extionsion not allowed.'; } if($file_size > 10485760) { $errors[] = 'File too large. Must be under 10MB.'; } if(empty($errors)) { //upload file if (move_uploaded_file($file_tmp, 'uploads/images'.$file_name)) { echo 'File uploaded to kalam1ty.com/uploads/images/'; } } else { foreach ($errors as $error) { echo $error, '<br>'; } } } ?> <html> <head> <title> Kalam1ty - Home </title> <link rel="stylesheet" type="text/css" href="style.css" /> <link href='http://fonts.googleapis.com/css?family=Anonymous+Pro' rel='stylesheet' type='text/css'> <link rel="shortcut icon" href="/home/corona/Development/Web/favicon.ico" type="image/x-icon" /> <meta name="google" value="notranslate"> </head> <body> <div id="EncBG"> <div id="Navbar"> <a href="index.php">Home</a> <a href="downloads.php">Downloads</a> <a href="upload.php">Upload</a> <a href="contact.php">Contact</a> </div> <div id="Enc1"> <div id="Content"> <h2>Upload Files</h2> <form action='' method='POST' enctype='multipart/form-data'> <p> <input type='file' name='image'> <input type='submit' value='Upload'> </p> </form> </div> </div> <div id="Footer" class="whitetext" align="center"> <b><i> <a href="index.html">HOME</a> // <a href="downloads.html">DOWNLOADS</a> // <a href="#">OTHER</a> </b></i><br> <img src="smlogo.png" alt="Kalam1ty"/> </div> </div> </body> </html> Thank you guys very much if you can help me! I'm very fresh to php coding, and embedding it in html, so I'm a bit lost here. I have made a script for doing backups of mysql on the QNAP box, I want to make a page to set the variables in it, so the users don't have to change directly in the script like they do now. First the page should read in the current config, and display that in the form, and then users can change the values, and press save, quite simple I'm sure for the experienced php coder :-) I'm not sure if it's a HTML or PHP question, so here goes, if it's in the wrong forum, maybe the moderator will forgive me, and move the topic to the right one. THe page I've currently created looks like this: Code: [Select] <html><body> <h1>Mysql Backup Script</h1> <?php $config=parse_ini_file("config.txt"); ?> <form action="writeconfig.php" method="post"> <p>Days to save config: <input type="text" name="configdays" value="<?php echo $config('configdays'); ?>"/> <br> <p>Name of Backup location (share): <input type="text" name="share" value="<?php echo $config('share'); ?>"/> <br> <p>MySQL Backup User: <input type="text" name="user" value=<?php echo $config('user'); ?>"/> <p>Errorlevel (0=off, 1=error, 2=error+warn, 3=error+warn+info: <select name="error"> <option>0</option> <option>1</option> <option>2</option> <option>3</option> </select> <br> <p><input type="submit" value="Save" /> </form> </body></html> The problem is that it doesn't show the variables in the value fields, but rather the php code And the writeconfig.php looks like this: Code: [Select] <?php $configdays = $_POST['configdays']; $share = $_POST['share']; $user = $_POST['user']; $error = $_POST['error']; $int_options = array("options"=>array("min_range"=>1, "max_range"=>100)); if (!filter_var($configdays, FILTER_VALIDATE_INT, $int_options)) die("Value for number of config days is incorrect allowed value is 1-100"); $fp = fopen("config.txt", "w"); fwrite($fp, "configdays=" . $configdays . "\r\n" . "share=". $share . "\r\n" . "user=". $user . "\r\n" . "error=". $error . "\r\n"); fclose($fp); echo "Config file successfully written" ?> It's all still very raw, but the write part works ok, still needs a lot of input validation of course. As you can see, I'm still in the beginning part of this, so if I'm doing something wrong, or there's another way to do it properly, let me know! Hello, I have a form that ends up creating a file. The file has one of two values in the file: specials.php specials8.php Within several of my web pages, I have an image map, and I would like one of the href values to be the value that PHP would get from the underlying file. How would I code this to make the image map function accordingly? Notice the "Daily Specials" entry within the image map. It is this href that I need to become the value from the file. The unsuccessful code that I tried is: <?php $file38a = "test/activate.txt"; $file38b = file_get_contents("$file38a"); echo "<img src=\"sample2/sidebar1.jpg\" alt=\"Collegeville Diner\" width=\"94\" height=\"330\" border=\"0\" align=\"top\" usemap=\"#Map2\" longdesc=\"http://collegevillediner.com\" />"; echo "<map name=\"Map2\" id=\"Map2\">"; echo "<area shape=\"rect\" coords=\"8,102,88,152\" href=\"about.htm\" target=\"_self\" alt=\"About the Diner\" />"; echo "<area shape=\"rect\" coords=\"7,159,90,204\" href=\"menu.htm\" target=\"_self\" alt=\"Our Menu\" />"; echo "<area shape=\"rect\" coords=\"7,214,90,262\" href=\"$file38b\" target=\"_self\" alt=\"Daily Specials\" />; <area shape=\"rect\" coords=\"0,275,86,320\" href=\"desserts.htm\" target=\"_self\" />"; echo "</map>"; ?> Hello all brilliant minds, I'm a new in all this world of DB and coding and always i tried to avoid it because I think it is very hard (I'm trying to change now). problem: I have a text file (log.txt) have data like below ======================================== > rtrv-ls Command Accepted - Processing OXX 12-02-21 08:44:41 EST EAGLE5 42.0.1-63.38.31 rtrv-ls Command entered at terminal #7. ; OXX 12-02-21 08:44:41 EST EAGLE5 42.0.1-63.38.31 L3T SLT GWS GWS GWS LSN APCI (SS7) SCRN SET SET BEI LST LNKS ACT MES DIS SLSCI NIS gtcen1pls 7-010-4 none 1 2 no D 2 off off off no off gtgdv1pls 7-010-5 none 1 2 no D 2 off off off no off > rtrv-sid Command Accepted - Processing OXX 12-02-21 08:43:43 EST EAGLE5 42.0.1-63.38.31 rtrv-sid Command entered at terminal #7. ; OXX 12-02-21 08:43:43 EST EAGLE5 42.0.1-63.38.31 PCA PCI PCN CLLI PCTYPE 010-010-010 7-055-1 01830 crher1p OTHER ; . . may output like this starting always with > =================================================== Then I have SQL DB that have column with "rtrv-ls" and "rtrv-sid" Requirement : A) Use Php to open the .txt file, I used HTML form so the user can upload the file on the browser then B) Search for the column name as pattern C)Send query to MYSQL to update DB with Data for each column D) Query the Mysql for several possible outputs (I've done this part using HTML form and simple php code as below. <?php // If we got a GET request on the page from HTML part of the code, we will want to store the data in a variable e.g $my_country // So country will be passed via GET and will be stored in $my_country variable. $host="localhost"; $con_usr="aomar"; $con_pass="nokia"; if ($_POST) { $user_input = $_POST['my_log'] ; // We use the request to search in Database and return the result, First step we need to connect to the database // $connect = mysql_connect ($host,$con_usr,$con_pass); // Then we need to select database to run the query and return the result the connection stored in the $connect ressource // if ($connect) { mysql_select_db("eagle",$connect); // We store the query in variable $query becuase is it quite long text; we actually attach our varaiable $my_log passed from HTML // $query = "SELECT `".$user_input."` FROM `ss7`"; //$update="INSERT INTO ss7 ('".$user_input."') VLAUES ('; // SO if user selected Egypt it will be stored in (name=my_country) variable from HTML and will be send via GET // Then we store the query results in a variable called $result $result = mysql_query ($query) ; $arr= mysql_fetch_array($result); echo nl2br ($arr["$user_input"]); //Finally we return what we want from the array. // Print the output as we wish. in case of row data like STP output, use the nl2br to preserve the output as it was written to MYSQL DB // We need to check if the Query return no value (!$arr) , and print corresponding action } //================SECONED PART WHEN I LOAD THE LOG FILE TO BE STORED IN MYSQL================// //close the db mysql_close ($connect); } ?> <html> <body> <form action = "eagle.php?pmode=my_file" method = "POST" enctype="multipart/form-data> <fieldset> <legend> Enter you log file here and click Save: </br> </legend> <label for="my_file"> This Is My Capture File </label> <input type = "file" name ="my_file" style=margin:auto > </input><br/><br/> <input type="submit" value="Upload file"> </fieldset> </form> </body> </html> <html> <body> <title>Eagle Data Base Store</title> <form action = "eagle.php?pmode=my_log" method = "POST"> <fieldset> <legend> Please Select the DB you would like to retrive </legend> <br/><br/> <input type = "radio" name ="my_log" value = "rtrv-serial-num">Serial-Num</input> <input type = "radio" name ="my_log" value = "rtrv-stp">rtrv-stp</input> <input type = "radio" name ="my_log" value = "rtrv-feat">rtrv-feat</input> <input type = "radio" name ="my_log" value = "rtrv-dstn">rtrv-dstn</input> <input type = "radio" name ="my_log" value = "rtrv-rte">rtrv-rte</input> <input type = "radio" name ="my_log" value = "rtrv-trm">rtrv-trm</input> <input type = "radio" name ="my_log" value = "rtrv-slk">rtrv-slk</input><br/> <br/> <input type = "radio" name ="my_log" value = "rtrv-ls">rtrv-ls</input> <input type = "radio" name ="my_log" value = "rtrv-sccpopts">rtrv-sccpopts</input> <input type = "radio" name ="my_log" value = "rtrv-stpopts">rtrv-stpopts</input> <input type = "radio" name ="my_log" value = "rtrv-gsmopts">rtrv-gsmopts</input> <input type = "radio" name ="my_log" value = "rtrv-cmd">rtrv-cmd</input> <input type = "radio" name ="my_log" value = "rtrv-assoc">rtrv-assoc</input><br/> <br/> <input type = "radio" name ="my_log" value = "rtrv-gpl">rtrv-gpl</input> <input type = "radio" name ="my_log" value = "rtrv-ip-host">rtrv-ip-host</input> <input type = "radio" name ="my_log" value = "rtrv-ip-lnk">rtrv-ip-lnk</input> <input type = "radio" name ="my_log" value = "rtrv-secu-trm">rtrv-secu-trm</input> <input type = "radio" name ="my_log" value = "rtrv-secu-user">rtrc-secu-user</input> <input type = "radio" name ="my_log" value = "rept-stat-sys">rept-stat-sys</input><br/><br/> <input type = "radio" name ="my_log" value = "rept-stat-clk">rept-stat-clk</input> <input type = "radio" name ="my_log" value = "rtrv-ctrl-feat">rtrv-ctrl-feat</input> <input type = "radio" name ="my_log" value = "rtrv-tabl-capacity">rtrv-tabl-capacity</input><br/><br/> <input type = "submit" value = "Show Selected Table"/> </fieldset> </form> </body> </html> ==========================================END OF CODE=========================== *** Sorry for the many comments but I want to be sure I remember why I do that . You help is much appreciated (Please note that need to understand more than I need the Code itself) This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=355225.0 Hey all need some help with the subject title. Have a user text file with all information(Name, address, passwords, etc..) Need to be able to select the user based on Checkbox selection then hit delete button and all users checked will be removed from the txt file and thus removed from HTML page. Im using PHP to show contents of the txt file into my HTML page. So far i have: <?php $file = fopen("user.txt","r"); echo '<table border="0" width="100%"> <tr> <th>Select</th> <th>Username</th> <th>Password</th> <th>FirstName</th> <th>LastName</th> <th>Email</th> <th>Address</th> </tr> '; while(!feof($file)) { $user = explode(":", fgets($file)); echo '<tr> <td><input type="checkbox" name="chkDelete" id = "chkDelete" value="select" /></td> <td>' . $user[0] . '</td> <td>' . $user[1] . '</td> <td>' . $user[2] . '</td> <td>' . $user[3] . '</td> <td>' . $user[4] . '</td> <td>' . $user[5] . '</td> </tr>'; echo '</table>'; fclose($file); ?> Am unsure as to how i would go about doing this, help would be greatly appreciated! Thanks in advanced, Daniel. Hi all, I've done a LOT of googling on this and found things that match what I need but nothing actual achieves what I need. I've used one php file before for doing multiple jobs (ie registration?step=2 / 3 etc), but I've boo-booed somewhere and it only works up until step 2, step 3 doesn't work. Here's what I have currently, it also leads me on to the next question. Code: [Select] <?php if(isset($_GET['step']) == 2) { ?> Please select the problem you are encountering. <a href="registration.php?step=3&console=<?php $_GET['console'] ?>&issue=1rrod"><img src="store/image/data/1rrod.gif" width="92" height="92" /></a> <?php } if (isset($_GET['step']) == 3) { ?> END <?php } else {?> Please select which type of Xbox 360 you have<br /> <a href="registration.php?step=2&console=xbox"><img src="images/xbox360.gif"></a> <a href="registration.php?step=2&console=xboxslim"><img src="images/xbox360e.gif"></a> <?php } ?> So, first step (which is bottom), lets you select which type of console it is - only two possible choices. That then passes that data to step 2, which is what issue the console is having. Now I've tried this with $_POST, as I would prefer to work that way. I've experimented with $_SESSION to try and pass everything I need, using code found elsewhere. But I just can't get it to pass from page to page and finally be able to use it to email in the final step. Final issue - I want to use images instead of buttons to be able to pass the variables. I've found that using hidden type input does work but I think this issue is related to the above one. Thanks! Jason Hi Still very new to PHP but getting some good use out of it with includes. I'm currently using this code to check the file name of the page and then give the corresponding anchor tag a class of active in order to style my main menu. This is all working for most of my pages but my problem is that I now have multiple files called index.php one in the root of my site and some are in folders. Is there any way around this issue? Can PHP check to see if its the index file in the root of the site or is there a better way. I suppose I could create a second variable in the index file in my root ie my home page and then check to say if the current page is called index and has the variable of home but how would I write this? My code Code: [Select] $currentPage = basename($_SERVER['SCRIPT_NAME'], '.php'); <li><a href="/" <?php if ($currentPage == 'index') {echo 'class="active"';} ?>>Home</a></li> Hey guys, I'm a total newbie here, and just about as a new to php. My issue: I have a very large .html file that contain multiple articles (I actually have a few of these, but we'll start with one for practicality). The article titles are all wrapped in <h2> tags, there are 10 articles in one file. The articles are very simple, just a title wrapped with <h2> and then a few paragraphs wrapped in <p> tags. What I want to know how to do: I want to know if there's a way to open that file, and have each article saved as it's own .html or .txt document (the title & following paragraphs of each article). Ultimately taking my 1 large file, and creating the subsequent 10 smaller files from the articles inside of it. I am having trouble explaining this in text so I'll try to illustrate: I have "Articles.html" - which contains (article1,article2,article3.. ..article10) I want to split "Articles.html" and create "Article1.html", "Article2.html", "Article3.html", etc. Is that possible? Or am I looking at something far more complex than I can imagine at this point - perhaps something I'd be better off doing by hand? Ultimately I intend to stick all these articles into a database, but that's the 2nd part of what I want to do (and I think will be the easier of the tasks). Let me know if you need any additional information in the event my description above is unclear... I simply am having issues figuring out how to separate out the text into individual articles. Hey there, Thanks for taking the time to read my thread. My issue is that I can't think of a way to edit a XML file using PHP's XML functionality and then assign the edited contents to a string instead of saving the file. Because my issue is that I have to edit the XML file based upon a string brought from a remote location then give it back to that remote location using a string again, to be exact I am doing it via Linux command line utilizing SSH2. This is what I managed to complete on my own. function CheckIVMPConfig($ServerID) { global $Panel; if(is_numeric($ServerID) && $this->IsValidServer($ServerID)) { // We select the game server that the FTP account was created for. $Servers = mysql_query("SELECT * FROM control_servers WHERE server_id = '".mysql_real_escape_string($FTPAccount['ftp_server'])."'"); $Server = mysql_fetch_array($Servers); // Here we select the Box ID that the game server is on. $Boxs = mysql_query("SELECT * FROM control_machines WHERE machine_id = '".$Server['server_machine']."'"); $Box = mysql_fetch_array($Boxs); // Now we select the required package for the box. $Packages = mysql_query("SELECT * FROM control_packages WHERE package_id = '".$Server['server_package']."'"); $Package = mysql_fetch_array($Packages); // Retrive the file. $Config = $CProtocol->exec("cat /home/{$Server['server_id']}/{$Package['package_config']}"); $Parse = SimpleXMLElement($Config); foreach($Parse as $Entry) // loop through our books { if($Entry->port != $Server['server_port']) { // edit the value } else if($Entry->maxplayers > $Server['server_slots']) { // edit the value } } } } Hi, I have a class: Code: [Select] class Toplist { public function get_article($id) { $id = (int) $id; $articles = array(); $query = $db->sql_query("SELECT * FROM `toplist` WHERE `id` = '$id'"); $result = $db->sql_fetchrowset($query); return $articles; } } and I get Quote Notice: Undefined variable: db in classToplist.php on line 11 Fatal error: Call to a member function sql_query() on a non-object in classToplist.php on line 11 the variable db is in ./models/config.php and it isn't working on the classToplist. I have a feeling that this happens because the config file is not a class. How can I fix this? Okay so we all know Code: [Select] <?php include "theme.html"; print: "Hello"; ?> Would output the theme and output hello at the bottom of the page, my question is, how do you position hello to go on the current theme? Thanks. |