Solved

get listbox selected value from array

Posted on 2008-10-17
20
1,756 Views
Last Modified: 2013-12-13
I am trying to get the selected value from a mysql php dynamic array.  I have gotten the array dumped as a select box, but my foreach $selected part isn't working.
here's what I have so far:
while ($row=mysql_fetch_array($result)) {
$sort_id=$row["faq_admin_sort"];

foreach ($sort_id as $value)
{
if ($value == $sort_id)
{$selected = " selected";}
else
{$selected = "";}
}
$options.="<option value=\"$sort_id\"$selected>".$sort_id."</option>";
}

what am I doing wrong?
Thanks.
0
Comment
Question by:newbe101
  • 10
  • 4
  • 3
  • +1
20 Comments
 
LVL 2

Expert Comment

by:Benji_
ID: 22745651
are you wanting to show the selected value so ie

Currently Selected: BOB
Bob
Dave
Mike
0
 

Author Comment

by:newbe101
ID: 22745656
exactly.
0
 
LVL 2

Expert Comment

by:Benji_
ID: 22745702
May i ask, will the dropdown be on the selected record or are you doing it via a browser uri
0
 

Author Comment

by:newbe101
ID: 22745727
the dropdowns are on multiple records all shown at once.  So, I wouldn't know how I would be able to pass a post or get variable, because the amount so dropdowns will change... Not sure if this answers your question.
0
 

Author Comment

by:newbe101
ID: 22745746
here is the entire page... maybe it will make more sence:


<?

include("../corp_menus/header.php");

include("../corp_menus/connect.php");
 

$query="SELECT * FROM faq_admin ORDER BY faq_admin_sort";

$result=mysql_query($query);

$num=mysql_numrows($result);
 

mysql_close();

?>
 

<b>FAQs Administrator (FAQ Categories)</b><br /><br />

Resort<br /><br />
 

<table width="100%" border="1" cellspacing="0" cellpadding="2">

  <tr>

    <td bgcolor="#CCCCCC" width=35%>Name</td>

    <td bgcolor="#CCCCCC" width=65%>Display Order</td>

  </tr>
 

<?php

/*
 

while ($row=mysql_fetch_array($result)) { 

$sort_id=$row["faq_admin_sort"];

$selected = '';

foreach ($row["faq_admin_sort"] as $faq_admin_sort) {

$selected = ' selected';}

$options.="<option value=\"$sort_id\"".$selected.">".$sort_id."</option>";

}
 
 

while ($row=mysql_fetch_array($result)) { 

$sort_id=$row["faq_admin_sort"]; 

$options.="<OPTION VALUE=\"$sort_id\">".$sort_id;

}
 

*/
 
 

while ($row=mysql_fetch_array($result)) { 

$sort_id=$row["faq_admin_sort"];
 

foreach ($sort_id as $value) 

{ 

if ($value == $sort_id) 

{$selected = " selected";} 

else 

{$selected = "";} 
 

}
 

$options.="<option value=\"$sort_id\"$selected>".$sort_id."</option>";

}
 
 
 
 
 

$i=0;

while ($i < $num) {$faq_admin_id=mysql_result($result,$i,"faq_admin_id");

$faq_admin_id=mysql_result($result,$i,"faq_admin_id");

$faq_admin_name=mysql_result($result,$i,"faq_admin_name");

$faq_admin_sort=mysql_result($result,$i,"faq_admin_sort");
 

?>
 

  <tr>

    <td><? echo "$faq_admin_name"; ?></td>

    <td><select name="faq_admin_sort">

    

<?

echo $options;

?>

</select><? echo "$faq_admin_sort"; ?></td>

  </tr>
 

<?php

++$i;

}

?>
 

</table>
 

<?php

include("../corp_menus/footer.php");

?>

Open in new window

0
 
LVL 8

Expert Comment

by:aldanch
ID: 22745752
I'm confused.

It looks like you are comparing $value to $sort_id (which is an array at this point)
0
 

Author Comment

by:newbe101
ID: 22745773
what i am trying to do is for each record... if the $faq_admin_sort equals the value of the array item ($sort_id), then $selected = "selected"
0
 

Author Comment

by:newbe101
ID: 22745787
my php and mysql knowledge is hanging on by a thread...
0
 
LVL 2

Expert Comment

by:Benji_
ID: 22745795
give me a cpl of mins i will come up with someone for you
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:newbe101
ID: 22745802
cool.
0
 
LVL 2

Expert Comment

by:Benji_
ID: 22745881
I have attached a code snippet from a piece of work i did some time ago, but it should hopefully point you in the right direction.:D

Let me know if it helps:D
 <select name="select" id="select">

          <option value="<?php echo htmlentities($row_DetailRS1['TariffType'], ENT_COMPAT, 'utf-8'); ?>" selected="selected">Currently Selected: <?php echo htmlentities($row_DetailRS1['Tariff'], ENT_COMPAT, 'utf-8'); ?></option>
 

		  <?php

do {  

?>

          <option value="<?php echo $row_phone['TariffType']?>"> <?php echo $row_phone['TariffType']?></option>

          <?php

} while ($row_phone = mysql_fetch_assoc($phone));

  $rows = mysql_num_rows($phone);

  if($rows > 0) {

      mysql_data_seek($phone, 0);

	  $row_phone = mysql_fetch_assoc($phone);

  }

?>

        </select>

   

Open in new window

0
 

Accepted Solution

by:
newbe101 earned 0 total points
ID: 22746716
I figured it out myself:


<?php

$i=0;

while ($i < $num) {$faq_admin_id=mysql_result($result,$i,"faq_admin_id");

$faq_admin_id=mysql_result($result,$i,"faq_admin_id");

$faq_admin_name=mysql_result($result,$i,"faq_admin_name");

$faq_admin_sort=mysql_result($result,$i,"faq_admin_sort");

?>
 

  <tr>

    <td><? echo "$faq_admin_name"; ?></td>

    <td><?

echo "<select>";

for ( $options = 1; $options <= $num; $options += 1) {

if ($faq_admin_sort == $options){$selected=" selected";}else{$selected="";}

	echo "<option value=$options$selected>$options</option>";

}

echo "</select>";	

	 echo "Original Sort Number: $faq_admin_sort"; ?></td>

  </tr>
 

<?php

++$i;

} 

?>

Open in new window

0
 
LVL 13

Expert Comment

by:AielloJ
ID: 22748432
As of late, I've noticed many posts where there has been significant amounts of work (code snippets posted, syntactical errors pointed out, etc),  and reply's by Experts, Guru's, etc with the eventual comment by the originator stating "I figured it out myself" and no points awarded.  Most of them also appear to be by Beginners, and Premium Service Member, so there may be a financial incentive, or misunderstandings of the boards ettiquete.

I haven't spent a lot of time reviewing this post as to whether the posts by the experts group were a significant factor in resolving the poster's issue, but it fits the pattern I've noticed.  Are there any processes in place to assure that experts get the credit due for their work, or is it handled on a case by case basis?  I'm a longtime member, and have recently become active again after a few years hiatus.
0
 

Author Comment

by:newbe101
ID: 22748635
If you have reviewed this thread then you should have noticed that before you, there were 2 contributors.  One of which only contributed a simple question "It looks like you are comparing $value to $sort_id (which is an array at this point)".  The other one asked a series of simple questions and then posted a code snipplet of some work that he had previously completed and that (as far as my untrained eyes can tell) was completely unrelated.
Now, if you took some time to view my history (multiple year membership), you will notice that I am quite willing to distribute points to people who deserve it.
And, yes, I pay for a premium membership, which happens to mean that I really could care less if I distribute points or not.  It makes no difference to my subscription either way.  So, in good faith, and appretiation of good help, I am more that pleased to let somebody know my appretiation by providing them with points that cost me nothing extra.
0
 

Author Comment

by:newbe101
ID: 22748652
And, BTW, compair all of my code to the single post of code.  See any similarities?  I don't.  It's an obvious, "Here's something I did a long time ago, maybe it will help..." posting.  As a matter of fact, read the message that accompanies the code snipplet.  It says it all.
0
 
LVL 13

Expert Comment

by:AielloJ
ID: 22748978
newbe101:

As I stated, my post is an observation of trends that Ihave spotted recently, and was hoping it would post in the Community Forum for the moderators to respond to.  That being said, I feel you're being disingenuous in your indignation.  In your post of 10.17.2008 at 05:02PM EST, ID: 22745746, you published a code snippet:

  if ($value == $sort_id)
  {$selected = " selected";}
  else
  {$selected = "";}

To which user aldanch pointed out an error:

  It looks like you are comparing $value to $sort_id (which is an array at this point)

which is exactly my point.  You posted code and asked for help.  I'm assuming you presented this code to the board as your solution in progress.  A user pointed out an error, yet he or she didn't get even partial points for their contribution.  The fact that you subsequently rewrote the code is irrelevent to whether some points should be awarded.  The member spotted an error in what you presented, and should get some consideration.  You also mentioned you don't think consideration is warranted for responses from work done long ago.  Once again, if the prior work someone posts is helpful to you, it doesn't matter how long ago it was generated.  If it helps solve your issue, then they deserve points.

As far as good faith goes, someone who posts a response pointing out a coding error on your part that is relevant to what you posted, deserves credit whether you totally rewrote it or not.  I have no dog in this fight.  I reviewed your question as one I may be able to help in and saw you were already in good hands.  Your responses about whether ANY points should awarded is what my primary objection is about.  My objection to this type of closure stands, and I would ask the moderators to comment on what the policy is or should be.
0
 

Author Comment

by:newbe101
ID: 22749911
I understand what you are saying, however, being new to php and mysql, I don't fully understand aldanch's response; which should be obvious in my replying statement.  Also, the code snippet which Benji_ attached didn't make any sense to me.  I ran through it for a couple of hours and didn't see (and still don't see) how it is related to my issue.  Therefor I think no points should be awarded.

I can just award points to close this thread, but I think it defeats the purpose of this site.  People searching for solutions generally want acceptable ones.  So, my steps to close this thread without awarding any points fits this sites scheme of valuable content.
0
 
LVL 13

Expert Comment

by:AielloJ
ID: 22750058
I wasn't my intent to throw rocks in your particular direction or single you out.  I've just seen many posts lately with many contributions, and no points awarded.  This one appears similar.  From the wording of the text box for filing objections, I thought my objection would end up somewhere for general discussion on what the rules are or should be, not linked in any way directly to your post, and not specifically about you.

I know what you mean about not understanding some of the reply's.  One thing I'm impressed with here is the willingness of most of the experts to restate their explanations in clearer wording when asked.  I agree that posts should explain the underlying concept clearly, and politely provide clarification when asked.  After 2 or 3 hours of trying to figure it out, I think you're entitled to ask for a clarification.   Reponses should also fit the level of expertise.  My explanations to beginners are more detailed that I would provide someone of expert level, and expect the same for questions I post.  As an instructor and trainer I think that's the best way to conduct things.  After all, points are a type of currency here and they should be well earned.  Just last week I provided approximately 18 responses to someone and covered what probably should have been several questions before it was closed.  I really earned those points!!  I do believe the user that pointed out the code bug deserves some credit, but that's just my opinion.

I'm glad we were able to have a fairly even discussion of our viewpoints.  I'm still curious about what the protocol is.  If it's vague or not defined it leads to issues like this.  I'm still hoping that the objection triggers a response from a moderator outlining what the proper thing to do is.  Close your post, there'll be no further objections filed.  Much success with your project!!

JRA
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now