Enjoy an ad free experience by logging in. Not a member yet?
Register .
03-16-2010, 02:19 PM
PM User |
#1
New to the CF scene
Join Date: Mar 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
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
03-16-2010, 02:35 PM
PM User |
#2
Senior Coder
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
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...?
03-16-2010, 02:48 PM
PM User |
#3
New to the CF scene
Join Date: Mar 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
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.
03-16-2010, 03:01 PM
PM User |
#4
Senior Coder
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
You still didn't answer Matt's question. Is $votesdb->useridfield an array, a string, or something else?
03-16-2010, 03:11 PM
PM User |
#5
New to the CF scene
Join Date: Mar 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
I think it's a string of comma separated id's.
03-16-2010, 03:11 PM
PM User |
#6
Senior Coder
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
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?
03-16-2010, 03:15 PM
PM User |
#7
Super Moderator
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Quote:
Originally Posted by
masterofollies
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
}
Users who have thanked Inigoesdr for this post:
03-16-2010, 03:20 PM
PM User |
#8
Senior Coder
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
Code:
$idfield = explode(',', str_replace(' ', '', $votesdb->useridfield));
if (in_array($userid, $idfield))
{
[do something here]
}
03-16-2010, 03:27 PM
PM User |
#9
New to the CF scene
Join Date: Mar 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Thank you all for your effort.
Quote:
Originally Posted by
Inigoesdr
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.
03-16-2010, 04:25 PM
PM User |
#10
Senior Coder
Join Date: May 2005
Posts: 2,137
Thanks: 96
Thanked 72 Times in 72 Posts
Quote:
Originally Posted by
Inigoesdr
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?
03-16-2010, 05:08 PM
PM User |
#11
Senior Coder
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
Quote:
Originally Posted by
masterofollies
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.
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 09:44 AM .
Advertisement
Log in to turn off these ads.