PDA

View Full Version : Suggestions Welcomed


Sarcoth
08-07-2007, 06:19 PM
Hi all. I've been working on some new code for my forums and I'm close to done. I just finished testing my latest addition and it is working, but I was hoping to get some suggestions on it.

$find_monster = $test->db->query_first("SELECT m_equipment FROM test_monster where m_enc_loc='{$test->user['current_place']}'");

$find_equipment = $find_monster['m_equipment'];

$first_token = strtok($find_equipment, '{');
$second_token = strtok('}');

$full_equipment = explode(";", $second_token);

$equipment_head = $full_equipment[1];
$equipment_chest = $full_equipment[3];
$equipment_lhand = $full_equipment[5]; //shield
$equipment_rhand = $full_equipment[7]; //weapon
$equipment_feet = $full_equipment[9];

if ($equipment_lhand == 's:1:"8"') { $current_shield = "Wooden Shield"; $level_shield = 1; $cost_shield = "MAX"; }

// ################################################## ########################

if ($test->input['do'] == 'guard_shield') {
if ($equipment_lhand == 's:1:"0"') {
$full_equipment[5] = 's:1:"8"';
$rebuild_equipment = array($full_equipment[0], $full_equipment[1], $full_equipment[2], $full_equipment[3], $full_equipment[4], $full_equipment[5], $full_equipment[6], $full_equipment[7], $full_equipment[8], $full_equipment[9]);
$rebuild_mid = implode(";", $rebuild_equipment);
$rebuild_done = 'a:5:{' . $rebuild_mid . ';}';
$test->db->query_write("update test_monster set m_equipment='{$rebuild_done}' where m_enc_loc='{$test->user['current_place']}'");
}

$test->redirect($test->lang['purchased_land'], 'test.php?' . $test->system->systemvars['session'] . 'do=place&id=' . $test->user['current_place']);
}

I know it probably isn't the best way to accomplish my goals, but like I said...it does work. I'm just wondering if anyone can make suggestions on how I can make it better. I still have to add more if statements depending on many different variables.

Thanks!

nico_swd
08-08-2007, 11:30 AM
I'm under the impression that $find_monster['m_equipment'] is a serialized array. So you could do something like this to work way easier with it.

$find_equipment = unserialize($find_monster['m_equipment']);

echo '<pre>';
print_r($find_equipment);
echo '</pre>';

Opserty
08-08-2007, 11:45 AM
$rebuild_equipment = array($full_equipment[0], $full_equipment[1], $full_equipment[2], $full_equipment[3], $full_equipment[4], $full_equipment[5], $full_equipment[6], $full_equipment[7], $full_equipment[8], $full_equipment[9]);

Seems like a useless line really, $rebuild_equipment = $full_equipment would be the same, unless you have removed/added something to array which you are purposefully missing out.

Sarcoth
08-08-2007, 12:48 PM
I'm under the impression that $find_monster['m_equipment'] is a serialized array. So you could do something like this to work way easier with it.

$find_equipment = unserialize($find_monster['m_equipment']);

echo '<pre>';
print_r($find_equipment);
echo '</pre>';


I forgot to post that, yes it is a serialized array; I just read all about those yesterday after making this post. I've been trying to go the unserialize/serialize route, but I guess I just don't understand how to pull the data out individually when it is unserialized. Here is the serialized array.

a:5:{s:4:\"head\";s:1:\"0\";s:5:\"chest\";s:1:\"0\";s:5:\"lhand\";s:1:\"8\";s:5:\"rhand\";s:1:\"0\";s:4:\"feet\";s:1:\"0\";}

I actually had the same unserialize that you posted there, but I don't see how to view the data. In my top post, I use $full_equipment[1] to get my first value. The same doesn't work for the unserialized value: $find_equipment[1]. Can someone tell me how to get those?

@ Opserty - The rebuild_equipment only goes through if the person clicks on the button to upgrade their equipment "if ($test->input['do'] == 'guard_shield')". Once they do that, it will change one of the values, so I used the rebuild to put them all back together after the change occurs.

nico_swd
08-08-2007, 02:21 PM
You have to unescape the quotes.

$array = unserialize(stripslashes($array));



Also, if you don't get an array, set error reporting to E_ALL to see where the error lays.

error_reporting(E_ALL);


unserialize() will throw an error with the offset, so you can see what's wrong.

Opserty
08-08-2007, 03:08 PM
@ Opserty - The rebuild_equipment only goes through if the person clicks on the button to upgrade their equipment "if ($test->input['do'] == 'guard_shield')". Once they do that, it will change one of the values, so I used the rebuild to put them all back together after the change occurs.

$full_equipment[5] = 's:1:"8"';
You have changed part of the array there, so its changed fully. Theres no need to rebuild it using array() again. You can just implode it as you have done on the next line.

Sarcoth
08-08-2007, 03:10 PM
I used the stripslashes and the error_reporting. No errors to report. Here's the php code I am using.

$find_monster = $inferno->db->query_first("SELECT m_equipment FROM inferno_monster where m_enc_loc='{$inferno->user['current_place']}'");
$find_equipment2 = unserialize(stripslashes($find_monster['m_equipment']));


I'm using the following in a template to see the data. I'm guessing these are wrong?

{$find_equipment2}<BR>
{$find_equipment2[1]}

$full_equipment[5] = 's:1:"8"';
You have changed part of the array there, so its changed fully. Theres no need to rebuild it using array() again. You can just implode it as you have done on the next line.

Ahhhh! Thanks for that.