Link to home
Start Free TrialLog in
Avatar of jayseena
jayseenaFlag for United Arab Emirates

asked on

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
Avatar of Gary
Gary
Flag of Ireland image

$string = '$profile';
should be

$string =$profile;


Though I don't know why you are assigning a variable to another variable
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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;
Avatar of jayseena

ASKER

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
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)
@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".
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