Kryllster Posted September 4, 2009 Share Posted September 4, 2009 Here is a block of code that returns the correct name of the configured type of weapon Im using! $xml = simplexml_load_file('weapondata.xml'); $weapon_array = array(); foreach($xml->attack as $attack){ $name = $attack->name; $bonus = $attack->basebonus; $weapon_array["$name"] = $bonus; } if(in_array($weapon,$weapon_array)){ $weapon = $bonus; exit(); } it will not print out the numerical value assciated with the $weapon_array array. Once again Im baffled! Quote Link to comment https://forums.phpfreaks.com/topic/173071-solved-xml-problem-with-numbers/ Share on other sites More sharing options...
JonnoTheDev Posted September 4, 2009 Share Posted September 4, 2009 This is beacuse $bonus is an object. Cast it to an integer when adding to the array: <?php foreach($xml->attack as $attack){ $name = (string)$attack->name; $bonus = (int)$attack->basebonus; $weapon_array[$name] = $bonus; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/173071-solved-xml-problem-with-numbers/#findComment-912294 Share on other sites More sharing options...
Kryllster Posted September 4, 2009 Author Share Posted September 4, 2009 Thanks for the reply here is the code as it stands now: $xml = simplexml_load_file('weapondata.xml'); $weapon_array = array(); foreach($xml->attack as $attack){ $name = (string)$attack->name; $bonus = (int)$attack->basebonus; $weapon_array[$name] = $bonus; } if(in_array($weapon,$weapon_array)){ $weapon = $bonus; exit(); } And here is the xml I am using: <?xml version='1.0' standalone='yes'?> <attacks> <attack> <name>Eastwind Katana</name> <basebonus>2</basebonus> </attack> <attack> <name>Robin Bow</name> <basebonus>2</basebonus> </attack> <attack> <name>Blue Ice Staff</name> <basebonus>2</basebonus> </attack> <attack> <name>Aldonian Spatha</name> <basebonus>4</basebonus> </attack> <attack> <name>Cobalt Bow</name> <basebonus>4</basebonus> </attack> <attack> <name>Fire Staff</name> <basebonus>4</basebonus> </attack> <attack> <name>Hitzman Claymore</name> <basebonus>6</basebonus> </attack> <attack> <name>Fire Bow</name> <basebonus>6</basebonus> </attack> <attack> <name>Plaz Staff</name> <basebonus>6</basebonus> </attack> <attack> <name>Big Club</name> <basebonus>1</basebonus> </attack> </attacks> When I echo out the bonus I get the value of 1 where it should be 2 as the character has a Eastwind Katana <?php echo $bonus; ?> value returns as 1 Don't know where to take this from here I feel as if I'm way out of my league on this!! Quote Link to comment https://forums.phpfreaks.com/topic/173071-solved-xml-problem-with-numbers/#findComment-912318 Share on other sites More sharing options...
JonnoTheDev Posted September 4, 2009 Share Posted September 4, 2009 print the array at the end of the loop to check that the correct data exists <?php $weapon_array = array(); foreach($xml->attack as $attack){ $name = (string)$attack->name; $bonus = (int)$attack->basebonus; $weapon_array[$name] = $bonus; } print_r($weapon_array); exit(); ?> Looks fine to me Array ( [Eastwind Katana] => 2 [Robin Bow] => 2 [blue Ice Staff] => 2 [Aldonian Spatha] => 4 [Cobalt Bow] => 4 [Fire Staff] => 4 [Hitzman Claymore] => 6 [Fire Bow] => 6 [Plaz Staff] => 6 [big Club] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/173071-solved-xml-problem-with-numbers/#findComment-912330 Share on other sites More sharing options...
KevinM1 Posted September 4, 2009 Share Posted September 4, 2009 Why are you explicitly exiting the script, and where are you attempting to echo the bonus value? Quote Link to comment https://forums.phpfreaks.com/topic/173071-solved-xml-problem-with-numbers/#findComment-912359 Share on other sites More sharing options...
Kryllster Posted September 4, 2009 Author Share Posted September 4, 2009 Well after I got some sleep I realized I haven't been telling it which weapon to use as the key value. I just assumed it would pick that up from the database where the name of the weapon and weapon bonus are. I do have those higher up in the script. I'm using a case switch block as a controller file. here is the whole case block for that script and where I am echoing the value. case 'fight_view3': //create a variable to store the xml data you're going to import from the file (in the same dir. as your script): $xml = simplexml_load_file('weapondata.xml'); $weapon_array = array(); foreach($xml->attack as $attack){ $name = (string)$attack->name; $bonus = (int)$attack->basebonus; $weapon_array[$name] = $bonus; } if(in_array($weapon,$weapon_array)){ $bonus = $bonus + $weapon_bonus; exit(); } include 'fight_view.php'; break; I am echoing it in fight_view.php as to why Im exiting I think I just did that to see if that would help. The value $weapon is from another config where I have that in. $weapon = $row['weapon'] So how do I tell the script to associate the value of $weapon which should be the same as $name in the other so that it will use the basebonus which is $bonus? It seems to me thats where the problem is (well besides my pea brain attempt at php) Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/173071-solved-xml-problem-with-numbers/#findComment-912711 Share on other sites More sharing options...
KevinM1 Posted September 4, 2009 Share Posted September 4, 2009 I think you need to take a step back and look at your project with fresh eyes. Remember what I said earlier, about using the best tool for the job? I can't help but feel you're trying to use a hammer to insert a screw. XML is just one tool available for you to use. Don't think of it as a cure-all, or a replacement for the database. Just so the rest of us get an idea as to what your overall design is, as well as to spur you to think about your design critically, here are some questions: What's the difference between the weapon data stored in the database and the data stored in the XML file? Do you need this data stored in two different places in two different ways? What are all these various config files for? Can you combine/streamline them? What are you trying to do here, specifically? Obviously, you're attempting to return weapon data, but for what purpose? Is this part of your combat system? Are you merely trying to give a character a new weapon as loot? Like I said earlier how you intend to use this data is an important consideration when deciding how to store it. It seems like this is an opportune time to really think about this before moving on. Quote Link to comment https://forums.phpfreaks.com/topic/173071-solved-xml-problem-with-numbers/#findComment-912773 Share on other sites More sharing options...
Kryllster Posted September 4, 2009 Author Share Posted September 4, 2009 I have been thinking the same thing. I have 1 part of the data in a database which is $weapon which has the Name of "Eastwind Katana" it was "Big Club" But I bought the new weapon at a shop hence the name change, This is reflected correctly on my player_view.php which shows the correct value. So Im trying to mix the 2. Database and xml file and it is part of a combat system I have 2 but they are rather static I was hoping to make it more dynamic. So I have been thinking of a total rewrite for lots of reasons but this is getting nowhere at the moment. I cant get the $weapon and $name in the xml files to match as well as the $bonus they either 1 come up as Big Club or the value of Big Club which is 1 so it is reading the xml file but not matching when I compare them. I will wait to see if there is a solution or if I should just do a rewrite. Thanks for all who have been trying to help I have learned alot but have more to learn. 1 more thing I would like to add is I'm a retired veteran with a disability which is schizophrenia so that makes it hard too for me but this is kind of therapeutic too and helps with my illness and thinking. Here is my game so far: http://crimson.servegame.org/ Quote Link to comment https://forums.phpfreaks.com/topic/173071-solved-xml-problem-with-numbers/#findComment-912783 Share on other sites More sharing options...
KevinM1 Posted September 5, 2009 Share Posted September 5, 2009 There are two acronyms that every programmer should try to adhere to: KISS - everyone knows this one. DRY - Don't Repeat Yourself. Essentially, if you're repeatedly writing the same block of code, or repeatedly have to address a certain problem, your design is off somewhere. Storing the same weapon data in two different locations and with two different techniques is a clear case of this kind of design problem. With what you're trying to do, a database is the way to go, as you'll need a way to tie various items to characters. That kind of relationship is difficult, if not impossible, to maintain in XML. XML is better suited for static data whose relationship with other system components is also static. Example: character class info from, say, World of Warcraft. A Hunter - its abilities, leveling stats, types of weapons and armor it can use - remains the same regardless of what race the character is, or faction, or gender. This is the kind of data that benefits from being modeled in XML. You also should be doing only what is absolutely necessary to access and manipulate the data. Just like with machinery, the more 'moving parts' the greater the chance of failure. Keep at it. The knowledge you're gaining with this project is invaluable, not just in terms of game making, but programming in general. As an aside, I understand where you're coming from, at least partially. I'm also disabled. I was born with a physical disability, have had over 40 surgeries because of it, and require an electric wheelchair for mobility. The internet is the great equalizer - a place where everyone can be a participant, and where its inherent anonymity means our ideas can be judged on merit and not prejudice. So, again, keep at it. You may also find that reading other threads on the forum to be beneficial. I learned the bulk of my PHP skill by reading other threads, trying to come up with solutions to other people's problems on my own, and hanging out in the Application Design sub-forum. Quote Link to comment https://forums.phpfreaks.com/topic/173071-solved-xml-problem-with-numbers/#findComment-912906 Share on other sites More sharing options...
Kryllster Posted September 5, 2009 Author Share Posted September 5, 2009 Thank you for your kind words of encouragement. I will keep at it and have already realized that part of my major problem was in planning. This project is an off the cuff deal and basically no planning so that is where I'll start. Redisgn and reconstruct it taking a little more time to get things right. I will mark this topic as solved. Quote Link to comment https://forums.phpfreaks.com/topic/173071-solved-xml-problem-with-numbers/#findComment-912921 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.