/    Sign up×
Community /Pin to ProfileBookmark

Help with my array ??

Hi all,
I am still learning about using these arrays! ?

I am using the code below to create an array that I use in a select box.
So that it looks nicer I want to have 2 elements in the array – one that will be
the stored variable and the other that will be displayed in the select box.

Here is my code (so far):

[code=php]
$sql = “SELECT DISTINCT account_name,SO_num FROM $client_table
WHERE account_type =’S_ORD’ ORDER BY ‘account_name,SO_num,prod'”;

$result = mysql_query($sql)
or die(“Couldn’t execute MAKE CUST LIST query.”);

while($row = mysql_fetch_assoc($result)) {
$optionsAll[] = “{$row[account_name]}/-/{$row[SO_num]}”;
} // end while

$options = array_unique($optionsAll);

// some HTML table formating goes here//

<select name=’sal_ord’>
<?php
foreach($options as $value)
{
echo “<option value=’$value’>$value</option>n”;
} // end for loop
[/code]

Now I guess that the created array “$options” contains just the counter ( 0 – whatever ) and one element
which is $row[account_name] /-/ $row[SO_num]

For display purposes I want another element in the array that will be:
” $row[account_name] – $row[SO_num]”.

If I want to use this element in my SELECT box using the var name $disp like this:

[code=php]
<select name=’sal_ord’>
<?php
foreach($options as $value)
{
echo “<option value=’$value’>$disp</option>n”;
} // end for loop
[/code]

How do I create this extra element in the while loop and
how do I use it in the “foreach($options as $value) ” statement ???

Thanks for your help !

David.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 20.2005 — [code=php]
<?php
$sql = "SELECT DISTINCT account_name,SO_num FROM $client_table " .
"WHERE account_type ='S_ORD' ORDER BY 'account_name,SO_num,prod'";
$result = mysql_query($sql)
or die("Couldn't execute MAKE CUST LIST query.");

while($row = mysql_fetch_assoc($result))
{
$options[$row['SO_num']] = $row['account_name'];
} // end while
# your DISTINCT in the query makes this redundant:
# $options = array_unique($optionsAll);

// some HTML table formating goes here//
?>
<select name='sal_ord'>
<?php
foreach($options as $key => $value)
{
echo "<option value='$key'>$value - $key</option>n";
} // end for loop
?>
</select>
[/code]

However, if this is all you need the array for, I'd just "cut out the middle man, since you essentially already have it in $result:
[code=php]
<?php
$sql = "SELECT DISTINCT account_name,SO_num FROM $client_table " .
"WHERE account_type ='S_ORD' ORDER BY 'account_name,SO_num,prod'";
$result = mysql_query($sql)
or die("Couldn't execute MAKE CUST LIST query.");

// some HTML table formating goes here//
?>
<select name='sal_ord'>
<?php
while($row = mysql_fetch_assoc($result))
{
echo "<option value='{$row['SO_num']}'>{$row['account_name']} - {$row['SO_num']}</option>n";
} // end while
?>
</select>
[/code]
×

Success!

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