/    Sign up×
Community /Pin to ProfileBookmark

multiple select box values

I have a multiple select box with contact names and phone numbers. When you select a name that has “Investors” in front of it, I want it to display all the Names and phone numbers, but only show the word Investors once. For example, I want it to look like this:

Invesors: Name Phone Number, Name2, Phone Number 2

Here is the code I am using to seperate the value by a comma:

<? foreach($contacts as $value) {
list ($id, $info) = split (“,”, $value);
echo “<input type=hidden name=contacts[] value=$id,$info>n”;
}?>

Then here is the code I am using to display the data:

<?
echo “Contacts:”;
foreach($contacts as $value) {
list ($id, $info) = split (“,”, $value);
$info = str_replace(“~”, “&nbsp;”, $info);
$inv = substr(“$info”, 0, 9);
//while ($inv == “Investors”) {
// echo “<p>” . $info . “,&nbsp;”;
//}
if ($inv == “Investors”) {
echo “<p>” . $info . “&nbsp;or <a href=mailto:[email protected]>[email protected]</a></p>”;
} else {
echo “<p>” . $info . “</p>”;
}
}
?>

But this gives me the following result:

Investors: Name Phone Number
Investors: Name2 Phone Number

Any help would be greatly appreciated

to post a comment
PHP

22 Comments(s)

Copy linkTweet thisAlerts:
@pyroJun 10.2003 — What does $contacts look like? If it is the contents of a file, post what the file looks like.
Copy linkTweet thisAlerts:
@jrthor2authorJun 10.2003 — $contacts is an array that looks like this:

Contacts:Array ( [0] => 1,Investors:~Jason~Roscoe~717-761-2633 [1] => 5,Investors:~Doug~Silvis~717-761-2633 [2] => 2,Media:~Jason~Kasper~717-761-2633 )
Copy linkTweet thisAlerts:
@pyroJun 10.2003 — Not the cleanest, but I would read/write the file differently, which would mean we could do this differently... Anyway, with what you've got:

[code=php]<?PHP

$x = 0;

echo "Contacts:";
foreach($contacts as $value) {
list ($id, $info) = split (",", $value);
$info = str_replace("~", " ", $info);
$invinfo = substr($info, 11, -1);
$inv = substr("$info", 0, 9);
if ($inv == "Investors") {
if ($x == 0) {
echo "<p>Investors: " . $invinfo . " or <a href=mailto:[email protected]>[email protected]</a></p>";
$x = 1;
}
else {
echo "<p>" . $invinfo . " or <a href=mailto:[email protected]>[email protected]</a></p>";
}
}
else {
echo "<p>" . $info . "</p>";
}
}

?>[/code]
Copy linkTweet thisAlerts:
@jrthor2authorJun 10.2003 — Ok, we're close. This is what the page looks like now:

Investors: Jason Roscoe 777-777-263 or [email][email protected][/email]

Doug Silvis 777-777-263 or [email][email protected][/email]

Media: Jason Kasper 777-777-2633

But what I want it to look like is:

Investors: Jason Roscoe 777-777-2633, Doug Silvis 777-777-2633 or [email][email protected][/email]

Media: Jason Kasper 777-777-2633

Also, it is cutting off the last number in the phone number??

P.S. - I got the phone number to show all numbers by doing this:

$invinfo = substr($info, 11);
Copy linkTweet thisAlerts:
@pyroJun 10.2003 — [code=php]if ($inv == "Investors") {
if ($x == 0) {
echo "<p>Investors: " . $invinfo . " or <a href=mailto:[email protected]>[email protected]</a>,";
$x = 1;
}
else {
echo " " . $invinfo . " or <a href=mailto:[email protected]>[email protected]</a>,";
}
}
else { [/code]
Copy linkTweet thisAlerts:
@jrthor2authorJun 10.2003 — Ok, now I have this:

Investors: Jason Roscoe 777-777-2633 or [email][email protected][/email], Doug Silvis 777-777-2633 or [email][email protected][/email],

But, I want the "or [email][email protected][/email]" to only show up at the end of that line like this:

Investors: Jason Roscoe 777-777-2633, Doug Silvis 777-777-2633 or [email][email protected][/email],

Thanks
Copy linkTweet thisAlerts:
@pyroJun 10.2003 — Is it possible for you to re-work the way the file looks? (I'm assumbing this data is being pulled from a file) It would be much easier if you were able to split it up differently....
Copy linkTweet thisAlerts:
@jrthor2authorJun 10.2003 — Yes, it is possible. the select box is being pulled from a database. The info I am showing is just being passed from the form to this page. I will try anything right now.

Thanks
Copy linkTweet thisAlerts:
@pyroJun 10.2003 — Actually... let's go the easy way, and just keep what you've already got. Try this:

[code=php]<?PHP

$invvals = "";
$infovals = "";
$contacts = file("array.txt");

echo "Contacts:";
foreach($contacts as $value) {
list ($id, $info) = split (",", $value);
$info = str_replace("~", " ", $info);
$invinfo = substr($info, 11, -1);
$inv = substr("$info", 0, 9);
if ($inv == "Investors") {
$invvals .= $invinfo." ";
}
else {
$infovals .= $info;
}
}

echo "<p>Investors: $invvals <a href=mailto:[email protected]>[email protected]</a></p>";
echo "<p>$infovals <a href=mailto:[email protected]>[email protected]</a></p>";

?>[/code]
Copy linkTweet thisAlerts:
@jrthor2authorJun 10.2003 — Actuall, I had to modify it a little, but it looks to be working. here is what I used:

<?PHP

$invvals = "";

$infovals = "";

echo "Contacts:";

foreach($contacts as $value) {

list ($id, $info) = split (",", $value);

$info = str_replace("~", " ", $info);

$invinfo = substr($info, 11);

$inv = substr("$info", 0, 9);

if ($inv == "Investors") {

$invvals .= $invinfo.", ";

}

else {

$infovals .= $info;

}

}

echo "<p>Investors: $invvals or <a href=mailto:[email protected]>[email protected]</a></p>";

echo "<p>$infovals </p>";

?>

Although, is it possible to not show the comma after the last person listed as an investor??
Copy linkTweet thisAlerts:
@pyroJun 10.2003 — Add this right after your foreach loop (before the echo's):

[code=php]$invvals = substr($invvals, 0, -2);[/code]
Copy linkTweet thisAlerts:
@jrthor2authorJun 10.2003 — YEEEEEEEHAWWWW, it's finally working the way I need it to. Thanks so much. Now I have another issue. After putting this info away into a database, I need to retrieve it and display it the same way. Here is my code now, but it lists the investors in a new row:

<?
$sql="SELECT EVT.ITEM_NBR, EVT.TITLE_TXT,
EVT.BODY_TXT, REF.ID, INF.FIRST_NME, INF.LAST_NME, INF.PHONE_NBR,
INF.DEPARTMENT
FROM SYSADM.INDYH_DYNCONTENT_HTML EVT,
SYSADM.INCOR_CONTACT_REF REF,
SYSADM.INCOI_CONTACT_INFO INF
WHERE EVT.ITEM_NBR = $item_nbr
AND EVT.ITEM_NBR = REF.ITEM_NBR
AND REF.ID = INF.ID
ORDER BY DEPARTMENT, INF.LAST_NME";
$D->my_sel($sql);
//print $sql;

if ($D->numrows > 0) {
echo "<p>Contacts:</p>";

for($i=0; $i< $D->numrows; $i++) {
$first_nme=$D->results["FIRST_NME"][$i];
$last_nme=$D->results["LAST_NME"][$i];
$phone_nbr=$D->results["PHONE_NBR"][$i];
$department=$D->results["DEPARTMENT"][$i];
if ($department == "Investors") {
if ($first_nme == "0") {
$contact_info = "<p>" . $department . ":&nbsp;" . $phone_nbr . "&nbsp;or <a href=mailto:[email protected]>[email protected]</a></p>";
} else {
$contact_info = "<p>" . $department . ":&nbsp;" . $first_nme . "&nbsp;" . $last_nme . "&nbsp;" . $phone_nbr . "&nbsp;or <a href=mailto:[email protected]>[email protected]</a></p>";
}
} else {
$contact_info = "<p>" . $department . ":&nbsp;" . $first_nme . "&nbsp;" . $last_nme . "&nbsp;" . $phone_nbr;
}
}
?>


Thanks for all your help!!!
Copy linkTweet thisAlerts:
@jrthor2authorJun 11.2003 — Can anyone please help?
Copy linkTweet thisAlerts:
@pyroJun 11.2003 — Could you explain your problem/question better?
Copy linkTweet thisAlerts:
@jrthor2authorJun 11.2003 — It's pretty much like what we worked on yesterday. I am selecting content from a database and need to display it the same way we did yesterday:

Investors: Name Phone Number, Name2 Phone Number2, etc.

This is how I am getting the data from the database:

$sql="SELECT EVT.ITEM_NBR, EVT.TITLE_TXT,
EVT.BODY_TXT, REF.ID, INF.FIRST_NME, INF.LAST_NME, INF.PHONE_NBR,
INF.DEPARTMENT
FROM SYSADM.INDYH_DYNCONTENT_HTML EVT,
SYSADM.INCOR_CONTACT_REF REF,
SYSADM.INCOI_CONTACT_INFO INF
WHERE EVT.ITEM_NBR = $item_nbr
AND EVT.ITEM_NBR = REF.ITEM_NBR
AND REF.ID = INF.ID
ORDER BY DEPARTMENT, INF.LAST_NME";
$D->my_sel($sql);
//print $sql;

if ($D->numrows > 0) {
echo "<p>Contacts:</p>";

for($i=0; $i< $D->numrows; $i++) {
$first_nme=$D->results["FIRST_NME"][$i];
$last_nme=$D->results["LAST_NME"][$i];
$phone_nbr=$D->results["PHONE_NBR"][$i];
$department=$D->results["DEPARTMENT"][$i];


And right now, it displays each row individually like:

Investors: Name Phone Number

Investors: Name2 Phone Number2

The code looks like this right now when displaying:

if ($department == "Investors") {
if ($first_nme == "0") {
$contact_info = "<p>" . $department . ":&nbsp;" . $phone_nbr . "&nbsp;or <a href=mailto:[email protected]>[email protected]</a></p>";
} else {
$contact_info = "<p>" . $department . ":&nbsp;" . $first_nme . "&nbsp;" . $last_nme . "&nbsp;" . $phone_nbr . "&nbsp;or <a href=mailto:[email protected]>[email protected]</a></p>";
}
} else {
$contact_info = "<p>" . $department . ":&nbsp;" . $first_nme . "&nbsp;" . $last_nme . "&nbsp;" . $phone_nbr;
}
?>
<font face="Arial, Helvetica, sans-serif">
<?=$contact_info?>
<?}
}?>
Copy linkTweet thisAlerts:
@pyroJun 11.2003 — Then it will be close to what we did yesterday. This, perhaps?

[code=php]$contact_info = "";
$contact_info2 = "";
if ($department == "Investors") {
if ($first_nme == "0") {
$contact_info .= $department . ": " . $phone_nbr;
}
else {
$contact_info .= $department . ": " . $first_nme . " " . $last_nme . " " . $phone_nbr;
}
}
else {
$contact_info2 .= $department . ": " . $first_nme . " " . $last_nme . " " . $phone_nbr;
}

echo "<p>$contact_info <a href=mailto:[email protected]>[email protected]</a></p>";
echo "<p>$contact_info2 <a href=mailto:[email protected]>[email protected]</a></p>";[/code]
Copy linkTweet thisAlerts:
@jrthor2authorJun 11.2003 — This code:

for($i=0; $i< $D->numrows; $i++) {
$first_nme=$D->results["FIRST_NME"][$i];
$last_nme=$D->results["LAST_NME"][$i];
$phone_nbr=$D->results["PHONE_NBR"][$i];
$department=$D->results["DEPARTMENT"][$i];
$contact_info = "";

$contact_info2 = "";

if ($department == "Investors") {

if ($first_nme == "0") {

$contact_info .= $department . ": " . $phone_nbr;

}

else {

$contact_info .= $department . ": " . $first_nme . " " . $last_nme . " " . $phone_nbr;

}

}

else {

$contact_info2 .= $department . ": " . $first_nme . " " . $last_nme . " " . $phone_nbr;

}

echo "<p>$contact_info <a href=mailto:[email protected]>[email protected]</a></p>";

echo "<p>$contact_info2</p>";

}

}

Gives me this:

Investors: John Standley 777-777-8857 [email][email protected][/email]


Investors: Kevin Twomey 777-777-6540 [email][email protected][/email]


[email][email protected][/email]

Media: Karen Rugen 777-777-7766
Copy linkTweet thisAlerts:
@pyroJun 11.2003 — What does this give you:

[code=php]$contact_info = "";
$contact_info2 = "";
if ($department == "Investors") {
if ($first_nme == "0") {
$contact_info .= $phone_nbr;
}
else {
$contact_info .= $first_nme . " " . $last_nme . " " . $phone_nbr;
}
}
else {
$contact_info2 .= $first_nme . " " . $last_nme . " " . $phone_nbr;
}

echo "<p>Investors: $contact_info <a href=mailto:[email protected]>[email protected]</a></p>";
echo "<p>Media: $contact_info2 <a href=mailto:[email protected]>[email protected]</a></p>";[/code]

Make sure the echo statements are outside of any of your loops.
Copy linkTweet thisAlerts:
@jrthor2authorJun 11.2003 — I get this;

Investors: [email][email protected][/email]

Media: Karen Rugen 777-777-7766

Using this code:


[code=php] if ($D->numrows > 0) {
echo "<p>Contacts:</p>";

for($i=0; $i< $D->numrows; $i++) {
$first_nme=$D->results["FIRST_NME"][$i];
$last_nme=$D->results["LAST_NME"][$i];
$phone_nbr=$D->results["PHONE_NBR"][$i];
$department=$D->results["DEPARTMENT"][$i];
$contact_info = "";
$contact_info2 = "";
if ($department == "Investors") {
if ($first_nme == "0") {
$contact_info .= $phone_nbr;
}
else {
$contact_info .= $first_nme . " " . $last_nme . " " . $phone_nbr;
}
}
else {
$contact_info2 .= $first_nme . " " . $last_nme . " " . $phone_nbr;
}
} //end for loop
} //end If $D->numrows > 0 loop
echo "<p>Investors: $contact_info <a href=mailto:[email protected]>[email protected]</a></p>";
echo "<p>Media: $contact_info2</p>";
[/code]
Copy linkTweet thisAlerts:
@pyroJun 11.2003 — Try this one out... ?

[code=php]$contact_info = "";
$contact_info2 = "";
if ($D->numrows > 0) {
echo "<p>Contacts:</p>";

for($i=0; $i< $D->numrows; $i++) {
$first_nme=$D->results["FIRST_NME"][$i];
$last_nme=$D->results["LAST_NME"][$i];
$phone_nbr=$D->results["PHONE_NBR"][$i];
$department=$D->results["DEPARTMENT"][$i];
if ($department == "Investors") {
if ($first_nme == "0") {
$contact_info .= $phone_nbr;
}
else {
$contact_info .= $first_nme . " " . $last_nme . " " . $phone_nbr;
}
}
else {
$contact_info2 .= $first_nme . " " . $last_nme . " " . $phone_nbr;
}
} //end for loop
} //end If $D->numrows > 0 loop
echo "<p>Investors: $contact_info <a href=mailto:[email protected]>[email protected]</a></p>";
echo "<p>Media: $contact_info2</p>";[/code]
Copy linkTweet thisAlerts:
@jrthor2authorJun 11.2003 — Yep, thanks a ton!!!!!!! ?
Copy linkTweet thisAlerts:
@pyroJun 11.2003 — Your welcome!

Cheers. ?
×

Success!

Help @jrthor2 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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