...

View Full Version : Resolved PHP if syntax



gatsman
03-16-2010, 03:19 PM
Hello, i could use some help.

I have a field in the database that takes the users id as values,
like this: 61, 65, 87, e.t.c.

Is there an if statement that can check if users id (i can get the users id) is included in one of those numbers.


Thanks John

MattF
03-16-2010, 03:35 PM
Are you extracting the id's from the DB and creating an array of id's, a string of comma delimited id's straight from the DB field or...?

gatsman
03-16-2010, 03:48 PM
Thanks for the fast reply

I am new with PHP.
I don t know if i read the field in the right way but i hope that you may get your answer from the following


if ($userid == ($votesdb->useridfield)) {
do something}

if the fields data are 61, 65, 87
$userid value will have to be exactly 61, 65, 87 in order for this to be true
but the $userid will be 61 or 87 or e.t.c.

I hope i said it right.
Thanks again for reading this.

tomws
03-16-2010, 04:01 PM
You still didn't answer Matt's question. Is $votesdb->useridfield an array, a string, or something else?

gatsman
03-16-2010, 04:11 PM
I think it's a string of comma separated id's.

masterofollies
03-16-2010, 04:11 PM
You'd need to use in_array.



$checkid = $userrow['id'];
$unique = explode( ',', $checkid );

if (in_array("$id", $unique)) {
echo "This ID matches.";
}

Inigoesdr
03-16-2010, 04:15 PM
You'd need to use in_array.



$checkid = $userrow['id'];
$unique = explode( ',', $checkid );

if (in_array("$id", $unique)) {
echo "This ID matches.";
}

If you're going to give an example, at least either use the asker's original code or state that the example is an unrelated example.

For example:

$ids = explode(',', $votesdb->useridfield);
if(in_array($userid, $ids))
{
// do something
}

MattF
03-16-2010, 04:20 PM
$idfield = explode(',', str_replace(' ', '', $votesdb->useridfield));

if (in_array($userid, $idfield))
{
[do something here]
}

gatsman
03-16-2010, 04:27 PM
Thank you all for your effort.


If you're going to give an example, at least either use the asker's original code or state that the example is an unrelated example
Thank you!!! i am new at this and your example made it clear.

masterofollies
03-16-2010, 05:25 PM
If you're going to give an example, at least either use the asker's original code or state that the example is an unrelated example.

For example:

$ids = explode(',', $votesdb->useridfield);
if(in_array($userid, $ids))
{
// do something
}

That coding isn't something I am familar with. I posted the correct code, just needed to be customized. No reason to make me look dumb in front of everyone when I supplied the correct code.

MattF
03-16-2010, 06:08 PM
No reason to make me look dumb in front of everyone when I supplied the correct code.

That wasn't his? intention, I believe. It's simply a fact that if someone posts on here, their level of proficiency at coding could be anywhere from practically zero to master of. If someone is in the early stages of programming skills, presenting an indirect example may be taken as being literal code to use, hence could end up confusing the O.P tenfold, hence the suggestion to present usable code that can be dropped in or to mention the fact that it is unusable, (directly, as is), example code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum