PHP - New To Php & Exploring The Language, Hit A Snag With Multidimentional Arrays!
Good evening all,
I am trying to expand my experience with PHP by doing a small project for a personal website. I am working from scratch using notepad and so far have been having great sucess. My experience with C# is helping greatly but sadly ive hit a snag and can't seem to free myself. I have an array that is populated with items, and the prices for those items if you are buying/selling them. What I will have is a form with a drop list that is populated by this array list (already working) and a text entry that you can enter a numerical quantity value, then another drop list that will have buy / sell as an option. What the page will do is take the values from the form and calculate the "total" from the supplied values. Sadly some reason I cant set a $Variable to the value of the array values. I can echo them and get the correct value, but can't set the Variable to the arrays value! Code: [Select] <?php $ResBuySell = array( "Iron ingot" => array("Buy" => 6, "Sell" => 7), "Shadow iron ingot" => array("Buy" => 14, "Sell" => 16), "Dull copper ingot" => array("Buy" => 14, "Sell" => 16), "Copper ingot" => array("Buy" => 6, "Sell" => 7), "Bronze ingot" => array("Buy" => 6, "Sell" => 7), "Gold ingot" => array("Buy" => 6, "Sell" => 7), "Agapite ingot" => array("Buy" => 6, "Sell" => 7), "Verite ingot" => array("Buy" => 6, "Sell" => 7), "Valorite ingot" => array("Buy" => 6, "Sell" => 7), "Feathers" => array("Buy" => 6, "Sell" => 7), "Arrows" => array("Buy" => 6, "Sell" => 7), "Bolts" => array("Buy" => 6, "Sell" => 7), "Boards" => array("Buy" => 4, "Sell" => 6), "Spined leather" => array("Buy" => 16, "Sell" => 18), "Horned leather" => array("Buy" => 6, "Sell" => 7), "Barbed leather" => array("Buy" => 6, "Sell" => 7), "Cloth" => array("Buy" => 6, "Sell" => 7), "Ribs" => array("Buy" => 6, "Sell" => 7), "Fish steaks" => array("Buy" => 6, "Sell" => 7), "Orange petals" => array("Buy" => 6, "Sell" => 7), "Red leaves" => array("Buy" => 6, "Sell" => 7), "Green thorns" => array("Buy" => 6, "Sell" => 7) ); function GetPrice($ResType = "Iron ingot", $TranType = "Buy") { return $ResBuySell[$ResType][$ResType]; } function GetQuantity() { return 5; } function GetTotal() { $ResPrice = GetPrice(); $ResQuanity = GetQuantity(); $ResTotal = $ResPrice * $ResQuanity; return $ResTotal; } echo GetTotal(); ?> Quote 0 The problem resides he Code: [Select] function GetPrice($ResType = "Iron ingot", $TranType = "Buy") { return $ResBuySell[$ResType][$ResType]; } Some reason when I echo this: Code: [Select] $ResType = "Iron ingot"; $TranType = "Buy"; echo $ResBuySell[$ResType][$ResType]; it will return; Quote 6 The proper value, but when I put it into a function and try to retrieve this info via the function it returns zero. I am a beginner with php and just trying to learn by trial and error, but after failing at this for 5 days I figured it was time to call in help from the big guns! Thanks in advance to anyone who can offer my any assistance with this problem! Similar TutorialsHow do I echo the array below in a for each loop? I'm trying to output this data in a table. Array ( [u] => Array ( [0] => Array ( [row_id] => 1 [0] => Jn [1] => Bo [2] => do [3] => jilcom [4] => hocom ) [1] => Array ( [row_id] => 3 [0] => Jk [1] => Kk [2] => wd [3] => jk [4] => ) [2] => Array ( [row_id] => 5 [0] => Jn [1] => Bo [2] => on [3] => jm [4] => hm ) ) [blog] => Array ( [0] => Array ( [row_id] => 3 [0] => dk [1] => [2] => [3] => [4] => ) [1] => Array ( [row_id] => 9 [0] => 2rs [1] => dee [2] => [3] => [4] => ) ) ) In case you guys don't know what lisp is, tl;dr is that is an old language without syntax that uses polish notation and parentheses to represent all language constructs. You can also create your own language constructs. For example, this mysql statement : UPDATE table_name SET column1=value1,column2=value2 WHERE some_column=some_valueCan be represented as (update "table_name" (set ('column1 "value1") ('column2 "value2") (where (= some_column "some_value")))One advantage of this whole thing is that you don't have to mix multiple languages to create a site. When you program in php, you actually use html, css, php and MySQL. When you program in lisp, you only use lisp. Another advantage is that the language with no syntax is easier to programaticaly generate than one with it. I can auto generate various sql statements as lisp trees. The disadvantage is that lisp is obscure as hell and no one uses it. So I was thinking of using php arrays to get some of that expressiveness of lisp in PHP and to abstract away necessity to glue together sql strings. So far it has been mixed results. One way to represent update is this ["update", "table_name", ["column1" => "value1", "column2" => "value2"], ["where", [ "=", "some_column", "some_value"]]]I represent columns to be updated as key=>value pairs since column names are unique anyway. One advantage of this method is that I don't have to use mysql_real_escape_string on variables for updating. My interpreter automatically escapes everything inside SET bracket and adds quotes to values. This is however, less than practical in its WHERE part. That's because it is less clear what part you can and can't escape, since column names, functions and values can all be mixed up in WHERE part, and arrays are not nearly as flexible as s-expressions. Anyone has any idea how best to do this? Edited by Goat, 24 July 2014 - 02:46 PM. Hi Guys, PHP has never been my fortay and I have hit a real snag. Please find below code for a simple PHP contact form. However, in the subject line of the email sent to me I would like it to show: Contact Form: $subject - ($subject is what is currently displayed). I know this is a simple fix however I have been messing about for hours and can't seem to get it right. Any help would be greatly appreciated. Best, Scott. Code: [Select] <?php define("WEBMASTER_EMAIL", 'contact@pearceresourcing.com'); error_reporting (E_ALL ^ E_NOTICE); ////////////////////////////////////////////////////// function ValidateEmail($email) { $regex = '/([a-z0-9_.-]+)'. # name '@'. # at '([a-z0-9.-]+){2,255}'. # domain & possibly subdomains '.'. # period '([a-z]+){2,10}/i'; # domain extension if($email == '') return false; else $eregi = preg_replace($regex, '', $email); return empty($eregi) ? true : false; } ////////////////////////////////////////////////////// $post = (!empty($_POST)) ? true : false; if($post) { $name = stripslashes($_POST['name']); $email = trim($_POST['email']); $subject = trim($_POST['subject']); $message = stripslashes($_POST['message']); $error = ''; // Check name if(!$name) $error .= 'Name required! '; // Check email if(!$email) $error .= 'E-mail required! '; if($email && !ValidateEmail($email)) $error .= 'E-mail address is not valid! '; // Check message if(!$message) $error .= "Please enter your message!"; if(!$error) { $mail = mail(WEBMASTER_EMAIL, $subject, $message, "From: ".$name." <".$email.">\r\n" ."Reply-To: ".$email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) echo 'OK'; } else echo '<div class="errormsg">'.$error.'</div>'; } ?> Hello every body i have a array like this
Array ( [19/5/14] => Array ( [94-DE-80-28-CF-BC] => Array ( [7] => Array ( [qax] => 1 ) ) } }I want to add new array under the [94-DE-80-28-CF-BC] => Array.. I am doing code but what it does it overrides the full array inside that array and create a new one... The code i've used is | array_push( $vis[$date][$macp], array( "$id"=>array( "$country"=>1 ) )); I've got a few arrays as follows: Code: [Select] $array1 = ('A', 54, 'a', 2); $array2 = (, 67, 'b',); $array3 = ('R', 54,,); $array4 = (, 36, 'b'); $array5 = (, 36, 'a',); $array6 = ('FX', 119, 's', 4); $array7 = ('R', 3, 'c',); $superarray = array($array1, $array2, $array3, $array4, $array5, $array6, $array7); Now, I've got a piece of code that sorts $superarray by index[1] of the sub-arrays. The code looks like this: Code: [Select] function sortmulti ($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) { if(is_array($array) && count($array)>0) { foreach(array_keys($array) as $key) $temp[$key]=$array[$key][$index]; if(!$natsort) { if ($order=='asc') asort($temp); else arsort($temp); } else { if ($case_sensitive===true) natsort($temp); else natcasesort($temp); if($order!='asc') $temp=array_reverse($temp,TRUE); } foreach(array_keys($temp) as $key) if (is_numeric($key)) $sorted[]=$array[$key]; else $sorted[$key]=$array[$key]; return $sorted; } return $sorted; } $sortedarray = sortmulti($superarray, 1, ASC, $natsort=FALSE, $case_sensitive=FALSE) The output is this: R3c 36b 36a A54a2 R54 67b FX119s4 This is great because it goes 3 -> 36 -> 36 -> 54 -> 54 -> 67 -> 119. However, the problem, if you look, is this part: 36b 36a Its sorted everything according to index[1] of the sub-arrays, but now I would like to sort by index[2], then index[3] as well. I'd like the output to look like this: R3c 36a 36b A54a2 R54 67b FX119s4 Any ideas? Because this is way over my head ... this is an array returned after parsing YAMl file using spyc api. i want to write a code to to extract value related to key topic name and put in a separate 1d array say $topicnames Array ( [1] => Array ( [topicname] => singapore travels [rev] => 250 => Array ( [25] => Array ( ) [topicname] => airlines [rev] => 24 ) [1] => Array ( [35] => Array ( ) [topicname] => shubham [rev] => 65 ) ) [2] => Array ( [topicname] => singapore entertainment [rev] => 210 => Array ( [45] => Array ( ) [topicname] => night safari[rev] => 54 ) i tried using simple for loop but not working.. pl help me out. Hello everyone For a multilingual website we have to be able to open 1 page in 4 different languages. For example, product 'A' is in Dutch, English, French and German. How can I open the right language file? At the moment I use Code: [Select] http://www.zinncosmetics.nl/shop/product_info.php?products_id=36?language=de but it doesn't work. It allways opens the default Dutch pages. What do I do wrong? I don't have very much php experience (newbie) so please don't make it too difficult for me. Thanks for all help! I'm having troubling with trying to create a function to spit out a single array with the following array. I can write it in a away that looks through the arrays manually. the results i am trying to generate is that each item in generated array. Array to convert array( "account" => array( "login", "register", "logout", "edit", ), "p" => array( "report", ), "array1.0" => array( "array2.0" => array( "array3.0", "array3.1 ), "array2.1", ), generating the array will look like this
Array ( [0] => account [1] => account/login [2] => account/register [3] => account/logout [4] => account/edit [5] => p [6] => p/report [7] => array1.0 [8] => array1.0/array2.0 [9] => array1.0/array2.0/array3.0 [10] => array1.0/array2.0/array3.1 [11] => array1.0/array2.1 ) The idea is that id generates a single array with combined labels and arrays inside, etc. I just can't figure out how to create a script that will create this array even If I add a new value or array.
I have this thing that i am trying to make but i cant get it to work.. can anyone help? Code: [Select] function sql_read( $dbname,$dbusername,$dbpassword ) { $names = array(); $password = array(); $connect = @mysql_connect("mysql11.000webhost.com",$dbusername,$dbpassword) or die("Could Not Connect"); @mysql_select_db ($dbname) or die("Could not find DataBase"); $query = mysql_query("select * from users"); $numrows = mysql_num_rows($query); if ($numrows > 0){ while($row = mysql_fetch_assoc($query)){ $names[] = $row["uname"]; $password[] = $row["password"]; $id = $row["id"]; } $return = array($names,$password,$id); }else{ $return = array(); } return $return[]; } $names = array(); $names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0]; The error i get is Code: [Select] Parse error: syntax error, unexpected '[' in /home/a5480952/public_html/sql/index.php on line 28 Line 28 is "$names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0];" Please help... i REALLLLD need help with this.. ask questions if you want to know more about what i am trying to do... thanks! i created a insert php code which successfully submit to database but i was lost at some point because what i really wanted was a php code that will print on the next page after submission to db This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=321347.0 I have textfield for signature in my form, I m checking if the signature was sent ( if(!empty($_POST['signature'])) and if it's not empty I m putting it into the db. The problem is that, I have noticed a greek user has entered something in greek letters, and function could not deal with it and saved it as it is.( DB field supports greek charset ) I just want to allow english and german chars with special chars ( ",',< etc. ) , no other charset like greek. How can I do that? any function for this purpose ? Hi! I want to have a first page on my website where you as visitor can choose the preferred language. No login at all. I want to save this choice in a cookie or something. The easiest way So next time he/she visits, the language choice is already made for he/she. Any good suggestions for a php code that works? what's the best way to have a website in 2 languages? I already have the website duplicated in the 2nd language but I'm thinking on the best way to do the language change. should I just append variables to the URL when clicking on the flag icon on each page to change to the 2nd language? any other ideas?? Hi again PHP Freaks.
So I would like to code something similar to the buy menu of Counter-strike: Global offensive in-game module, just in a browser and for a project of mine. I've made the design in photoshop to illustrate better.
I just don't know what I should look into to make this kind of menu, I would love if someone could explain me what languages that this would require. (I'm guessing HTML, CSS and jQuery but I am not sure) more specific help like which particular codes would be much appreciated.
Here is a picture of what I would like to create. (Also attached)
http://i1227.photobu...zps4195288a.png
Attached Files
CSGOKeyz.png 149.81KB
0 downloads Hello, I am currently making a small website that requires two languages. This is the class I made: Code: [Select] <?php class languages { public function __construct() { if(!isset($_SESSION['language'])) { $_SESSION['language'] = 'nl'; } $lang = isset($_GET['lang']) ? $_GET['lang'] : ""; $languages = array('en', 'nl'); if(in_array($lang, $languages)) { $_SESSION['language'] = $lang; } } } $languages = new languages; $language = $_SESSION['language']; ?> Now as soon I use index.php?lang=en, the website will display it self in English but, as soon I remove the &lang=en from the URI, the language gets not stored in the session and it displays nl agian. Can somebody help me? Thank you! are all languages possible to be inserted into database and be retrieved directly?? eg: write down the japanese words in the textbox and INSERT those into table and retrieve it using mysql_fetch_array() Is that as simple as that, or i have to modify certain script...for certain language? thanks Hi, I am trying to append a language session like this: Code: [Select] $ATC_itemWeight = floatval("".$row_productos_RS['capacidad_'.$_SESSION['session_idioma'].'']; but I get this error Code: [Select] Parse error: syntax error, unexpected ';' in /usr/home/mysite.com/web/productos_esp.php on line 121 what am I doing wrong? Thanks At the minute i am using variables in my tpl files in place of plain text so users can change the language the page is shown in. eg. the tpl file would include: $lang->hello; and the replacement would be $lang['hello'] = 'hello'; the php file would include the language class which replaces the variable in the language file with the array value in the relevant language file. but what happens when i have a phrase with a needed variable in the middle? $lang['msgs'] = 'you have '.$msgs.' messages'; i cant have 'you have' and 'messages' as seperate lang variables as phrases are constructed differently in other languages? so how do i accomplish this? hopefully i habe explained this ok. I got site wirch I like tu turn to multilanguage. Code: [Select] <?=$home_jezik?>This is my variable for language code. So I wonna add it to Code: [Select] <?php echo $redak['novost_naslov_hr']; ?>so that "hr" change to "en" or any other language. I try with this but nothing happens Code: [Select] <?php echo $redak['novost_naslov_.$home_jezik']; ?> |