Jump to content

Execute Query from Listbox


websponge

Recommended Posts

Hi,

Im starting to write a simple (so I thought! ) project at work, I have no problems with the HTML, CSS or even the php mysql setup, but I am struggling with a simple piece of code!

 

I have a list box, that populates from a table column using the following code:

 

<?php
function select(){
    $query="SELECT appname FROM kpe_apps";
    $result = mysql_query($query);
    echo '<select name="item" onchange="this.form.submit()">';
    while($nt=mysql_fetch_array($result)){
         echo '<option value="' . $nt['id'] . '">' . $nt['appname'] . '</option>';
    }
    echo '</select>';
}
?>

 

I then called the function on the page I needed. which works fine,

now all I need is whatever is selected in the listbox other columns in the same table relating to the selected item are seen.

 

I just cant seem to do it, loads of people are using jquery and other code, surely this can be done in php?

 

any help would really be appreciated..

Many Thanks

 

MOD EDIT: code tags fixed, linefeeds and indenting added.

Link to comment
https://forums.phpfreaks.com/topic/250380-execute-query-from-listbox/
Share on other sites

<?php
function select(){
  $query="SELECT appname FROM kpe_apps";
  $result = mysql_query($query);
  echo '<select name="item" onchange="this.form.submit()">';
  while($nt=mysql_fetch_array($result))
    {
      echo "<option value=\"$nt['id']\">$nt['appname']</option>";
    }
  echo '</select>';
}?>

 

So what happens when you call that function?

 

well its supposed to display all the fields, but it doesnt. I select an app from the dropdown list, for example "payroll" and I want the query to execute so it displays all the fileds for that record.

 

the php for listing all the fields looks like this:

$query="select * from ofcom where appname='Payroll'";

$result = mysql_query($query);

while($row = mysql_fetch_array($result))

  {

  echo "<div id='recordholder'>";

echo "<div id='database-heading'>". $row['appname'] . "</div>"."<div id='description'>" . $row['description'] . "</div>";

echo "<div id='opcatholder'><div id='opcatheader'>Operational Cataogories</div><div id='opcat'><span class='op'>OP CAT 1 - </span>".$row['opcat1'] ."</div><div id='opcat'>

<span class='op'>OP CAT 2 - </span>".$row['opcat2'] ."</div><div id='opcat'><span class='op'>OP CAT 3 - </span>".$row['opcat3'] ."</div></div>";

/*echo "<div id='routingheader'>Routing Information</div>";*/

echo"<div id='info'>".$row['info']."</div>";

echo "<div id='opcatholder'><div id='opcatheader'>Routing</div>".$row['routing']."</div>";

  echo "</div>";

  echo "<br clear='all' />";

  }

 

as you can see, ive manually set it too Payroll so that it works.. I want the listbox to execute the query when I select the app.. really hard to explain and ive been on this all day :(

 

 

thanks for replying

Please post your code within

 . . . 

tags, not PHP manual [m] . . . [/m] tags, and format it so it's readable with proper indenting if you want people to take the time to read it and try to help you.

I tried to edit it! sorry.. stupid IE9!

 

well its supposed to display all the fields, but it doesnt. I select an app from the dropdown list, for example "payroll" and I want the query to execute so it displays all the fileds for that record.

 

the php for listing all the fields looks like this:

$query="select * from ofcom where appname='Payroll'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
  {
  echo "<div id='recordholder'>";
echo "<div id='database-heading'>". $row['appname'] . "</div>"."<div id='description'>" . $row['description'] . "</div>";
echo "<div id='opcatholder'><div id='opcatheader'>Operational Cataogories</div><div id='opcat'><span class='op'>OP CAT 1 - </span>".$row['opcat1'] ."</div><div id='opcat'>
<span class='op'>OP CAT 2 - </span>".$row['opcat2'] ."</div><div id='opcat'><span class='op'>OP CAT 3 - </span>".$row['opcat3'] ."</div></div>";
/*echo "<div id='routingheader'>Routing Information</div>";*/
echo"<div id='info'>".$row['info']."</div>";
echo "<div id='opcatholder'><div id='opcatheader'>Routing</div>".$row['routing']."</div>";
  echo "</div>";
  echo "<br clear='all' />";
  }

as you can see, ive manually set it too Payroll so that it works.. I want the listbox to execute the query when I select the app.. really hard to explain and ive been on this all day :(

 

 

thanks for replying

It is even easier to read when using the [ php][/ php] tags (minus the extra spaces).

 

It is also better to put commands like select in caps too: SELECT * FROM ofcom WHERE appname='Payroll'  ...... easier to bug find later as it is easier to read.

$query="select * from ofcom where appname='Payroll'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
  {
  echo "<div id='recordholder'>";
echo "<div id='database-heading'>". $row['appname'] . "</div>"."<div id='description'>" . $row['description'] . "</div>";
echo "<div id='opcatholder'><div id='opcatheader'>Operational Cataogories</div><div id='opcat'><span class='op'>OP CAT 1 - </span>".$row['opcat1'] ."</div><div id='opcat'>
<span class='op'>OP CAT 2 - </span>".$row['opcat2'] ."</div><div id='opcat'><span class='op'>OP CAT 3 - </span>".$row['opcat3'] ."</div></div>";
/*echo "<div id='routingheader'>Routing Information</div>";*/
echo"<div id='info'>".$row['info']."</div>";
echo "<div id='opcatholder'><div id='opcatheader'>Routing</div>".$row['routing']."</div>";
  echo "</div>";
  echo "<br clear='all' />";
  }

 

 

 

So what gets outputted with just this?

$query="select * from ofcom where appname='Payroll'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
  {
print_r($row);
  }

 

now all I need is whatever is selected in the listbox other columns in the same table relating to the selected item are seen.

 

 

So if your query works fine, then this sounds like an AJAX question.

 

client action > info to server > info back to client from server displaying something new on clients screen

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.