Jump to content

Questions about arrays.


overlordofevil

Recommended Posts

I got a question with arrays.

 

What I want to do is pull data from mysql in to an array. now I know how to pull the info and get an array that uses a loop to show the data.

 

example

$query = "SELECT * FROM skill";
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_array($result))
  {
   extract($row);
   echo $row[whatever];
  }

 

what I want to do is pull the data and store it in an array that I can then use to test data.

 

What I have is a script that tests for a specific variable and then compares it to what is stored in a separate table. What I am having trouble doing is when it needs to check for 2 separate values in the table.

 

while ($row = mysql_fetch_array($result))
  {
  extract($row);
$cost = getskillcost($skillID, $classid);
  echo "<tr><td>\n";
  $preq= explode(",",$prereq);
  foreach ($preq as $value)
  {
  if ($value == $n)
  {

  echo "<a href='addskill.php?action=add&skillID=$skillID'>$skillName</a> ($cost)";	
  }

 

Ok as you see with the first part is is pulling the general data and checking for the the value. if it is '0' then the item is printed if it is any other value it goes to the next step.

 

  else
  {
  $query1 = "SELECT * FROM skill where id = '$id'";
  $result1 = mysql_query($query1) or die (mysql_error());
  while ($row1 = mysql_fetch_array($result1))
  {
  extract($row1);
  if ($value == $skill)
  {
  echo "<a href='addskill.php?action=add&skillID=$skillID'>$skillName</a> ($cost)";	
  }
  }
  }
  }
  echo "</td></tr>\n";
  }

 

Now as you can see on the second part it works when it comes to pulling just one value to compare but for a few items i have multiple values that I need to compare. 

 

So I guess what I am asking is there a way to pull the data and store it as skill[] and use that reference to check the values Or I might be completely off and looking at this the wrong way.

 

Any suggestions would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/120616-questions-about-arrays/
Share on other sites

<?php
$resultArray = array();
while ($row = mysql_fetch_assoc($result))
{
   $resultArray[] = $row; 
}
?>

 

that will load a mysql query resource, in this case $result, into an array $resultArray.

 

I couldn't understand your exact problem (I'm out of it right now) so I couldn't advise you if you're going about it the wrong way :)

Archived

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

×
×
  • 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.