SieRobin Posted August 10, 2006 Share Posted August 10, 2006 Quick question here, is there a way to pull a varchar from your mysql database, and put it into an array style if each word is seperated by commas? Link to comment https://forums.phpfreaks.com/topic/17099-array/ Share on other sites More sharing options...
DylanBlitz Posted August 10, 2006 Share Posted August 10, 2006 do you want each word to be it's own array element? It can be done[code]$result = mysql_fetch_array($query);$query_data = $result[names];$query_array = explode(',', $query_data);print_r($query_array);[/code]That should get ya started maybe? Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72272 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 Ok, well i'm doing an if statement for, "in_array" so how would I incorporate that into it? Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72279 Share on other sites More sharing options...
DylanBlitz Posted August 10, 2006 Share Posted August 10, 2006 you mean something like?[code]$result = mysql_fetch_array($query);$query_data = $result[names];$query_array = explode(',', $query_data);if (in_array('Derby', $query_array)){echo "Derby is here";}[/code] Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72281 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 Let me ask something, what exactly does the explode do? Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72282 Share on other sites More sharing options...
DylanBlitz Posted August 10, 2006 Share Posted August 10, 2006 explode will take a line of text and split it up depending on the qualifier. So if you have$myline = "See spot run. See spot jump. See spot die";If you explode it on the . it will give you 3 array elements. Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72283 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 Ok, but as in an array everything is seperated by commas and single quotes, therefore it wouldn't help much since it can't implement the quotes. Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72284 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 If you're curious, this is how I have it set up, which obviously doesn't work or I wouldn't be torturing you with my stupidity :P[code]$itemraces=mysql_query("SELECT * from market where ID='$inventory3[IID]'"); $itemraces2=mysql_fetch_array($itemraces); $itemraces3=$itemraces2['race']; $racearray=explode(",", $itemraces3); if (isset($equip)) { if (in_array($userstats3['race'], $racearray)||$race=='All') {[/code] Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72285 Share on other sites More sharing options...
kenrbnsn Posted August 10, 2006 Share Posted August 10, 2006 Change the function mysql_fetch_array() to mysql_fetch_assoc().Put some debugging statements in to see what is being returned:[code]<?php$itemraces=mysql_query("SELECT * from market where ID='$inventory3[IID]'"); $itemraces2=mysql_fetch_assoc($itemraces); echo '<pre>' . print_r($itemraces2,true) . '</pre>'; $itemraces3=$itemraces2['race']; $racearray=explode(",", $itemraces3); echo '<pre>' . print_r($racearray,true) . '</pre>'; if (isset($equip)) { if (in_array($userstats3['race'], $racearray)||$race=='All') {?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72287 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 My site is a browser based MMORPG, therefore everyone has their own individual inventories. Some items are race specific, since there is different races in my game. I'm trying to make it filter out whom is or isn't of the race type to equipped the item. So therefore there is a "race" field where I store which races can utilize the item. Now taking it from the database and making it as an array to use an, "in_array" statement was the easiest way I thought of, which isn't becoming so easy.The script above basically shows my variables I'm using to retrieve the info from the database, all I really want is for that field to become a simple array. As of now there is an item called "Yasuka Blade" which only Dark Elves and Elves can use, and it's seperated as such in the database.. Dark Elf, ElfPutting that into a simple array is my problem, my obstacle if you will. Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72289 Share on other sites More sharing options...
DylanBlitz Posted August 10, 2006 Share Posted August 10, 2006 ah, I think I know what your saying. Your getting a bit confused with the array though.$myarray = array('Dog', 'Jump', 'Brick', 'House');Is that what your talking about? That's how you set an array. That's not what php considers the array to be once it takes that in. If you want to see how the array is setup try doing a print_r on an array so you can see how it's layed out. I think that will help you understand it a bit better. Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72291 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 Hmmn, ok.. if you have any good suggestions, or a different way to do it, please post :] Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72294 Share on other sites More sharing options...
DylanBlitz Posted August 10, 2006 Share Posted August 10, 2006 did you try what ken posted?It should give you an output of the arrays. First you want to make sure the right market records are getting returned. Also you can echo out the $inventory3[IID] and $userstats3['race'] to make sure they're holding the values your expecting. I can't count the times I've tried to trouble shoot a problem only to find out it was a bad variable that I had set wrong lol. Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72295 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 Array( [0] => )That's what it prints if you're interested ken. Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72298 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 If I use a $_GET command and use that in the array, when I hit equip, basically it shows the two as, "Dark Elf, Elf" Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72299 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 Array( [0] => Dark Elf [1] => Elf)I've gotten to that point, but it still doesn't work. Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72302 Share on other sites More sharing options...
DylanBlitz Posted August 10, 2006 Share Posted August 10, 2006 Okay, try this cause I'm getting confused lol. Paste what the page shows back here. I know we're not gettign to the in_array, trying to figure out where the problem lies.[code]$itemraces=mysql_query("SELECT * from market where ID='$inventory3[IID]'");$itemraces2=mysql_fetch_assoc($itemraces);echo 'Market results:<BR><pre>' . print_r($itemraces2,true) . '</pre><BR><BR>';$itemraces3=$itemraces2['race'];$racearray=explode(",", $itemraces3);echo 'Table results:<BR><pre>' . print_r($racearray,true) . '</pre><BR><BR>'; echo 'equip is:' . $equip . '<BR>';echo 'race is:' . $userstats3['race'];exit; if (isset($equip)) { if (in_array($userstats3['race'], $racearray)||$race=='All') {[/code] Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72304 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 [code]$equip=$_GET['equip']; $unequip=$_GET['unequip']; $user=$_GET['user']; $type=$_GET['type']; $race=$_GET['race']; $dmg=$_GET['dmg']; $arm=$_GET['arm']; $agil=$_GET['agil']; $dex=$_GET['dex']; $itemraces=mysql_query("SELECT * from market where ID='$inventory3[IID]'"); $itemraces2=mysql_fetch_assoc($itemraces); echo '<pre>' . print_r($itemraces2,true) . '</pre>'; $itemraces3=$itemraces2['race']; $racearray=explode(", ", $race); echo '<pre>' . print_r($racearray,true) . '</pre>'; if (isset($equip)) { if ($userstats3['race']==$race||$race=='All') {[/code]That's what I have, and when I hit equip on the item this is what it shows.Array( [0] => Dark Elf [1] => Elf) Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72305 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 THe script you told me to put doesn't output anything to tell you the truth. Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72307 Share on other sites More sharing options...
DylanBlitz Posted August 10, 2006 Share Posted August 10, 2006 the market table does have a race column right?We're problaby just missing something simple, a variable set wrong somewhere, that's why I'm asking you to output it all. Try something I forgot, if there's more then 1 row you need to loop through the results heh.[code]echo 'IID is:' . $inventory3[IID] . '<BR>';$race_query = mysql_query("SELECT * from market where ID='$inventory3[IID]'");for ($i = 0; $i < mysql_num_rows($race_query); $i++){$race_fetch = mysql_fetch_assoc($race_query);$race_data = $race_fetch['race'];echo 'Database race is: ' . $race_data . '<BR>';$racearray = explode(", ", $race_data);echo 'Race Array:<BR><pre>' . print_r($racearray,true) . '</pre>';}echo '<BR><BR><BR>Users race is: ' . ($userstats3['race'] . '<BR>';exit; if (isset($equip)) { if ($userstats3['race']==$race||$race=='All') {[/code] Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72309 Share on other sites More sharing options...
SieRobin Posted August 10, 2006 Author Share Posted August 10, 2006 [quote author=DylanBlitz link=topic=103668.msg413009#msg413009 date=1155189961]the market table does have a race column right?We're problaby just missing something simple, a variable set wrong somewhere, that's why I'm asking you to output it all. Try something I forgot, if there's more then 1 row you need to loop through the results heh.[code]echo 'IID is:' . $inventory3[IID] . '<BR>';$race_query = mysql_query("SELECT * from market where ID='$inventory3[IID]'");for ($i = 0; $i < mysql_num_rows($race_query); $i++){$race_fetch = mysql_fetch_assoc($race_query);$race_data = $race_fetch['race'];echo 'Database race is: ' . $race_data . '<BR>';$racearray = explode(", ", $race_data);echo 'Race Array:<BR><pre>' . print_r($racearray,true) . '</pre>';}echo '<BR><BR><BR>Users race is: ' . ($userstats3['race'] . '<BR>';exit; if (isset($equip)) { if ($userstats3['race']==$race||$race=='All') {[/code][/quote]No need, I got it thank you :DThe problem was in the IID for the query, I just changed it to IID='$equip' now it recognizes which item it is.Yet the assoc and the explode I still use, so that still helped me out alot, thank you muchs :D Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72310 Share on other sites More sharing options...
DylanBlitz Posted August 10, 2006 Share Posted August 10, 2006 ah, see, always something other then what you thought the problem was lol. Glad to help out. Link to comment https://forums.phpfreaks.com/topic/17099-array/#findComment-72312 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.