Hi. I'm trying to get my script to take the options a user selects from a list box over to a second list box. I managed to get them move over to the second from the first.
I thought that this code should work, but for some reason it has a weird problem.
When I run it it will put certain items back from one box into the original and then needs me to add them again, so whilst it is possible to empty the list on the left into the right some need to be added several times and I'm stumped as to why it might be doing this.
Here is my code:
<?
//list.php
include ("db_connect.php");
$dowhat=$_POST['submit'];
$filter=trim($_POST['filter']);
$selecteddocs=$_POST['publications'];
$listselectedocs=$_POST['addedpublications'];
$preselecteddocs=$_POST['selected'];
$usestyle=$_POST['styles'];
$order=$_POST['order'];
$runtot=$_POST['runtot'];
$total=$_POST['total'];
$array=array();
$counter=0;
while($counter < $total){
$counter++;
$var = $_POST['selected'] . $counter;
if(!in_array($var,$array)){
$array[].=$var;
}
}
$counter=0;
if(isset ($selecteddocs)){
foreach ($selecteddocs as $docid){
if(!in_array($docid,$array)){
$array[].=$docid;
$counter++;
}
}
}
array_unique($array);
if ($dowhat=="Remove"){
foreach ($listselecteddocs as $docid){
unset ($array[in_array($docid)]);
}
}
?>
<html>
<head>
<title>Publications Database - Create List</title>
<link href="main.css" rel="stylesheet" type="text/css">
</head>
<body class="bodystyle">
<h1 class="xbig">Publications Database - Create List</h1>
<h2 class="medium">Create a list of publications to export using this form. If you get confused about how to use it why not consult the <a href="listhelp.php">help</a>.</h2>
<br>
<form action="list.php" method="post">
<?
$counter=0;
if(isset ($array)){
foreach($array as $item){
$counter++;
echo '<input name="selected' . $counter . '" type="hidden" value="' . $item . '">';
echo '<input name="total" type="hidden" value="' . $counter . '">';
}
}
?>
<div align="left">
1) To start please choose a style to apply to this list:
<select name="styles">
<?
$stylequery=mysql_query("SELECT * FROM style");
while ($stylename = mysql_fetch_array($stylequery)){
if ($usestyle==$stylename["styleid"]){
echo '<option value ="' . $stylename["styleid"] . '" selected>' . $stylename["stylename"] . '</option>';
} else {
echo '<option value ="' . $stylename["styleid"] . '">' . $stylename["stylename"] . '</option>';
}
}
?>
</select>
<br><br><br>
2) Now please select which fields to papers to include:
<br><br>
<input name="filter" type="text"> <input name="submit" type="submit" value="Filter">
<br><br>
<table width="90%">
<tr>
<td>
<select name="publications[]" size="10" multiple>
<?
if($dowhat=="Filter"){
$docquery=mysql_query("SELECT * FROM docs WHERE title LIKE '%$filter%' order by title") or die("Fatal error: failed to run query on 'list.php' L44");
while ($docname = mysql_fetch_array($docquery)){
if(!in_array($docname["docid"], $array)){
echo '<option value="' . $docname["docid"] . '">' . substr($docname["title"],0,35) . ' (' . $docname["year"] . ')' . '</option>';
}
}
} else {
$docquery=mysql_query("SELECT * FROM docs");
while ($docname = mysql_fetch_array($docquery)){
if(!in_array($docname["docid"], $array)){
echo '<option value="' . $docname["docid"] . '">' . substr($docname["title"],0,35) . ' (' . $docname["year"] . ')' . '</option>';
}
}
}
?>
</select>
<input name="submit" type="submit" value="Add">
</td>
<td>
<select name="addedpublications" size="10" multiple>
<?
if($dowhat=="Add"){
if(isset ($array)){
foreach($array as $item){
$docinfo=mysql_fetch_array(mysql_query("SELECT * FROM docs WHERE docid='$item'"),1);
echo '<option value="' . $docinfo["docid"] . '">' . substr($docinfo["title"],0,35) . ' (' . $docinfo["year"] . ')' . '</option>';
}
}
if(isset ($selecteddocs)){
foreach ($selecteddocs as $docid){
if(!in_array($docid,$array)){
$docinfo=mysql_fetch_array(mysql_query("SELECT * FROM docs WHERE docid='$docid'"),1);
echo '<option value="' . $docinfo["docid"] . '">' . substr($docinfo["title"],0,35) . ' (' . $docinfo["year"] . ')' . '</option>';
}
}
}
}
?>
</select>
<input name="remove" type="submit" value="Remove">
</td>
</tr>
</table>
<br><br>
3) Now choose how you would like your outputted list to be ordered:
<br><br>
Alphabetically (by title): <input name="order" type="radio" value="alphaname" <? if ($order=="alphaname"){ echo "checked"; } ?>> Alphabetically (by author): <input name="order" type="radio" value="alphaauth" <? if ($order=="alphaauth"){ echo "checked"; } ?>> Chronologically: <input name="order" type="radio" value="chron" <? if ($order=="chron"){ echo "checked"; } ?>> As selected: <input name="order" type="radio" value="as" <? if ($order=="as"){ echo "checked"; } ?>>
<br><br><br>
4) Ready to create your list?
<input name="go" type="button" value="Go">
</div>
</form>
<?
include('footer.php');
?>
</body>
</html>
If anyone can suggest what might be wrong with it I'd be very grateful