/    Sign up×
Community /Pin to ProfileBookmark

If isset & equal OR not at all.

Crazy title. But I’m looking through some results.

If a GET is set (isset) I want to filter to results only of that ID otherwise show all.

` if (isset($_GET[‘ID’]) && $Person->getFunctionID() == $_GET[‘ID’]){`

I’m I’m looping through a student list. If the ID is set as a GET then check if the property is set to the same ID.

IF no ID is set via GET then show them all.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NachfolgerDec 06.2020 — @kiwis80#1625646

Assuming you're looping through an array,

``PHP<i>
</i>$functionID = (isset($_GET['ID']) &amp;&amp; !empty($_GET['ID']) &amp;&amp; is_numeric($_GET['ID']) ? $_GET['ID'] : 0);

foreach ($Persons as $Person) {
if ($functionID != 0 &amp;&amp; $Person-&gt;getFunctionID() == $functionID) {
echo "ID was set, and matches Person function ID";
} else if ($functionID == 0) {
echo "functionID was not set";
}
}<i>
</i>
``


Might be subject to revisions, just a generic example.
Copy linkTweet thisAlerts:
@wdmangaloreDec 06.2020 — Good one
Copy linkTweet thisAlerts:
@NogDogDec 06.2020 — Couldn't think of a really concise way to do it, and gave up after coming up with this, FWIW:
<i>
</i>$personID = $Person-&gt;getFunctionID();
if(!isset($_GET['ID'])) {
do_thing($theData);
}
elseif($_GET['ID'] == $personID) {
do_thing($theData, $personID));
}

function do_thing($data, $id=null) {
foreach($data as $row) {
if(empty($id) or $id == $row['some_id']) {
// do thing with this row
}
}
}
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...