Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 08-07-2007, 06:19 PM
Sarcoth Sarcoth is offline
 
Join Date: Mar 2006
Location: Huntsville
Posts: 521
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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!
Reply With Quote
  #2  
Old 08-08-2007, 11:30 AM
nico_swd's Avatar
nico_swd nico_swd is offline
 
Join Date: Dec 2005
Location: Spain
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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>'
Reply With Quote
  #3  
Old 08-08-2007, 11:45 AM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #4  
Old 08-08-2007, 12:48 PM
Sarcoth Sarcoth is offline
 
Join Date: Mar 2006
Location: Huntsville
Posts: 521
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by nico_swd View Post
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.
Reply With Quote
  #5  
Old 08-08-2007, 02:21 PM
nico_swd's Avatar
nico_swd nico_swd is offline
 
Join Date: Dec 2005
Location: Spain
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #6  
Old 08-08-2007, 03:08 PM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Sarcoth View Post
@ 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.
Reply With Quote
  #7  
Old 08-08-2007, 03:10 PM
Sarcoth Sarcoth is offline
 
Join Date: Mar 2006
Location: Huntsville
Posts: 521
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 View Post
$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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:31 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.07477 seconds
  • Memory Usage 2,258KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_html
  • (7)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete