PHP - Calling Variables In Different Php Files
Hi,
i'm new to php and am trying to call $variables in different in multiple hierarchy from one main variable file. I have one main file called variables.php that contain a list of variables <?php $variable1 = 'root/main/'; $variable2 = 'welcome'; $variable3 = 'testing'; ?> a second file called file1.php in the same directory as the variables.php file <?php require 'varibles.php'; include $variable1 . 'testing.php' ?> a third file called testing.php inside root/main/ <?php echo $variable2; include $varible1 . 'temp/abc.php'; ?> a fourth file called abc.php inside root/main/temp <?php echo $variable3; ?> the problem is testing.php is able to echo $variable2 but abc.php is not able to echo $variable3...is there a limit to the level of files before it can't echo a variable? Similar TutorialsI have a user form with a dete button on a dropdown. When you click said button this runs function deleteUser(){ var e = event.target var uid = e.getAttribute("data-id"); console.log(e) console.log(uid) $.ajax({ type: 'post', data: {"ajax" : 'delUser', "id" : uid} }) } which goes into this if(isset($_POST['ajax'])){ switch($_POST['ajax']) { case 'newUserMod': exit(popNewUserModal()); break; case 'userMod': exit(popUserModal($_POST['id'])); break; case 'deleteUser': exit(deleteUser($_POST['id'])); break; } }; which calls this function in another file function deleteUser($id){ include 'includes/dbconn.php'; $stmt = $conn -> prepare("DELETE FROM user WHERE id = ?"); $stmt -> bind_param('i', $id); $stmt -> execute(); header('Location: manage-users'); } I know that i am getting the correct id in the ajax and i can see that the post happens correctly. Any ideas why the users are not being deleted? This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=328735.0 Hello im receiving the error code, Quote Parse error: syntax error, unexpected ';'on line 58 Code: [Select] <?php mysql_connect("","business",""); mysql_select_db("business") or die("Unable to select database"); $result = mysql_query("SELECT subtype FROM business WHERE type ='restaurant' ORDER BY name"); $number_of_results = mysql_num_rows($result); $results_counter = 0; if ($number_of_results != 0) {while ($array = mysql_fetch_array($result);) //THIS IS LINE 58 IN MY CODE $results_counter++; if ($results_counter >= $number_of_results); ?> Firstly do you know why I would get this error? and secondly how do i call my results. I basically want to return my results and then later on format them into lists. I would also like each result to have a different variable name. eg. result1 = $result1 result 2 = $result2 result3 = $result3 etc. any guidance much appreciated. Im a bit lost. Does anyone know if it's possible to call a class with an arbitrary number of variables? I'll explain what I mean. Here is my class construct definition: Code: [Select] function __construct($file, $outFile = 'newpdf.pdf', $fontSize = 70, $alpha=0.6, $overlayMsg = 'N O T F O R S H O P', $degrees = 45) { I know that I have to pass over the $file variable for it to work now. But can I create the class passing just the $file var and the $overlayMsg var? Everything else I would want to leave as default. How would I do that? Thanks Mike Apologies for such a silly question.... Been away from php for a long time... How can i call a variable from a position above where the variable is set? I need a variable to echo out near the top of my page but its defined near the bottom of my script. As you can see i am tryin to echo the amount of rows found from the query below. Code: [Select] <?php echo $num_rows; $query = "SELECT Name, FROM table"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $num_rows = mysql_num_rows( $result ); } ?> Thanks in advance Hi Guys, I am relatively new to php, and have hit a problem. Basically I am writing a PHP socket server - which acts as a socket server for a swish movie. The only way of getting data into the swish movie is one function - loadvariablesnum() - which uses 'post' or 'get'. The swish movie is a web GUI for a system and therefore needs a reasonably decent refresh rate - 5 times a second for example. Originally I wrote a php file that created / bound / listened to the socket and then entered an infinite while loop that would read from the socket and echo the output to the swish movie. The problem with this method, was that each time the swish movie called loadvariablesnum() the PHP script would re-open - re-bind - re-listen to the socket - which caused all sorts of problems for my client. Therefore I decided to re-write the PHP file that included functions: Code: [Select] <? //TCP_Socket_Server.php global $output; function TCP_Socket_Server() { $host = "10.10.13.59"; $port = 10000; set_time_limit(0); $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); $result = socket_bind($socket, $host, $port);// or die("Could not bind to socket\n"); $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); socket_set_block($socket); $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); // read client input while(1) { $input = socket_read($spawn, 10000);// or die("Could not read input\n"); $input = trim($input); global $output; $output = $input; WriteData(); flush(); } socket_close($spawn); socket_close($socket); } function WriteData() { static $LocalTemp; global $output; $LocalTemp = $output; $TempBuffer = "&TCP_SocketServerBufferVar="; //$TempBuffer .= $GLOBALS['output']; $TempBuffer .= $LocalTemp; echo $TempBuffer; } ?> Using this method I can call TCP_Socket_Server() only once at the beginning of the swish movie using the loadvariablesnum() (from another PHP file), which sets the server running - e.g. Code: [Select] <? //Load_Server.php include 'TCP_Socket_Server.php'; TCP_Socket_Server(); ?> My plan was to then call the WriteData() function regularly approximately 5x per second, (again from another PHP file using loadvariablesnum() in swish): Code: [Select] <? //Write_Data.php include 'TCP_Socket_Server.php'; Write_Data(); ?> However, when I call this function from an external PHP file - no valid data is output by echo - even though I can see the data being received (in the browser) in the main Load_Server.php page - which is output using the same Write_Data() function. I have tried many different ways of accessing this variable ($output) from a different php file - e.g returning the variable from Write_Data() - which had the same result (i.e. no valid data). I have also tried using sessions, but could only seem to get them to work in files that did not run indefinitely - e.g. without the infinite while loop in TCP_Socket_Server(). I am currently running the PHP files (and the rest of the web content) on APACHE2 - in Ubuntu. I would really appreciate any advice on this - as at the moment, this looks like a show stopper - and would waste 2 months work. Hy! I have 3 files: file 1 (index.php) includes 2 files: file2.php and file3.php File2.php contains $aditionalStuff and in file3.php I want to use $aditionalStuff, but it wont work (like it wasn't initialized). How can I make this work? index.php include "file2.php"; include "file3.php"; file2.php $aditionalStuff = 'some stuff'; file3.php echo $aditionalStuff; Thanks! Hi, I'm having a bit of a problem sharing some variables I have across a few files. My first file (index.php) calls an ajax request for (display.php) and sends a few variables to it. All fine so far. display.php then does its thing and displays the content on index.php as it should. Now in display.php I need a variable to be passed back to index.php for use in a menu. I've tried setting a session variable in display.php but unfortunately this doesn't seem to work. I also can't use get or post as I don't want to refresh the page. Really scratching my head over this one! Edd This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=343041.0 Hi, I need to be able to call a class based on variables. E.G. I would normally do: Code: [Select] $action = new pattern1() but i would like to be able to do it dynamicaly: Code: [Select] $patNum = 1; $action = new pattern.$patNum.() Im wondering if that's possible? If so what would the correct syntax be? Many Thanks. So far I have managed to create an upload process which uploads a picture, updates the database on file location and then tries to upload the db a 2nd time to update the Thumbnails file location (i tried updating the thumbnails location in one go and for some reason this causes failure) But the main problem is that it doesn't upload some files Here is my upload.php <?php include 'dbconnect.php'; $statusMsg = ''; $Title = $conn -> real_escape_string($_POST['Title']) ; $BodyText = $conn -> real_escape_string($_POST['ThreadBody']) ; // File upload path $targetDir = "upload/"; $fileName = basename($_FILES["file"]["name"]); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); $Thumbnail = "upload/Thumbnails/'$fileName'"; if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){ // Allow certain file formats $allowTypes = array('jpg','png','jpeg','gif','pdf', "webm", "mp4"); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ // Insert image file name into database $insert = $conn->query("INSERT into Threads (Title, ThreadBody, filename) VALUES ('$Title', '$BodyText', '$fileName')"); if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; $targetFilePathArg = escapeshellarg($targetFilePath); $output=null; $retval=null; //exec("convert $targetFilePathArg -resize 300x200 ./upload/Thumbnails/'$fileName'", $output, $retval); exec("convert $targetFilePathArg -resize 200x200 $Thumbnail", $output, $retval); echo "REturned with status $retval and output:\n" ; if ($retval == null) { echo "Retval is null\n" ; echo "Thumbnail equals $Thumbnail\n" ; } }else{ $statusMsg = "File upload failed, please try again."; } }else{ $statusMsg = "Sorry, there was an error uploading your file."; } }else{ $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, mp4, webm & PDF files are allowed to upload.'; } }else{ $statusMsg = 'Please select a file to upload.'; } //Update SQL db by setting the thumbnail column to equal $Thumbnail $update = $conn->query("update Threads set thumbnail = '$Thumbnail' where filename = '$fileName'"); if($update){ $statusMsg = "Updated the thumbnail to sql correctly."; echo $statusMsg ; } else { echo "\n Failed to update Thumbnail. Thumbnail equals $Thumbnail" ; } // Display status message echo $statusMsg; ?> And this does work on most files however it is not working on a 9.9mb png fileĀ which is named "test.png" I tested on another 3.3 mb gif file and that failed too? For some reason it returns the following Updated the thumbnail to sql correctly.Updated the thumbnail to sql correctly. Whereas on the files it works on it returns REturned with status 0 and output: Retval is null Thumbnail equals upload/Thumbnails/'rainbow-trh-stache.gif' Failed to update Thumbnail. Thumbnail equals upload/Thumbnails/'rainbow-trh-stache.gif'The file rainbow-trh-stache.gif has been uploaded successfully. Any idea on why this is? Hello I have a simple question about file handling... Is it possible to list all files in directories / subdirectories, and then read ALL files in those dirs, and put the content of their file into an array? Like this: array: [SomePath/test.php] = "All In this php file is being read by a new smart function!"; [SomePath/Weird/hello.txt = "Hello world. This is me and im just trying to get some help!";and so on, until no further files exists in that rootdir. All my attempts went totally crazy and none of them works... therefore i need to ask you for help. Do you have any ideas how to do this? If so, how can I be able to do it? Thanks in Advance, pros I am using WPSQT plugin in my blog site .I code some files in PHP also.how to add that files in plugin files.
does anyone know how to decode this XML variable value into string values? I also need to know the oposite way: creating variable values into xml. I've tried several code examples but they did filter the requested data. Code: [Select] $xml='<?xml version="1.0" encoding="utf-8"?> <elements> <text identifier="ed9cdd4c-ae8b-4ecb-bca7-e12a5153bc02"> <value/> </text> <textarea identifier="a77f06fc-1561-453c-a429-8dd05cdc29f5"> <value><![CDATA[<p style="text-align: justify;">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>]]></value> </textarea> <textarea identifier="1a85a7a6-2aba-4480-925b-6b97d311ee6c"> <value><![CDATA[<p style="text-align: justify;">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>]]></value> </textarea> <image identifier="ffcc1c50-8dbd-4115-b463-b43bdcd44a57"> <file><![CDATA[images/stories/red/cars/autobedrijf.png]]></file> <title/> <link/> <target/> <rel/> <lightbox_image/> <width><![CDATA[250]]></width> <height><![CDATA[187]]></height> </image> <text identifier="4339a108-f907-4661-9aab-d6f3f00e736e"> <value><![CDATA[Kramer 5]]></value> </text> <text identifier="ea0666d7-51e3-4e52-8617-25e3ad61f8b8"> <value><![CDATA[6000 RS]]></value> </text> <text identifier="90a18889-884b-4d53-a302-4e6e4595efa0"> <value><![CDATA[Eindhoven]]></value> </text> <text identifier="410d72e0-29b3-4a92-b7d7-f01e828b1586"> <value><![CDATA[APK Pick up and return]]></value> </text> <text identifier="45b86f23-e656-4a81-bb8f-84e5ea76f71f"> <value><![CDATA[15% korting op grote beurt]]></value> </text> <text identifier="3dbbe1c6-15d6-4375-9f2f-f0e7287e29f3"> <value><![CDATA[Gratis opslag zomerbanden]]></value> </text> <text identifier="2e878db0-605d-4d58-9806-8e75bced67a4"> <value><![CDATA[Gratis abonnement of grote beurt]]></value> </text> <text identifier="94e3e08f-e008-487b-9cbd-25d108a9705e"> <value/> </text> <text identifier="73e74b73-f509-4de7-91cf-e919d14bdb0b"> <value/> </text> <text identifier="b870164b-fe78-45b0-b840-8ebceb9b9cb6"> <value><![CDATA[040 123 45 67]]></value> </text> <text identifier="8a91aab2-7862-4a04-bd28-07f1ff4acce5"> <value/> </text> <email identifier="3f15b5e4-0dea-4114-a870-1106b85248de"> <value/> <text/> <subject/> <body/> </email> <link identifier="0b3d983e-b2fa-4728-afa0-a0b640fa34dc"> <value/> <text/> <target/> <custom_title/> <rel/> </link> <relateditems identifier="7056f1d2-5253-40b6-8efd-d289b10a8c69"/> <rating identifier="cf6dd846-5774-47aa-8ca7-c1623c06e130"> <votes><![CDATA[1]]></votes> <value><![CDATA[1.0000]]></value> </rating> <googlemaps identifier="160bd40a-3e0e-48de-b6cd-56cdcc9db892"> <location><![CDATA[50.895711,5.955427]]></location> </googlemaps> </elements>'; Hello, I am having some issues with a project I am working on. I am calling a php file on another server using Code: [Select] <script type="text/javascript" src="http://www.website.com/test/file.php"></script> I have the correct type header in my php file and the php outputs JS. example Code: [Select] echo "document.write(\"some stuff\");"; that kind of thing happens alot I also have it outputting a few JS functions, example Code: [Select] echo 'function dostuff(){ stuff }'; the problem I am having is when I add the JS call to the page that calls the php it does nothing I am using document.ready I am not really sure where to go from here, I know php well enough to do anything I need but I do not know JS at all I can not post the actual source code, I know that would be much easier to troubleshoot this but this is something I just can not do. Also this needs to be done this way, I need to call to php files on another server so they can do some stuff and then use JS to change some things on another page on another server. This is a content locker, a lot like CPALead and things like that. What am I missing? Any help is greatly appreciated Hi. I have some code which needs to return a single variable from the function and stored as a variable within this page, so it can be echoed later on in the page. I couldn't get it to return as a variable, so i used "return compact();" to return the variable and "extract (myFunction());" to extract it to variables. However, when I turned on php's display errors and error reporting function, I got an error message saying "Warning: extract() [function.extract]: First argument should be an array in /my/web/site/index.php on line 6" (which is where my extract function is). This works fine with passing more than one variables through, is there another way pass one variable from a function to be stored as a variable on the page which called the function? Here is the function: Code: [Select] <?php //This is a list of numeric error codes against their text. this will return the error code as the variable $issue function checkLoginIssue() { //If there is an error code if (isset($_GET["issue"])) { //cycle through the list until the code is reached, return the text and break the switch switch ($_GET["issue"]) { case "1": $issue = '<p class="warning">Please log in to view this page</p>'; break; case "2": $issue = '<p class="warning">Wrong Username or Password</p>'; break; case "3": $issue = '<p class="warning">No user found with those details</p>'; break; } //return the variable in an array with a single value return compact('issue'); } } ?> And here is the code which calls the function: Code: [Select] <?php extract(checkLoginIssue()); ?> I am trying to call a javascript function from a php script. This is the approach I'm using. I have an alert in the function, just to confirm the function executes. Code: [Select] echo "<script type='text/javascript' src='http://localhost/Project/JScript/File.js'>"; echo "<script type='text/javascript'>functionName();</script>"; Is this the correct approach? i have a database which contains a table named girls with three columns ...Serial(int) name(text) hits(int) <?php error_reporting(E_ALL ^ E_NOTICE); $con = mysql_connect("localhost","gaurav",""); mysql_selectdb("website",$con); $index1 = rand(1,$count); $sql = mysql_query("SELECT * FROM girls WHERE Serial=$index1"); $row = mysql_fetch_array($sql); $name1 = $row['name']; $hits1 = $row['hits']; echo <<<ECO <html> <script language="javascript"> increment(){ // some php code to increase the hits in database where name = $name1 } </script> <input type="button" value="increment" onclick="increment();" name="button"> </html> ECO; ?> sorry for the code to be really haphazard..i jst started with php.....now i dont know wat code to put to increment the hits in increment function since it would also use varialble $name1 and $index1..... I have a PHP config file called config.php. inside the file is simular to this: Quote <?php class JConfig { var $offline = '0'; var $editor = 'tinymce'; var $list_limit = '20'; } ?> From another file, i have included config.php, but how do I call $editor to get "tinymce"? Thanks |