Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-16-2010, 02:19 PM   PM User | #1
gatsman
New to the CF scene

 
Join Date: Mar 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
gatsman is an unknown quantity at this point
Question PHP if syntax

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

Last edited by gatsman; 03-16-2010 at 03:25 PM.. Reason: Resolved
gatsman is offline   Reply With Quote
Old 03-16-2010, 02:35 PM   PM User | #2
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
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...?
MattF is offline   Reply With Quote
Old 03-16-2010, 02:48 PM   PM User | #3
gatsman
New to the CF scene

 
Join Date: Mar 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
gatsman is an unknown quantity at this point
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

Code:
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.
gatsman is offline   Reply With Quote
Old 03-16-2010, 03:01 PM   PM User | #4
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
You still didn't answer Matt's question. Is $votesdb->useridfield an array, a string, or something else?
__________________
Are you a Help Vampire?
tomws is offline   Reply With Quote
Old 03-16-2010, 03:11 PM   PM User | #5
gatsman
New to the CF scene

 
Join Date: Mar 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
gatsman is an unknown quantity at this point
I think it's a string of comma separated id's.
gatsman is offline   Reply With Quote
Old 03-16-2010, 03:11 PM   PM User | #6
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
You'd need to use in_array.

PHP Code:
$checkid  $userrow['id'];
 
$unique explode','$checkid );

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

__________________
Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?
masterofollies is offline   Reply With Quote
Old 03-16-2010, 03:15 PM   PM User | #7
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by masterofollies View Post
You'd need to use in_array.

PHP Code:
$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:
PHP Code:
$ids explode(','$votesdb->useridfield);
if(
in_array($userid$ids))
{
    
// do something

Inigoesdr is offline   Reply With Quote
Users who have thanked Inigoesdr for this post:
gatsman (03-16-2010)
Old 03-16-2010, 03:20 PM   PM User | #8
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Code:
$idfield = explode(',', str_replace(' ', '', $votesdb->useridfield));

if (in_array($userid, $idfield))
{
        [do something here]
}
MattF is offline   Reply With Quote
Old 03-16-2010, 03:27 PM   PM User | #9
gatsman
New to the CF scene

 
Join Date: Mar 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
gatsman is an unknown quantity at this point
Thank you all for your effort.

Quote:
Originally Posted by Inigoesdr View Post
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.
gatsman is offline   Reply With Quote
Old 03-16-2010, 04:25 PM   PM User | #10
masterofollies
Senior Coder

 
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
masterofollies can only hope to improve
Quote:
Originally Posted by Inigoesdr View Post
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:
PHP Code:
$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.
__________________
Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?
masterofollies is offline   Reply With Quote
Old 03-16-2010, 05:08 PM   PM User | #11
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Quote:
Originally Posted by masterofollies View Post
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.
MattF is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:44 AM.


Advertisement
Log in to turn off these ads.