/    Sign up×
Community /Pin to ProfileBookmark

Hey, I’ve never had to use an array before today, and now I feel like I have a fairly complicated situation where arrays will be necessary.

Basically, I’m pulling information from a database (don’t worry, I don’t have any questions involving databases). I want to fill an array(s) with information from the database so I can be sure to prevent duplicates from appearing.

To be exact, I’m filling a <select> (dropdown menu) with categories from my database. It’s pretty easy to do this without arrays with one category, but the problem is, my database entries can have up to 3 categories each. So I want to pull all of these categories from the database, store them in the array (without duplicates) and list them in the <select>.

Here’s the code I have to fill the <select> using just 1 category. Could someone help me also include category2, and category3, without duplicates?

[code=php]$result = mysql_query($query);
$num = mysql_numrows($result);
$i = 0;
$prev = “”;
echo “<form name=”select_category” action=”directory.php” method=”GET”>n
<input type=”hidden” name=”member_type” value=”$member_type”>n
<select name=”category”>n
<option value=”all” selected>View All Categories</option>n”;
while ($i < $num) {
$category = mysql_result($result, $i, “category”);
if ($category != $prev) {
$prev = $category;
echo “<option value=”$category””;
if ($category == $_GET[‘category’]) {
echo ” selected “;
}
echo “>$category</option>n”;
}
$i++;
}
echo “</option>n
<input type=”submit” name=”submit” value=”View This Category”>n
</form>n”;[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiMar 10.2010 — Aren't all your categories in a single table (if not that's your real problem)? You can use the SQL DISTINCT keyword to return a unique list, though again in a properly normalized database structure you would have a table with each category listed only once anyway. More info about your db structure is needed I think.
Copy linkTweet thisAlerts:
@OctoberWindMar 10.2010 — Can I see what $query is, or better yet, what your database tables/content, and what the dataset is you're trying to pull?

There might be a simpler/easier way then stepping through the result set, counting rows.
Copy linkTweet thisAlerts:
@BeakersworthauthorMar 10.2010 — I always give up too soon ? after I posted this thread I started looking up information on arrays and figured it all out without too much trouble. For those interested, here is the working code:

[code=php]$result = mysql_query($query);
$num = mysql_numrows($result);
$i = 0;
$categories = array();
while ($i < $num) {
$category1 = mysql_result($result, $i, "category1");
$category2 = mysql_result($result, $i, "category2");
$category3 = mysql_result($result, $i, "category3");
$category4 = mysql_result($result, $i, "category4");
$category5 = mysql_result($result, $i, "category5");
if ($category1 != "") {
$categories[] = $category1;
}
if ($category2 != "") {
$categories[] = $category2;
}
if ($category3 != "") {
$categories[] = $category3;
}
if ($category4 != "") {
$categories[] = $category4;
}
if ($category5 != "") {
$categories[] = $category5;
}
$i++;
}
$categories = array_unique($categories);
sort($categories);
echo "<form name="select_category" action="directory.php" method="GET">n
<input type="hidden" name="member_type" value="$member_type">n
<select name="category">n
<option value="all" selected>View All Categories</option>n";
$i = 0;
$categorynum = count($categories);
while ($i < $categorynum) {
echo "<option value="$categories[$i]"";
if ($categories[$i] == $_GET['category']) {
echo " selected ";
}
echo ">$categories[$i]</option>n";
$i++;
}
echo "</option>n
<input type="submit" name="submit" value="View This Category">n
</form>n";[/code]
×

Success!

Help @Beakersworth 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.26,
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,
)...