Looping through character data and items from MySQL DB in PHP -
i have following code:
include "config.php"; error_reporting(0); $idsuccess = 0; if(isset($_post['username'])) { $usr = $_post['username']; $pwd = $_post['userpwd']; $sqlq = "select * dr_users dr_user_name='$usr' , dr_user_pwd='$pwd' limit 1"; $q = mysql_query($sqlq); if(mysql_num_rows($q) > 0) { $usero = mysql_fetch_assoc($q); $userdata = array('userid' => $usero['dr_user_id'], 'username' => $usero['dr_user_name'], 'userpwd' => $usero['dr_user_pwd'], 'usereml' => $usero['dr_user_email'], 'privlvl' => $usero['dr_user_priv_level']); $idsuccess = 1; $uid = $usero['dr_user_id']; $charq = mysql_query("select * dr_chars dr_user_id='$uid'"); while($char = mysql_fetch_array($charq)) { $chars[] = $char; $charid = $char['dr_char_id']; $itemq = mysql_query("select * dr_items dr_char_id='$charid'"); while($item = mysql_fetch_array($itemq)) { $itemdata[] = array('itemid' => $item['dr_item_id'], 'itemname' => $item['dr_item_name'], 'itemdesc' => $item['dr_item_desc'], 'itemtype' => $item['dr_item_type'], 'itemcost' => $item['dr_item_cost'], 'itemcurtype' => $item['dr_item_cur_type'], 'itematk' => $item['dr_item_stat_atk'], 'itemdef' => $item['dr_item_stat_def'], 'itemend' => $item['dr_item_stat_end'], 'itemluck' => $item['dr_item_stat_luck'], 'itemfileurl' => $item['dr_item_file_url'], 'itemstaff' => $item['dr_item_staff'], 'itemequipped' => $item['dr_char_eqp'], 'spx' => $item['dr_static_pos_x'], 'spy' => $item['dr_static_pos_y']); } $chardata[] = array('charid' => $char['dr_char_id'], 'charname' => $char['dr_char_name'], 'charrace' => $char['dr_char_race'], 'charlvl' => $char['dr_char_lvl'], 'charitems' => $itemdata); } $resdata = array('idsuccess' => $idsuccess, 'user' => $userdata, 'chars' => $chardata); } else { $resdata = array('idsuccess' => $idsuccess); } echo json_encode($resdata); } ?>
i have loaded character data table 'dr_chars' in database. i'm trying load corresponding item data retrieved character id , push $chardata array. can encode & output in json format.
if question isn't clear ask , i'll try explain situation better. json data being outputted flash dynamic use.
i managed fix final code follows:
<?php include "config.php"; error_reporting(0); $idsuccess = 0; if(isset($_post['username'])) { $usr = $_post['username']; $pwd = $_post['userpwd']; $sqlq = "select * dr_users dr_user_name='$usr' , dr_user_pwd='$pwd' limit 1"; $q = mysql_query($sqlq); if(mysql_num_rows($q) > 0) { $usero = mysql_fetch_assoc($q); $userdata = array('userid' => $usero['dr_user_id'], 'username' => $usero['dr_user_name'], 'userpwd' => $usero['dr_user_pwd'], 'usereml' => $usero['dr_user_email'], 'privlvl' => $usero['dr_user_priv_level']); $idsuccess = 1; $uid = $usero['dr_user_id']; $charq = mysql_query("select * dr_chars dr_user_id='$uid'"); $chars = array(); while($char = mysql_fetch_array($charq)) { $charid = $char['dr_char_id']; $itemq = mysql_query("select * dr_items dr_char_id='$charid'"); $items = array(); while($item = mysql_fetch_array($itemq)) { $items[] = array('itemid' => $item['dr_item_id'], 'itemname' => $item['dr_item_name'], 'itemdesc' => $item['dr_item_desc'], 'itemtype' => $item['dr_item_type'], 'itemcost' => $item['dr_item_cost'], 'itemcurtype' => $item['dr_item_cur_type'], 'itematk' => $item['dr_item_stat_atk'], 'itemdef' => $item['dr_item_stat_def'], 'itemend' => $item['dr_item_stat_end'], 'itemluck' => $item['dr_item_stat_luck'], 'itemfileurl' => $item['dr_item_file_url'], 'itemstaff' => $item['dr_item_staff'], 'itemequipped' => $item['dr_char_eqp'], 'spx' => $item['dr_static_pos_x'], 'spy' => $item['dr_static_pos_y']); } $chars[] = array('charid' => $char['dr_char_id'], 'charname' => $char['dr_char_name'], 'charrace' => $char['dr_char_race'], 'charlvl' => $char['dr_char_lvl'], 'charitems' => $items); } $resdata = array('idsuccess' => $idsuccess, 'user' => $userdata, 'chars' => $chars); } else { $resdata = array('idsuccess' => $idsuccess); } echo json_encode($resdata); } ?>
Comments
Post a Comment