/    Sign up×
Community /Pin to ProfileBookmark

how can i do this better/smarter??

i am pretty new to php and programming…
but when i look at the code below i think there must be an easier way to do this that involves less code.

[code=php]
$ha = ‘<a href=”sub.php?menu=ha&cat=ha_main”>Håndverk</a>’;
$hu = ‘<a href=”sub.php?menu=hu&cat=hu_main”>Husleie</a>’;
$re = ‘<a href=”sub.php?menu=re&cat=re_main”>Reiser</a>’;
$va = ‘<a href=”sub.php?menu=va&cat=va_main”>Varer</a>’;

echo “<ul>”;
if ($cat == ‘ha_main’){
echo “<li>” . $hu . “</li>”;
echo “<li>” . $re . “</li>”;
echo “<li>” . $va . “</li>”;
}
elseif ($cat == ‘hu_main’){
echo “<li>” . $ha . “</li>”;
echo “<li>” . $re . “</li>”;
echo “<li>” . $va . “</li>”;
}
elseif ($cat == ‘re_main’){
echo “<li>” . $ha . “</li>”;
echo “<li>” . $hu . “</li>”;
echo “<li>” . $va . “</li>”;
}
else{
echo “<li>” . $ha . “</li>”;
echo “<li>” . $hu . “</li>”;
echo “<li>” . $re . “</li>”;
}
echo “</ul>”;
[/code]

does anybody know how i can do this with more intelligent code??
what i am doing is:
if you are on a page with category ha_main then i dont want to show the link for the category that you have already chosen. only the 3 other categories…
see it in action here:
[url]http://www.morganwaage.com/fb/index2.php[/url]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@coppocksDec 02.2006 — EDITED: See so_is_this posted examples!
Copy linkTweet thisAlerts:
@so_is_thisDec 02.2006 — [code=php]$ha = '<a href="sub.php?menu=ha&cat=ha_main">Håndverk</a>';
$hu = '<a href="sub.php?menu=hu&cat=hu_main">Husleie</a>';
$re = '<a href="sub.php?menu=re&cat=re_main">Reiser</a>';
$va = '<a href="sub.php?menu=va&cat=va_main">Varer</a>';

echo "<ul>";
if ($cat != 'ha_main') echo "<li>" . $ha . "</li>";
if ($cat != 'hu_main') echo "<li>" . $hu . "</li>";
if ($cat != 're_main') echo "<li>" . $re . "</li>";
if ($cat != 'va_main') echo "<li>" . $va . "</li>";
echo "</ul>";[/code]
Copy linkTweet thisAlerts:
@so_is_thisDec 02.2006 — For a loop to work, you'd need to restructure your data -- something like this:
[code=php]
$categories = array(
array('ha', 'ha_main', 'Håndverk'),
array('hu', 'hu_main', 'Husleie'),
array('re', 're_main', 'Reiser'),
array('va', 'va_main', 'Varer')
);

echo "<ul>";
foreach ($categories as $entry):
if ($entry[1] != $cat):
echo "<li><a href="sub.php?menu={$entry[0]}&cat={$entry[1]}">{$entry[2]}</a></li>";
endif;
endforeach;
echo "</ul>";[/code]
Copy linkTweet thisAlerts:
@coppocksDec 03.2006 — EDITED: See so_is_this posted examples!
Copy linkTweet thisAlerts:
@so_is_thisDec 03.2006 — Why?[/QUOTE]
The original code, as one example, demonstrated that [B]$cat[/B] contained only the string [B]'ha_main'[/B]:
[code=php]
if ($cat == 'ha_main'){[/code]

While the link value contained an entire HTML string:
[code=php]
$ha = '<a href="sub.php?menu=ha&cat=ha_main">Håndverk</a>';[/code]

Since your example did not change this setup -- but merely loaded that string into an array:
[code=php]
$myList = array($ha,$hu,$re,$va);[/code]

The following statement couldn't possibly work:
[code=php]
if ($listItem != $cat) {[/code]

Why? Because you're comparing two dissimilar strings:
[code=php]
if ('<a href="sub.php?menu=ha&cat=ha_main">Håndverk</a>' != 'ha_main') {[/code]

These would never be equal and, thus, all four links would always be displayed -- which is not what was desired by the OP.
Copy linkTweet thisAlerts:
@coppocksDec 03.2006 — You're absolutely right. What I had in mind was that $myList = array($ha,$hu,$re,$va);

should have been the query strings. Which would not have worked as you pointed out. Gee I screwed that one up! The multidimensional array is indeed the correct approach. I humbly stand corrected!
Copy linkTweet thisAlerts:
@so_is_thisDec 03.2006 — It is easy to overlook things when making a quick post on a site like this. I've done it myself. ?
×

Success!

Help @demiurgen 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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