Jump to content

Archived

This topic is now archived and is closed to further replies.

SieRobin

Array

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Ok, well i'm doing an if statement for, "in_array" so how would I incorporate that into it?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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, Elf

Putting that into a simple array is my problem, my obstacle if you will.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Hmmn, ok.. if you have any good suggestions, or a different way to do it, please post :]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Array
(
    [0] =>
)

That's what it prints if you're interested ken.

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
Array
(
    [0] => Dark Elf
    [1] => Elf
)

I've gotten to that point, but it still doesn't work.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
[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
)

Share this post


Link to post
Share on other sites
THe script you told me to put doesn't output anything to tell you the truth.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
[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 :D

The 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

Share this post


Link to post
Share on other sites
ah, see, always something other then what you thought the problem was lol. Glad to help out.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.