Page 1 of 1

Displaying selected options in multiple select list

Posted: Thu May 23, 2002 11:05 am
by Crashin
I'm displaying records to users via a form where they can select multiple applications to tie to the record. I'm associating multiple applications with a single record and I've got to somehow loop through the array of associated applications and select them in the multiple select box when displaying a record to the end user for editing. The closest I've come so far is shown below:

Code: Select all

//query to determine which apps were initially assigned to the record
$query_app = "SELECT app_id FROM complete_record WHERE kb_record_id='$recid'";
$result_app = mysql_query($query_app);
$row_app = mysql_fetch_array($result_app);

//query for list of applications
$query = "SELECT * FROM application ORDER BY app_name";
$result = mysql_query($query);
if(!$result) {
    echo "Cannot perform query.";
    exit;
}
else {
    echo "<td><font class='small'>New Applications:</font></td>";
    echo "<td>";
    echo "<select name='new_app_id' size='5' multiple>";
    while($row = mysql_fetch_array($result)) &#123;
        if($row_app&#1111;0] == $row&#1111;0]) &#123;
            echo "<option value='".$row&#1111;0]."' selected>".$row&#1111;1]."</option>";
        &#125;
        else &#123;
            echo "<option value='".$row&#1111;0]."'>".$row&#1111;1]."</option>";
        &#125;
    &#125;
    echo "</select>";
    echo "</td>";
&#125;
I'm fairly sure that the loop for the $row_app array needs to go inside of the loop for the $row array, however when I've tried that it either won't display anything on the page or fails to select the applications that it should be selecting.

**BUMP**

Posted: Tue May 28, 2002 1:37 pm
by Crashin
I'm bumping this topic back to the top in hopes of getting a reply...

Posted: Tue May 28, 2002 5:41 pm
by mydimension
it looks like your problem is that your not changing the index for row_app[]. your always checking against row_app[0]. its hard to say though cause im seeing this code out of context. other than taht your code should work fine.

Posted: Tue May 28, 2002 5:50 pm
by Crashin
Well, I've played around a bit more and still...the closest I've come is as follows:

Code: Select all

echo "<td>";
	echo "<select name='new_app_id&#1111;]' size='5' multiple>";
	for($i=0; $i < count($row_app); $i++) &#123;
		while($row = mysql_fetch_array($result)) &#123;
			if($row_app&#1111;$i] == $row&#1111;0]) &#123;
				echo "<option value='".$row&#1111;0]."' selected>".$row&#1111;1]."</option>";
			&#125;
			else &#123;
				echo "<option value='".$row&#1111;0]."'>".$row&#1111;1]."</option>";
			&#125;
		&#125;
	&#125;
	echo "</select>";
echo "</td>";
Refer to the code in my first post for more info. about where the $row_app variable comes in. My form still only comes up with the first application selected, as opposed to all of the apps that were associated with the original record. Any ideas?

Posted: Tue May 28, 2002 6:02 pm
by mydimension
that still won't work, try this...

Code: Select all

&lt;?php
      $i = 1;
      while($row = mysql_fetch_array($result)) { 
         if($row_app&#1111;$i] == $row&#1111;0]) { 
            echo "&lt;option value='".$row&#1111;0]."' selected&gt;".$row&#1111;1]."&lt;/option&gt;"; 
         } 
         else { 
            echo "&lt;option value='".$row&#1111;0]."'&gt;".$row&#1111;1]."&lt;/option&gt;"; 
         } 
         $i++;
      }
?&gt;

Posted: Wed May 29, 2002 10:29 am
by Crashin
Still no go...here's a look at the whole thing, including both queries:

Code: Select all

//query to determine which apps were initially assigned to the record
$query_app = "SELECT app_id FROM complete_record WHERE kb_record_id='$recid'";
$result_app = mysql_query($query_app);
$row_app = mysql_fetch_array($result_app);

//query for list of applications
$query = "SELECT * FROM application ORDER BY app_name";
$result = mysql_query($query);
	if(!$result) &#123;
		echo "Cannot perform query.";
		exit;
	&#125;
	if (mysql_num_rows($result) == 0) &#123;
		echo "<td><font class='small'>No applications setup - go to the Applications page to setup applications before setting up categories</font></td>";
		exit;
	&#125;
	else &#123;
		echo "<td>";
			echo "<select name='new_app_id&#1111;]' size='5' multiple>";
			$i=0;
			while($row = mysql_fetch_array($result)) &#123;
				if($row_app&#1111;$i] == $row&#1111;0]) &#123;
					echo "<option value='".$row&#1111;0]."' selected>".$row&#1111;1]."</option>";
				&#125;
				else &#123;
					echo "<option value='".$row&#1111;0]."'>".$row&#1111;1]."</option>";
				&#125;
				$i++;
			&#125;
			echo "</select>";
		echo "</td>";
	&#125;
I know that the queries are working. It's just not cycling through the array for the selected apps properly. By the way, thanks a bunch for your help with this!

Posted: Wed May 29, 2002 11:08 am
by volka
$query_app = "SELECT app_id FROM complete_record WHERE kb_record_id='$recid'";
$result_app = mysql_query($query_app);
$row_app = mysql_fetch_array($result_app);
$row_app contains only one element - 'app_id'.
With every mysql_fetch_array you retrieve one 'line' (row) from the database.
With $row_app[$i] you're not cycling through the rows but through the columns/fields of the current recordset.
Since there is only one column (field), check your error-log to see how many warnings 'undefined index' you probably got.
But try this (I replaced fetch_array by fetch_row since there is no use of the additional data of fetch_array

Code: Select all

&lt;?php
$recid = 1;
//query to determine which apps were initially assigned to the record 
$query = "SELECT app_id FROM complete_record WHERE kb_record_id='$recid'"; 
$result = mysql_query($query);
$row_app&#1111;] = array();
while($currResult = mysql_fetch_row($result))
	$row_app&#1111;] = $currResult; 
mysql_free_result($result);
//query for list of applications 
$query = 'SELECT * FROM application ORDER BY app_name';
$result = mysql_query($query) or die('Cannot perform query.'); 
if (mysql_num_rows($result) == 0)
{ 
	print('&lt;td&gt;&lt;font class="small"&gt;No applications setup - go to the Applications page to setup applications before setting up categories&lt;/font&gt;&lt;/td&gt;');
	exit; 
} 
else
{ 
	print('&lt;td&gt;&lt;select name="new_app_id&#1111;]" size="5" multiple&gt;'); 
  while($row = mysql_fetch_row($result))
  { 
  	if (in_array($row&#1111;0], $row_app)
    	print("&lt;option value="{$row&#1111;0]}" selected&gt;{$row&#1111;1]}&lt;/option&gt;"; 
    else
			print("&lt;option value="{$row&#1111;0]}"&gt;{$row&#1111;1]}&lt;/option&gt;";  
    print("&lt;/select&gt;&lt;/td&gt;"); 
  }
}
mysql_free_result($result);
?&gt;
(server still crashed -> no working php -> not even 'tested by compiler' -> if you find any errors you may keep them ;) )

Posted: Wed May 29, 2002 12:15 pm
by Crashin
Alright...I tried the last step, as shown below:

Code: Select all

//query to determine which apps were initially assigned to the record
$query_app = "SELECT app_id FROM complete_record WHERE kb_record_id='$recid'";
$result_app = mysql_query($query_app);
$row_app&#1111;] = array();
while($currResult = mysql_fetch_row($result_app))
	$row_app&#1111;] = $currResult;
mysql_free_result($result_app);

//query for list of applications
$query = "SELECT * FROM application ORDER BY app_name";
$result = mysql_query($query);
	if(!$result) &#123;
		echo "Cannot perform query.";
		exit;
	&#125;
	if (mysql_num_rows($result) == 0) &#123;
		echo "<td><font class='small'>No applications setup - go to the Applications page to setup applications before setting up records</font></td>";
		exit;
	&#125;
	else &#123;
		echo "<td>";
			echo "<select name='new_app_id&#1111;]' size='5' multiple>";
			while($row = mysql_fetch_row($result)) &#123;
				if (in_array($row&#1111;0], $row_app))
					echo "<option value='".$row&#1111;0]."' selected>".$row&#1111;1]."</option>";
				else
					echo "<option value='".$row&#1111;0]."'>".$row&#1111;1]."</option>";
			&#125;
			echo "</select>";
		echo "</td>";
	&#125;
	mysql_free_result($result);
Unfortuneately, now none of the applications are selected. I'm assuming it has something to do with the comparison between the $row_app array and the $row[0] variable, but I'm not familiar enough with that function to know how to bug it. Any ideas? Thanks! :?

Posted: Wed May 29, 2002 12:44 pm
by volka
jepp, my fault. As mentioned I have no server to test at the moment

Code: Select all

$result = mysql_query($query) or die("failed");
$row_app = array();
while($currResult = mysql_fetch_row($result))
   $row_app&#1111;] = $currResult&#1111;0];

You're the best!

Posted: Wed May 29, 2002 1:00 pm
by Crashin
It works perfectly! Thank you both SO much for your help! I can't tell you how much it's appreciated! :D