Advertisement
undefined0000

Element Namer

Feb 28th, 2012
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. <?php
  2. include('dbcon.php');
  3. $getions = mysql_query("SELECT * FROM ions");
  4. $elements = array();
  5. $charges = array();
  6. $ides = array("oxygen" => "oxide", "nitrogen" => "nitride", "flourine" => "flouride", "phosphorus" => "phosphide", "sulfur" => "sulfide", "chlorine" => "chloride", "arsenic" => "arsenide", "bromine" => "bromide", "iodine" => "iodide");
  7. function toIde($element) {
  8. global $ides;
  9. $out = $ides[$element];
  10. return $out;
  11. }
  12. while($result = mysql_fetch_array($getions)) {
  13.         array_push($elements,$result['name']);
  14.         array_push($charges, $result['charges']);
  15. }
  16. function getElementCharge($element) {
  17. global $elements, $charges;
  18.     $key = array_search($element, $elements);
  19.     $charge = $charges[$key];
  20.     return $charge;
  21. }
  22.  
  23. function nameCompound($element1, $element2) {
  24. global $elements, $charges;
  25. $arr = array($element1, $element2);
  26. $name1 ="";
  27. $name2="";
  28.     foreach($arr as $i) {
  29.     $charge = getElementCharge($i);
  30.     if($charge > 0) {
  31.     $name1 = $i;
  32.     }
  33.     else if($charge < 0) {
  34.     $name2 = $i;
  35.     }
  36.     }
  37.     return $name1 . toIde($name2);
  38. }
  39.  
  40. print nameCompound("oxygen","lithium");
  41. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement