Jump to content


Photo

Array


  • Please log in to reply
21 replies to this topic

#1 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 04:24 AM

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?

#2 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 04:31 AM

do you want each word to be it's own array element? It can be done

$result = mysql_fetch_array($query);
$query_data = $result[names];
$query_array = explode(',', $query_data);

print_r($query_array);

That should get ya started maybe?

#3 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 04:46 AM

Ok, well i'm doing an if statement for, "in_array" so how would I incorporate that into it?

#4 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 04:51 AM

you mean something like?
$result = mysql_fetch_array($query);
$query_data = $result[names];
$query_array = explode(',', $query_data);

if (in_array('Derby', $query_array))
{
echo "Derby is here";
}


#5 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 04:53 AM

Let me ask something, what exactly does the explode do?

#6 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 04:56 AM

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.

#7 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 04:59 AM

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.

#8 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 05:00 AM

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

$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') {


#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 August 2006 - 05:03 AM

Change the function mysql_fetch_array() to mysql_fetch_assoc().

Put some debugging statements in to see what is being returned:
<?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') {
?>

Ken

#10 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 05:09 AM

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.

#11 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 05:12 AM

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.

#12 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 05:16 AM

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

#13 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 05:32 AM

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.

#14 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 05:35 AM

Array
(
    [0] =>
)

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

#15 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 05:39 AM

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"

#16 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 05:47 AM

Array
(
    [0] => Dark Elf
    [1] => Elf
)

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

#17 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 05:48 AM

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.

$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') {


#18 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 05:51 AM

$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') {

That's what I have, and when I hit equip on the item this is what it shows.

Array
(
    [0] => Dark Elf
    [1] => Elf
)

#19 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 10 August 2006 - 05:54 AM

THe script you told me to put doesn't output anything to tell you the truth.

#20 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 06:06 AM

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.


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') {





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users