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? Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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] Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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] Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 :] Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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" Quote Link to comment 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. Quote Link to comment 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] Quote Link to comment 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) Quote Link to comment 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. Quote Link to comment 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] Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.