Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

PHP If Elseif with string in query

Hi,
Struggling with possibly a simple solution:

I have the below code:

<?php
$profile = $row_usr_profile_type['value'];

if ($profile == 'University') {?>
<a href="university.php" >Edit profile</a>

<?php } elseif ($profile == 'School') { ?>
<a href="school.php" >Edit profile</a>
    
<?php } else { ?>
<a href="others.php" >Edit profile</a>
<?php } ?>

Open in new window


What I want is when $profile "Contains" the word University, the link need to be changed. I know the current one is "Equal" not "Contains"  I tried with "strpos" but couldn't find an example to hold multiple values. What I tried is as below:

<?php 
$string = '$profile';
if ( strpos($string, 'School') !== false )
{
    echo 'School Link';


} elseif( strpos($string, 'University') !== true )
	echo "University Link";


else {
	echo "Other Link";
}

?>

Open in new window


Can any one be of help please.
Thanks
Jay
0
jayseena
Asked:
jayseena
1 Solution
 
GaryCommented:
$string = '$profile';
should be

$string =$profile;


Though I don't know why you are assigning a variable to another variable
0
 
GaryCommented:
Just do it this way, you already have the value assigned to $profile from your initial code
<?php

if ( strpos($profile, 'School') !== false )
{
    echo 'School Link';


} elseif( strpos($profile, 'University') !== true )
      echo "University Link";


else {
      echo "Other Link";
}

?>
0
 
grahamnonweilerCommented:
You have a couple of options, assuming of course that you are only dealing with 3 possible values (namely "university",  "school" and "others"). The first is example below is less efficient than the second example:

Example 1:
<?php
$profile = $row_usr_profile_type['value'];
$ths_link="others.php";
if (strtolower($profile) == 'university') { $ths_link="university.php"; }
if (strtolower($profile) == 'school') { $ths_link="school.php"; }
echo('<a href="'.$ths_link.'" >Edit profile</a>');
?> 

Open in new window

Example 2: (recommended)
<?php
$profile = strtolower($row_usr_profile_type['value']);
$ths_link="others.php";
if ($profile=="university" or $profile=="school") { $ths_link=$profile.".php"; }
echo('<a href="'.$ths_link.'" >Edit profile</a>');
?> 

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GaryCommented:
You didn't read the question - how can they check it 'contains' the word not equal to and anyway the issue is
$string = '$profile' should be $string = $profile;
0
 
jayseenaAuthor Commented:
Though I don't know why you are assigning a variable to another variable
Yeah.. my ignorance. There should be a better way to do this, for sure. But right now it works for me.
$string = '$profile' should be $string = $profile;

Open in new window


Just another simple question, how can I echo <a href="publisher_profile.php?org_id=<?php echo $row_usr_profile_type['oraganisaiton_id']; ?>" target="submitform">build / Edit profile</a>   instead of     echo 'School Link';

@grahamnonweiler, thanks for your input. I didn't try your suggestion as the first suggestion itself worked for me. Thanks again for your time.

@GaryC123, Thanks for your help.

Jay
0
 
GaryCommented:
Just simply...

echo '<a href="publisher_profile.php?org_id=' . $row_usr_profile_type['oraganisaiton_id'] . ' target="submitform">Build / Edit profile</a>';

I assume oraganisaiton is just a typo (organisation)
0
 
grahamnonweilerCommented:
@GaryC123 - I did read the question, perhaps your abruptness is unintentional, but either way unwarranted! I answered the "requirement"  rather than focus on the "unnecessary".
0
 
Ray PaseurCommented:
I think if this is a query results set, you might be able to SELECT only the data you want or GROUP to get the data collection you want.  If that's not possible with the current table structure, you might add a column that will hold the URL of the link.  Then you would have less programming logic (hence less time wasted debugging and less risk of error).  Just a thought, ~Ray
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now