Jump to content

Recommended Posts

Hello all,

 

I am pulling data from a mysql table to compare with data in another table. The function I am working on is comparing data to see if an ID exists or not. Now I have a simple one that works fine but what I need to do is add a condition that tells the function to skip multiple ID's if a specific one exists.

 

function checkdata ($cid, $ID)
{
if ($ID == '17' || $ID == '18' || $ID == '19' || $ID == '20' || $ID == '21' || $ID == '22' || $ID == '23' || $ID == '24' || $ID == '25' || $ID == '26' || $ID == '27' || $ID == '28' || $ID == '29' || $ID == '30' || $ID == '31' || $ID == '32' || $ID == '34' || $ID == '35' || $ID == '56' || $ID == '57' || $ID == '58') 
{
  $query = "SELECT * FROM data where cid = '$cid' and ID='$ID'";
  $result = mysql_query($query) or die (mysql_error());
  $row = mysql_fetch_assoc($result);
  $skill = $row['ID'];		
  if ($skill == $ID)
  {
  $check=1; //returns a positive response and skips this id for the print/display	
  }
  else 
  {
  $check=0; //returns negative and prints/displays the id
  }	
return $check;
}
}

 

Now this works fine and if it finds the ID in question the check returns a positive and does not "print" it. What I need to do is add a condition if a specific ID exist then it can skip multiple specific ID's.

 

For Example if ID = 30, then I need the function to send a positive response back for 17, 18, 19, 20 and 21 so they do not "print".

 

I am unsure how to proceed so if anyone has a suggestion I would appreciate it.

 

Thanks

Bill

Link to comment
https://forums.phpfreaks.com/topic/169137-phpmysql-data-question/
Share on other sites

You could use DISTINCT in your query to only return 1 of each distinct values.

In other words, if the table consisted of IDs 1,1,1,2,3,4,5,6,6,7,8,9,9,9 and you did a

 

SELECT DISTINCT ID FROM data

 

it would return an array with 1,2,3,4,5,6,7,8,9

 

Please let me know if that helps.

well firstly, you could greatly reduce the size of that if statement by putting all the ids you want to check in an array, and using the in array function like

 

$array = array("17", "19", "20", etc...);

if (in_array($id, $array)){
//do whatever
}

 

I don't really get your question though. Can you try to explain again?

can you explain your variables please?

 

$cid is the CHECK ID as in the one you're going to see if it's in the other DB

$ID is the one you're checking against?

 

if so, your SQL doesn;t make sense...

 

infact reading through it , NONE OF IT DOES :(

 

 

eg

 

 

checkdata (12, 15) >>

 

15 is not in $ID if statement so carry on

 

iogfjergfioegjioergjregjioergjoe

 

RAGE

 

 

 

post the data structure/contents of your database please

or a small selection of it

 

and a more detailed description of your problem :)

 

 

thanks for the replies. I apologize for not explaining better I am still learning and working out the concepts. 

 

the id's are used to represent a data record and cid is the players/characters id.

 

I am creating a character db for some friends of mine. The idea is they login and go to their character/player info and sees a list of skills they can select from. certain skills can only be added once, out of these skills specific ones cancel out others.

 

so for example they can add the following skills once

Long sword - ID 17

Short Sword - ID 18

Mace  - ID 19

Dagger - ID 20

Weapon Master - ID 30

 

So from the skills listed above "Weapon Master" allows the player the ability to use the other 4 skills so if they add Weapon Master they shouldn't see the other ones ever again.

 

The current function shows how the interface sends a specific skill id and character id to it to be checked against the specific player's/character file and sees if a skill exists or not. If the skill is there it returns the positive so the skill info won't print and they can't add it again.

 

function checkdata ($cid, $ID) // character id and skill id passed from main script.
{
if ($ID == '17' || $ID == '18' || $ID == '19' || $ID == '20' || $ID == '21' || $ID == '22' || $ID == '23' || $ID == '24' || $ID == '25' || $ID == '26' || $ID == '27' || $ID == '28' || $ID == '29' || $ID == '30' || $ID == '31' || $ID == '32' || $ID == '34' || $ID == '35' || $ID == '56' || $ID == '57' || $ID == '58') // these skills can only be added once 
{
  $query = "SELECT * FROM data where cid = '$cid' and ID='$ID'"; // checking the players file for the specific skill id
  $result = mysql_query($query) or die (mysql_error());
  $row = mysql_fetch_assoc($result);
  $skill = $row['ID'];      
  if ($skill == $ID) // comparing the data 
  {
  $check=1; //returns a positive response and skips this id for the print/display
  }
  else
  {
  $check=0; //returns negative and prints/displays the id
  }   
return $check;
}
}

 

What I want to do is have the function get the "higher level" skill and if it exists in the character's file then it should be able to send a positive result back for the lower level skills as well.

 

I hope that explains it a bit better.

 

Thanks again for the responses and any other help is appreciated.

Agree with the other responders, it sounds like a case for a switch (pardon the pun)

 

I don't think anyone said anything about a switch statement lol. at least i didn't

 

Those were two separate statements

 

1. I agree with the other responders

 

and

 

2. It sounds like a case for a switch the way he described it

 

:) Not trying to ut words in anyone's mouth

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.