vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Suggestions Welcomed (https://vborg.vbsupport.ru/showthread.php?t=154568)

Sarcoth 08-07-2007 05:19 PM

Suggestions Welcomed
 
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.

PHP Code:

$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 10: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.
PHP Code:

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

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


Opserty 08-08-2007 10:45 AM

PHP Code:

$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 11:48 AM

Quote:

Originally Posted by nico_swd (Post 1312623)
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.
PHP Code:

$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 01:21 PM

You have to unescape the quotes.
PHP Code:

$array unserialize(stripslashes($array)); 


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

error_reporting(E_ALL); 

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

Opserty 08-08-2007 02:08 PM

Quote:

Originally Posted by Sarcoth (Post 1312650)
@ 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 02:10 PM

I used the stripslashes and the error_reporting. No errors to report. Here's the php code I am using.

PHP Code:

$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?

HTML Code:

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

Quote:

Originally Posted by Opserty (Post 1312771)
$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.


All times are GMT. The time now is 01:47 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02321 seconds
  • Memory Usage 1,765KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_html_printable
  • (7)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete