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
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?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
  }

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.