Jump to content

looping/array.compare data help


overlordofevil

Recommended Posts

Hello All,

 

I am trying to compared data from a mysql table to data that a user selects, so what I have done is I pulled the data and put it into an array. For the most part it works fine but I am lost on how to go through the array when I have multiple values to look for.

 

Here is the code.

  $query = "SELECT skillID FROM skill1 where cid = '$cid'";
  $result = mysql_query($query) or die (mysql_error());
  $prereq = array();
  while ($row = mysql_fetch_assoc($result))
  {
   $prereq[] = $row; 
  }	

Ok so the first part pulls the info I need from the table and drops it in to the array. this works for the most part.

 

The next part is suppose to run through the array and compare it to id's specified to see if the value is present. 

  if ($value =='998')
  {
  if ($prereq=='17' || $prereq=='18' || $prereq=='19' || $prereq=='20' || $prereq=='22' || $prereq=='23' || $prereq=='25' || $prereq=='26' || $prereq=='30' || $prereq=='31' || $prereq=='32')
  {
   $canhave = 1;
  }
  else
  {
  $canhave = 0;
  }	
  }

Now as you see I don't have a loop or anyway to compare this data except for the first value listed in the array. Where I am stuck is how to figure this out or if I need to use a loop.

 

The other thing I am stuck on is pulling 2 values from the same array to compare it. I want to see if 2 values are listed.

 

  if ($prereq=='34' && $prereq=='56')

 

but I don't know how to pull 2 pieces of data from the array when they have the same variable name.

 

Any hints or suggestions on what I can do or try would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/133234-loopingarraycompare-data-help/
Share on other sites

One of the points of using a database is that it will do searches and comparisons so that you retrieve only the information you are interested in.

 

The following query will only return rows that match what your existing query and your slow parsed/tokenized/interpreted php code would be doing -

 

$query = "SELECT skillID FROM skill1 where cid = '$cid'
AND skillID IN (17,18,19,20,22,23,25,26,30,31,32)";

Ok I see how it works. I can call the table check it and do the if statement after that but how do i get it to compare multiple values when I require 2 values to do the check ?

 

  if ($prereq=='34' && $prereq=='56')

 

I will try this out and see if it does what I want.  thanks for the help.

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.