Adventure Game Studio

Community => General Discussion => Topic started by: Erica McLane on Thu 10/04/2003 13:53:51

Title: Need help from a PHP/MySQL expert (I have problems with the given code)
Post by: Erica McLane on Thu 10/04/2003 13:53:51
How can I fill a drop-down style listbox with data from a MySQL table?
Thanks in advance.

Read below!
Title: Re:Need help from a PHP/MySQL expert
Post by: n3tgraph on Thu 10/04/2003 14:12:13
PHP itself doesn't support pull down stuffies, so it all depends on the flash / javascript your using

if you have a javascript pulldown menu, you could loop the data from the mysql query in the javascript which will create your pull down menu

- oh wait sorry, I read it wrong....
ummm

/me thinks

You mean a listbox like this?

<select name="listbox">
                 <option value="option1">option1
      <option value="option2">option2
      <option value="option3">option3
      <option value="option4">option4     
      </select></td>

if so, tell me, it is quite easy
Title: Re:Need help from a PHP/MySQL expert
Post by: Erica McLane on Thu 10/04/2003 14:25:14
Yes, the same thing. Can you tell me?
Title: Re:Need help from a PHP/MySQL expert
Post by: Spyros on Thu 10/04/2003 21:42:40
What kind of table is the one you want to get the data?
Do you want all the columns of one entry or a column of all entries?
Title: Re:Need help from a PHP/MySQL expert
Post by: Erica McLane on Fri 11/04/2003 11:27:30
Let`s say I have table1 with two columns - test1 and test2. I want to have two listboxes which will contain the data from test1 and from test2.

listbox1 will contain the data from test1 and listbox2 will contain the data from test2.

How can I do this?
Title: Re:Need help from a PHP/MySQL expert
Post by: Klaus on Fri 11/04/2003 15:49:47
Hi Erica,
this script should fit your needs:

<?php
// database: ags // table: table1 // fields: test1, test2

// connect
$mc = mysql_connect("localhost","user","pass") or die("error");
$ms = mysql_select_db("ags") or die("error");
$mq = mysql_query("select * from table1") or die("error");

// read and put into arrays
$count = 0;
while($mf = mysql_fetch_array($mq)) {
 $table['test1'][$count] = $mf['test1'];
 $table['test2'][$count] = $mf['test2'];
 $count++;
}
?>

<html>
 <body>
   <form action="yourscript.php" method="post" name="form">
     <select name="list1">

<?php
// show every test1 in option-tag // output is $list1
 foreach ($table['test1'] as $optvalue)
   print "<option value=\"".$optvalue."\">".$optvalue."</option>";
?>

     </select>
     

     <select name="list2">

<?php
// show every test2 in option-tag // output is $list2
 foreach ($table['test2'] as $optvalue)
   print "<option value=\"".$optvalue."\">".$optvalue."</option>";
?>

     </select>
     

     <input type="submit" name="submit" value="submit" />
   </form>
 </body>
</html>
Title: Re:Need help from a PHP/MySQL expert
Post by: Erica McLane on Sat 12/04/2003 10:06:23
I`ve put this code but I have some problems:

Parse error: parse error, unexpected T_VARIABLE in /home/slaveino/public_html/alphasys/con.php on line 12

CODE:

<?php
// database: ags // table: table1 // fields: test1, test2

// connect
$mc = mysql_connect("localhost","slaveino_usernam","password") or die("error");
$ms = mysql_select_db("slaveino_databaseName") or die("error");
$mq = mysql_query("select * from conf_test3") or die("error");

// read and put into arrays
$count = 0;
while($mf = mysql_fetch_array($mq)) {
  $table['proc_name'][$count] = $mf['proc_name'];
  $table['main_name'][$count] = $mf['main_name'];
  $count++;
}
?>

<html>
  <body>
    <form action="yourscript.php" method="post" name="form">
      <select name="list1">

<?php
// show every test1 in option-tag // output is $list1
  foreach ($table['proc_name'] as $optvalue)
    print "<option value=\"".$optvalue."\">".$optvalue."</option>";
?>

      </select>
     

      <select name="list2">

<?php
// show every test2 in option-tag // output is $list2
  foreach ($table['main_name'] as $optvalue)
    print "<option value=\"".$optvalue."\">".$optvalue."</option>";
?>

      </select>
     

      <input type="submit" name="submit" value="submit" />
    </form>
  </body>
</html>



I`ve also tried this code. The first listbox worked, but the second was empty.

<?
$host = "localhost";
$databasename = "slaveino_databaseName";
$tablename = "conf_test3";
$username = "slaveino_usernam";
$password = "password";

$db = mysql_connect("$host", "$username", "$password");
@mysql_select_db("$databasename", $db);
$query="SELECT * FROM $tablename proc_name";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;

?>
<form>
<select name="Procesori">
<?
while ($i < $num) {
$proc_name=mysql_result($result,$i,"proc_name");

echo "<option value=\"$proc_name\">$proc_name</option>";

++$i;
}

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

echo "<option value=\"$main_name\">$main_name</option>";

++$i;
}
?>




<input type="submit" value="Poruchai">
<input name="action" type="hidden" value="add">   <input type="reset" value="Izchisti">
</form>


Where is my mistake?
Title: Re:Need help from a PHP/MySQL expert (I have problems with the given code)
Post by: on Mon 14/04/2003 08:19:31
I still need help.
Title: Re:Need help from a PHP/MySQL expert (I have problems with the given code)
Post by: n3tgraph on Mon 14/04/2003 08:30:09
hmmm, I tried to give you some code earlier :) but anyways:

<?php

  $query = mysql_query("select * from blablabla")
     or die(mysql_error());

  echo '<select name="blabla">';

 
  while($row = mysql_fetch_array($query))
  {
        ?>
        <option value="<?php echo $row["test1"]; ?>"><?php echo $row["test1"];
  }

?>
</select>


Title: Re:Need help from a PHP/MySQL expert (I have problems with the given code)
Post by: Erica McLane on Mon 14/04/2003 14:08:14
And this should work for more than one listboxes, right?
I will test it and will report any porblems, thanks.
Title: Re:Need help from a PHP/MySQL expert (I have problems with the given code)
Post by: Erica McLane on Mon 14/04/2003 14:52:39
I`ve tried it but it gave me unknown T_WHILE in the line where while is located.
Title: Re:Need help from a PHP/MySQL expert (I have problems with the given code)
Post by: n3tgraph on Mon 14/04/2003 15:16:30
okay, I tested it and it works like a clock:

<html>
<body>

<?php

 include('dbconnect.php');  // this ufcuz contains your dbase connections

 $query = mysql_query("select * from geslaagd")    //'geslaagd' could be whatever table you want to select
     or die(mysql_error());

 echo '<select name="blabla">';    //blabla will be the name of your listbox

 
 while($row = mysql_fetch_array($query))
 {
       ?>
       <option value="<?php echo $row["naam"]; ?>"><?php echo $row["naam"];     //naam is a field in my table
 }

?>
</select>

</body>
</html>

so there you go
Title: Re:Need help from a PHP/MySQL expert (I have problems with the given code)
Post by: Erica McLane on Mon 14/04/2003 15:24:40
Thanks! It worked!  :)
Title: Re:Need help from a PHP/MySQL expert (I have problems with the given code)
Post by: Klaus on Fri 18/04/2003 18:14:56
Sorry, I was away from home for one week now with no internet at all. Your problem is already solved but I want to add some lines just to make it all complete. So here's my late response:

-1- Problem with my script

I didn't expect a problem here but depending on the used PHP installation you can surround the field names with ' and/or " in the lines 12, 13, 25 and 35
( $table['test1'][$count] = $mf['test1']; // line 12 )
Putting these to " in the whole code should have fixed the problem
( $table["test1"][$count] = $mf["test1"]; )

-2- Difference between the scripts

The version you now are using always opens and reads the database each time a new selectbox is created. My script does it all only once and keeps the information in memory. Both is possible, but you should open the database ( include('dbconnect.php'); ) only at the beginning of the code because it keeps connected until the end of the script is reached or until you close it by function. Perhaps you already use it that way.