PHP - Variable Variable Array Call How?
hi all,
I have an language pack for example: languages/en.php: Code: [Select] $en['mail']['letter closing'] = "regards,\n your friend!"; and in my config: Code: [Select] $language = "en"; $include_language = @include("languages/".$language.".php"); if(!($include_language)) { $try_default_language = @include("languages/nl.php"); if(!($try_default_language)) { echo "kan de taalpakket niet vinden<br>"; echo "Could not find the language pack.<br>"; echo "example on error: ".$test." shows nothing"; exit; } } In my function I want to include the language pack for example i have $language = 'en' so I want to include $en['general']['letter closing'] I will do this: Code: [Select] global $language,${$language}['general']; But that gives an error unexpected '[' blah blah. How can i call the variable variable array in the valid php way? Similar TutorialsCode: [Select] <? $i=1; $analysts[$i] = array(0=>'alpha'); $regions[$i] = array(0=>'beta'); $countries[$i] = array(0=>'gamma'); $provinces[$i] = array(0=>'delta'); $events[$i] = array(0=>'epsilon'); $selectable_document_elements = array ( 0=> analysts, 1=> regions, 2=> countries, 3=> provinces, 4=> events ); foreach($selectable_document_elements as $value) { echo $value; print_r($$value[$i]); echo "</br>"; } ?> I can't seem to call $$value[$i], however when I leave off the [$i] part Code: [Select] print_r($$value); I get the individual elements... Code: [Select] analystsArray ( [1] => Array ( [0] => alpha ) ) regionsArray ( [1] => Array ( [0] => beta ) ) countriesArray ( [1] => Array ( [0] => gamma ) ) provincesArray ( [1] => Array ( [0] => delta ) ) eventsArray ( [1] => Array ( [0] => epsilon) ) However, I would like to get the 1 part of the array, in this example. Is there a magic trick to calling variable arrays' individual elements? My login script stores the user's login name as $_SESSION[ 'name'] on login. For some unapparent reason, i'm getting errors stating that $user and $priv are undefined variables, though I've attempted to define $user as being equal to $_SESSION['name'], using $user to look up the the user's privilege level (stored as the su column ) in the SQL table, and then where the result of the sql query is $priv which is then evaluated in an if statement. I can't seem to figure out why this might not be working. The code I'm using: <?php session_start(); function verify() { //verify that the user is logged in via the login page. Session_start has already been called. if (!isset($_SESSION['loggedin'])) { header('Location: /index.html'); exit; } //if user is logged in, we then lookup necessary privleges. $_SESSION['name'] was written with the login name upon login. Privleges // are written in db as a single-digit integer of of 0 for users, 1 for administrators, and 2 for special users. $user === $_SESSION['name']; //Connect to Databse $link = mysqli_connect("127.0.0.1", "database user", "password", "database"); if (!$link) { echo "Error: Unable to connect to MySQL." . PHP_EOL; echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; exit; } //SQL Statement to lookup privlege information. if ($result = mysqli_query($link, "SELECT su FROM accounts WHERE username = $user", MYSQLI_STORE_RESULT)) { //LOOP TO CYCLE THROUGH SQL RESULTS AND STORE Privlege information as vairable $priv. while ($row = $result->fetch_assoc()) { $priv === $row["su"]; } } // close SQL connection. mysqli_close($link); // Verify privleges and take action. Only a privlege of "1" is allowed to view this page. A privlege of "2" indicates special //accounts used in other scripts that have certain indermediate additional functions, but are not trusted administrators. if ($priv !== 1) { echo $_SESSION['name']; echo "you have privlege level of $priv"; echo "<br>"; echo 'Your account does not have the privleges necessary to view this page'; exit; } } verify(); ?>
Hi guys im in the middle of optimizing code.. Code: [Select] $sql = "SELECT * FROM sa_enemystats WHERE username='$username'"; $res = mysql_query($sql); while ($row = mysql_fetch_assoc($res)) { $enemy['current_health'] = $row['current_health']; $enemy['current_skill'] = $row['current_skill']; $enemy['level'] = $row['level']; $enemy['damage'] = $row['damage']; $enemy['evade'] = $row['evade']; $enemy['accuracy'] = $row['accuracy']; $enemy['speed'] = $row['speed']; $enemy['luck'] = $row['luck']; echo 'debug: variables synced with db table'; }I know that the setting of these variables are messy and can be done in a better way... but how? I have tried foreach and copying other's code for an array copy but it lead no where. Then again my syntax could be wrong... I used something like Code: [Select] foreach ( $enemy[$value] as $row => $value) { $enemy[$value] = $row[$value]; } Hi I am buidling a Blog System using OO techniques (or at least attempting to).. I keep getting an error : Notice: Undefined variable: blog_categoryName in C:\wamp\www\atkinsonsCMS\admin170976\blog_categories.php on line 13 ...And then underneath all that is is displaying 'Call Stack'. I've never come across this notification before so I guess it is an OO term, and the a list of unexpected outputs: #, time, memory, function and location....each with an associated value. I attempted to resolve the variable issue in the usual way (as you can see in my code), but still the errors/warnings persist. The purpose of this part of the code is to simply retrieve SQL data (which is a list of blog categories), allowing the user to select one and then click through to be directed to the appropriate category page (although this last requirement of the code has not yet been programmed). Could those who have experience of this please take a look at my code and let me know where I am going wrong? Thanks blog_categories.php This file calls on the the blogFunctions.php Category Class, and the displays the results. session_start(); $pageTitle = "Blog Categories"; include("../includes/admin_header.php"); include("../classes/blogFunctions.php"); echo '<div class="admin_main_body"><br /><br />'; echo "<h2>Category List</h2><br />"; $content = new Category; $content -> process(); //print_r(debug_backtrace()); echo "<a href=blog.php>$blog_categoryName</a>"; echo '</div>'; echo '</div>'; include("../includes/admin_footer.php"); blogFunctions.php This class deals with the retreaval of the SQL data, and then passes it to blog_categories.php class Category { public $errors; public $blog_categoryName = NULL; public function __construct() { $this->errors = array(); $blog_categoryName = $this->blog_categoryName; } public function getCategories(){ if(!isset($blog_categoryName)) { $blog_categoryName = 'value'; } ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "atkinson", "XYZ111WA")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE atkinsonscms")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM blog_category ORDER BY ASC"); $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); if ($result) { while ($row = $result->fetch_object()) { $blog_categoryName = $row->blog_categoryName; } //need to use mysqli query to retrieve results. if(mysqli_affected_rows($GLOBALS["___mysqli_ston"])< 1) $this->errors[] = 'Could not retrieve data'; } } public function show_errors(){ echo "<h3>Errors</h3>"; foreach($this->errors as $key=>$value) echo $value."<br>"; } public function process() { $this->getCategories(); return count($this->errors)? 0 : 1; } } //Closes Category Class. Hello, I have the situation shown below in the code: if(//condition is met) { $val = new Val(); $valon = $val->check($_POST, array(.........)); } if($valon->passes()) { $use = new Use(); ........ } //check() and passes() both belong to class Val public function check($source, $items = array()) { ...................... return $this; }I get the following warning and error: Notice: Undefined variable: valon and Fatal error: Call to a member function passes() on a non-object I can't see anything wrong with the code. check() returns an object of class Val which is assigned to $valon, $valon then calls a member function of class Val. Could you please help. Please let me know whether to include further code. Code: [Select] $word = 'numbers'; $numbers= array('1', '2', '3', '4'); echo $$word[0]; I expected the output to be '1'. It ended up being nothing... Why does this not work? Is it not possible to have a variable variable array? And if not, is there a workaround? Cheers, Joe Where is my "Progr" return value?
This is not my first day using php but I don't code in the language that often. I don't understand why my dynamic variable is not returned in AJAX GET? Essentially, I need to give the client a simple UI so that they can copy files to a printer and I wanted to provide a little unfiltered feedback so they knew whether operation was a success. My feedback was just going to be output of exec() function though cleaned up slightly (perhaps replace line breaks with html break tags). PHP version is 5.2.17.17 and cannot be upgraded. Web Server Apache.
PHP dumbed down code:
exec("LoadZPL_PURL.bat", $output); I need a help in the following : I have an admin page from where the admin attaches an gif image.This attached image should be shown to the user as a scrolling image. The code for the scrolling image is done through javascript. So my actual problem is getting the name of the attached gif image to the javascript array which is used for scrolling horizontally. Hello all. I am very new to PHP, and I am not sure where to look or what I'm looking for in my current assignment. My task is to take in two numbers between 0-100. Once I take in that number, it should state beside it "The __ was accepted." The program should not accept any numbers greater than 100 or any characters. Once I do this, I must take a second number and do a similar thing. Finally, I must have a statement show up at the bottom stating which number is greater. Essentially, I need help in determining what I should use to place parameters, and how I can keep the program from echo ing any statement until input has been taken and tested for parameters. Any help you can provide will be greatly appreciated! I am trying to allow the user to update a variable he chooses by radio buttons, which they will then input text into a box, and submit, to change some attributes. I really need some help here. It works just fine until I add the second layer of variables on top of it, and I can't find the answer to this question anywhere. <?PHP require('connect.php'); ?> <form action ='' method='post'> <select name="id"> <?php $extract = mysql_query("SELECT * FROM cars"); while($row=mysql_fetch_assoc($extract)){ $id = $row['id']; $make= $row['make']; $model= $row['model']; $year= $row['year']; $color= $row['color']; echo "<option value=$id>$color $year $make $model</option> ";}?> </select> Which attribute would you like to change?<br /> <input type="radio" name="getchanged" value="make"/>Make<br /> <input type="radio" name="getchanged" value="model"/>Model<br /> <input type="radio" name="getchanged" value="year" />Year<br /> <input type="radio" name="getchanged" value="color" />Color<br /><br /> <br /><input type='text' value='' name='tochange'> <input type='submit' value='Change' name='submit'> </form> //This is where I need help... <?PHP if(isset($_POST['submit'])&&($_POST['tochange'])){ mysql_query(" UPDATE cars SET '$_POST[getchanged]'='$_POST[tochange]' where id = '$_POST[id]' ");}?> I have just re-installed Xampp and suddenly my sites are now displaying lots of: Notice: Use of undefined constant name - assumed 'name' in ... Notice: Use of undefined constant price - assumed 'price' in ... this is an example of the line its refering too: $defineProducts[1001] = array(name=>'This is a product', price=>123); Hi, I need help with this, I am quite new to arrays. This one works fine: echo array_sum(array(34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7))/count(array(34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7)); which gives "19..." it gets average number. What I want to put the values in the array in a variable so I do this: $t = "34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7"; so why can't I use array like this: echo array_sum(array($t))/count(array($t)); I need to use variable in array, please someone help. Hello Guys. I have a question here.. I have a photo upload script that uploads photos to the server and then sends the photo name to another page via POST. I only have one var name called $image_all when I echo it this is what I get (When uploading 2 files) Code: [Select] This is the list 2011-12-05_0751.png,2011-12-05_0747.png I also tested with "explode" Code: [Select] print_r(explode(',', $image_all, 10)); and I get this Code: [Select] Array ( [0] => 2011-12-05_0751.png [1] => 2011-12-05_0747.png [2] => What I would like for it to do is assign an incremental var name to each image uploaded and passed in the POST. Like this $image1 = 2011-xx.png $image2 = 2011-xx.png $image3 = 2011-xx.png It should created the variable names dynamically based on the number of images (Up to 10) Thanks for your help! Hello. Can someone show the syntax to echo the variable from the array below that is showing "2010-08-31 23:19"? I need to grab that date and use it elsewhere in my script. Thanks. Code: [Select] array(19) { ["name"]=> string(8) "com_jumi" ["params"]=> array(0) { } ["mainframe"]=> &object(JSite)#2 (6) { ["_clientId"]=> int(0) ["_messageQueue"]=> array(0) { } ["_name"]=> string(4) "site" ["scope"]=> string(8) "com_jumi" ["_errors"]=> array(0) { } ["requestTime"]=> string(16) "2010-08-31 23:19" } Is this right? $admin = array('24.68.200.235','24.68.214.97'); $visitor_ip = $_SERVER['REMOTE_ADDR']; if($visitor_ip == $admin) { echo "k"; } else { die("dead"); hello all, I have an array value in my post array ($_POST['check0'] ) that I am trying to break up into php variables. For some reason if I do this: $var1 = $_POST['check0'][0]; all I get is the first letter of the string from the first value of the array? Hi Everyone
Please checkout the below code:
foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; $get_available_towns = array(); }
foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; $get_available_towns = array(); }I don't really understand what is happening. I know the code and everything is correct and it works but I really don't understand how the loop, arrray and variable is working. I wanted to print_r the $get_available_towns outside the loop but it would not work without the square brackets but also I did not even know the square brackets were required. The point was to loop through the data and print all the values. I understand how loops, array's and variables work but I've not seen them used like this. This makes me think I am missing some basic knowledge but I have looked around and I can't see any examples of how loops and arrays are used like this. I have also checked the php manual but still no joy. Can you guys tell me where I could find the information that would have examples on how something like the above code would work? Hey, im struggeling with a small piece of code.... Thats my code: Global $user_ID; $user_ID = get_current_user_id(); echo"User number $user_ID is loggedin";
Echo gives me this:
Now i want to add it to my array: $atts['href'] .= $user_ID . 'abc'; 'href' is set to "https://test.com/" What im getting now ist https://test.com/abc
Can someone tell my why the '2' is missing??
Hi, I am basically trying to apply a different amount to $defineMe for certain numbers of an array .. in this case it's array numbers 11 - 16 Code: [Select] <?php if ($defineNumber > 11 and $defineNumber < 16) { $defineMe = 35.00; } else { $defineMe = 50.00; } $defineNumber[01] = array('name'=>'Product 1'); $defineNumber[02] = array('name'=>'Product 2'); $defineNumber[03] = array('name'=>'Product 3'); $defineNumber[04] = array('name'=>'Product 4'); $defineNumber[05] = array('name'=>'Product 5'); $defineNumber[06] = array('name'=>'Product 6'); $defineNumber[11] = array('name'=>'Product 11'); $defineNumber[12] = array('name'=>'Product 12'); $defineNumber[13] = array('name'=>'Product 13'); $defineNumber[14] = array('name'=>'Product 14'); $defineNumber[15] = array('name'=>'Product 15'); $defineNumber[16] = array('name'=>'Product 16'); ?> Hi ! I'd like to count all occurences in "$thiscodestring" into a "dynamic?" array and print it out after the end of the loop, how do I assign the variablenames dynamically ? $query = "SELECT codes, status FROM table"; while ($row = mysql_fetch_assoc($result)) { $thiscodestring = $row[codes]; // looks like "a1 c2 d3 10 15 1 a1 a1"; $singlecodes = explode(" ", $thiscodestring); foreach($singlecodes as $thiscode) { $$thiscode[count] = $$thiscode[count] + 1; } } After all Mysql-Stuff is done I'd like to print out the counted codes, for example the code "a1" was found 100 times in all querys then $$a1[count] should be 100, c2 was found 15 times, then $$c2[count] should be 15 and so on. Question: How do I tell php to assign a "dynamic" variable with the name of the found string which was exploded (in this example a1, c2, d3... ) before ?! $$ does not seem to work. And how can I then print_r() the value if I dont know the name ? Thanks ! |