PHP equivalent of this?

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
I have this calculator that I mocked up for a website, but I want to migrate it to a php server. It just reads some data from a text file, puts that into a hashmap along and performs some simple arithmetic on it based on the users input, is it possible to implement this in PHP?

Code:
<table border ="1">
	<tr><td>Mineral Name:</td><td>Mineral Quantity:</td><td>Corp Buyback Price:</td><td>Total:</td></tr>
<%
	HashMap<String, BigDecimal> map = new HashMap<String, BigDecimal>();
	
	String jspPath = session.getServletContext().getRealPath("/");
	String txtFilePath = jspPath + "/output.txt";
	
	File f = new File(txtFilePath);
	Scanner s2 = new Scanner(f);
	
	map.put("Tritanium", new BigDecimal(s2.nextLine()));
	map.put("Pyerite", new BigDecimal(s2.nextLine()));
	map.put("Mexallon", new BigDecimal(s2.nextLine()));
	map.put("Isogen", new BigDecimal(s2.nextLine()));
	map.put("Nocxium", new BigDecimal(s2.nextLine()));
	map.put("Zydrine", new BigDecimal(s2.nextLine()));
	map.put("Megacyte", new BigDecimal(s2.nextLine()));
	map.put("Morphite", new BigDecimal(s2.nextLine()));
	map.put("StrontiumClathrates", new BigDecimal(s2.nextLine()));
	map.put("Coolant", new BigDecimal(s2.nextLine()));
	map.put("EnrichedUranium", new BigDecimal(s2.nextLine()));
	map.put("HeavyWater", new BigDecimal(s2.nextLine()));
	map.put("HeliumIsotopes", new BigDecimal(s2.nextLine()));
	map.put("LiquidOzone", new BigDecimal(s2.nextLine()));
	map.put("MechanicalParts", new BigDecimal(s2.nextLine()));
	map.put("Oxygen", new BigDecimal(s2.nextLine()));
	map.put("Robotics", new BigDecimal(s2.nextLine()));
	map.put("BlueIce", new BigDecimal(s2.nextLine()));
	map.put("ThickBlueIce", new BigDecimal(s2.nextLine()));
	map.put("ClearIcicle", new BigDecimal(s2.nextLine()));
	map.put("EnrichedClearIcicle", new BigDecimal(s2.nextLine()));
	map.put("DarkGlitter", new BigDecimal(s2.nextLine()));
	map.put("GlacialMass", new BigDecimal(s2.nextLine()));
	map.put("SmoothGlacialMass", new BigDecimal(s2.nextLine()));
	map.put("GlareCrust", new BigDecimal(s2.nextLine()));
	map.put("Gelidus", new BigDecimal(s2.nextLine()));
	map.put("Krystallos", new BigDecimal(s2.nextLine()));
	map.put("WhiteGlaze", new BigDecimal(s2.nextLine()));
	map.put("PristineWhiteGlaze", new BigDecimal(s2.nextLine()));
	
	s2.close();
	
	String temp = "";
	String mineral = "";
	String inventory = "";
	BigDecimal quantity = new BigDecimal("0.0");
	BigDecimal total = new BigDecimal("0.0");
	
	inventory = request.getParameter("inventory");
	
	Scanner s = new Scanner(inventory);
	
	while(s.hasNextLine()) {
		temp = s.nextLine().replaceAll("[^a-zA-Z0-9]+", "");
		for(int i = 0; i < temp.length(); i++) {
			if(Character.isDigit(temp.charAt(i))) {
				mineral = temp.substring(0, i);
				for(int x = i; x < temp.length(); x++) {
					if(Character.isDigit(temp.charAt(x)) != true) {
						quantity = new BigDecimal(temp.substring(i, x));
						break;
					}
				}
				break;
			} 
		}
		
		if(map.containsKey(mineral)) { 
			total = total.add(map.get(mineral).multiply(quantity)); %>
			<tr><td><%= mineral %></td><td><%= quantity %></td><td><%= map.get(mineral) %> ISK</td><td><%= map.get(mineral).multiply(quantity) %> ISK</td></tr>
		<% }			
	}
	s.close(); %>
	<tr><td></td><td></td><td>Total</td><td> <%= total %> ISK</td></tr>
</table>
 
Last edited:
Thanks guys, I'm now reading the data from a mysql database I created rather than a text file which was suprisingly easy given how much code for that kind of thing always seems to be needed in Java.
 
Back
Top Bottom