LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2012, 06:07 AM   #1
sunlinux
Member
 
Registered: Feb 2006
Distribution: RHCL 5
Posts: 239

Rep: Reputation: 30
want to display database values in select box using php


$query = "select * from sun";

$result=mysql_query($query);

$num=mysql_numrows($result);

echo "<option value=\"".$num['id']."\"";
echo " selected=\"selected\"";
echo ">".$num['name']."\n";


but above not working
 
Old 08-07-2012, 10:40 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
You either have not posted enough of your code or you are missing statements that actually get the data from the resultant query. $num is just the number of rows returned by the query and not the actual data.

What are you trying to accomplish?
 
Old 08-07-2012, 11:50 PM   #3
sunlinux
Member
 
Registered: Feb 2006
Distribution: RHCL 5
Posts: 239

Original Poster
Rep: Reputation: 30
Suppose my query returns ID and Name column , I want to store ID values in a select box and display it on webpage using php.
 
Old 08-08-2012, 03:26 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
The query
Code:
select PK_grpsrvc, FK_srvc, service, FK_grp, groupname, has_timetable from group_srvc left join services on PK_srvc=FK_srvc left join groups on PK_grp=FK_grp order by service, groupname
The code that generates and runs the above query and stores the result in an array
Code:
   291  /******************************************************************************
   292  NAME:       get_groupservices
   293  FUNCTION:   get groupservices
   294  IN:
   295      dbcnx       database connection
   296      where       where clause
   297      order       sort order
   298      limit       limit
   299  OUT:
   300      msg         return message
   301      allgroupservices array with records for each groupservice; key is PK_grpsrvc
   302  RETURN:
   303      query result
   304  NOTE:
   305      html safe
   306      'active' hardcoded to 'y'
   307  ******************************************************************************/
   308  function get_groupservices($dbcnx,&$msg,&$allgroupservices,$where="",$order="",$limit="")
   309  {
   310  
   311      $what  = "PK_grpsrvc, FK_srvc, service, FK_grp, groupname, has_timetable";
   312      $from  = "group_srvc ";
   313      $from .= "left join services on PK_srvc=FK_srvc ";
   314      $from .= "left join groups on PK_grp=FK_grp ";
   315
   316      disp_msg("<p class=\"qrytxt bold\">get_groupservices()</p>");
   317      $rc=query_table($dbcnx,$msg,$from,$what,$where,$order,$limit);
   318      if(!$rc)
   319          return $rc;
   320
   321      while($result=mysql_fetch_assoc($rc))
   322      {
   323          $result['active']="y";
   324          $allgroupservices[$result['PK_grpsrvc']]=$result;
   325      }
   326
   327      return $rc;
   328  }
Line 317 executes the actual query and returns the resource (query_table basically calls mysql_query()); you can replace this with your own code. In line 321 to 325 I fetch the records and store them in an associative array allgroupservices (forget about line 323, just for my own needs). The index is PK_grpsrvc; in your case that can / will be the ID that you mention.

So you will now have an array index by a key and each entry containing a full record as pulled from the database (specified in $what)

This is how it's called ($order was set earlier in the code)
Code:
    44  // connect to incident database
    45  ////////////////////////////////////
    46  $dbcnx=connect($msg);
    47  if(!$dbcnx)
    48  {
    49      $alt="<p class=\"db_error\">A technical problem prevented access to the database</p>";
    50      disp_msg($msg,$alt);
    51      do_htmlfooter();
    52      exit;
    53  }
    54
    55  // get all records
    56  ////////////////////////////////////
    57  $rc=get_groupservices($dbcnx,$msg,$allgroupservices,"",$order,"");
    58  if(!$rc)
    59  {
    60      $alt = "<p class=\"db_error\">Error querying the database</p>";
    61      $msg = "<p class=\"db_error\">$msg</p>";
    62      disp_msg($msg,$alt);
    63      mysql_close($dbcnx);
    64      $dbcnx=0;
    65      do_htmlfooter();
    66      exit;
    67  }
    68  else
    69  {
    70      $msg = "<p class=\"db_ok\">$msg</p>";
    71      disp_msg($msg);
    72  }
After this, you can loop through your result set (mine is called allgroupservices) and populate the combobox. I have code for it but it's overkill for your purpose. Basically
Code:
echo "<select>\n";
foreach($allgroupservices as $id => $dta)
{
  echo "<option value=\"$id\">";
  echo htmlentities($dta[$display]) . "</option>\n";
}
echo "</select>\n";
$display indicates the field (in the record set) that I want to display; e.g the primary key, the group or the service. The result looks like below (I combined group and service in another part of the code and added it to the record(s)).

Code:
<select>
<option value="10">CSN..Bez Valley</option>
<option value="19">CSN..Bloemfontein</option>
<option value="1">CSN..Brixton</option>
...
...
<option value="264">MND..Worcester</option>
<option value="239">MND..Zeerust</option>
</select>
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl on line to take out select values casperdaghost Linux - Newbie 9 02-01-2010 12:37 AM
need a php script to format and display a select amount of text from a file steve51184 Linux - Software 27 01-27-2009 02:29 PM
How To Put Values In Select Box From Database In Jsp nimishabhatia Programming 1 07-20-2005 01:16 PM
Select Box, PHP agallant Programming 8 08-04-2004 01:15 PM
php: display timestamp in editable dropdown box? opioid Programming 5 07-28-2004 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration