Jump to content

Array


SieRobin

Recommended Posts

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
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
Link to comment
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.
Link to comment
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.
Link to comment
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.
Link to comment
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]
Link to comment
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
)
Link to comment
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]
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.