PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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:
//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)) {
if($row_appї0] == $rowї0]) {
echo "<option value='".$rowї0]."' selected>".$rowї1]."</option>";
}
else {
echo "<option value='".$rowї0]."'>".$rowї1]."</option>";
}
}
echo "</select>";
echo "</td>";
}
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.
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.
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?
//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;
}
if (mysql_num_rows($result) == 0) {
echo "<td><font class='small'>No applications setup - go to the Applications page to setup applications before setting up categories</font></td>";
exit;
}
else {
echo "<td>";
echo "<select name='new_app_idї]' size='5' multiple>";
$i=0;
while($row = mysql_fetch_array($result)) {
if($row_appї$i] == $rowї0]) {
echo "<option value='".$rowї0]."' selected>".$rowї1]."</option>";
}
else {
echo "<option value='".$rowї0]."'>".$rowї1]."</option>";
}
$i++;
}
echo "</select>";
echo "</td>";
}
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!
$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
<?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ї] = array();
while($currResult = mysql_fetch_row($result))
$row_appї] = $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('<td><font class="small">No applications setup - go to the Applications page to setup applications before setting up categories</font></td>');
exit;
}
else
{
print('<td><select name="new_app_idї]" size="5" multiple>');
while($row = mysql_fetch_row($result))
{
if (in_array($rowї0], $row_app)
print("<option value="{$rowї0]}" selected>{$rowї1]}</option>";
else
print("<option value="{$rowї0]}">{$rowї1]}</option>";
print("</select></td>");
}
}
mysql_free_result($result);
?>
(server still crashed -> no working php -> not even 'tested by compiler' -> if you find any errors you may keep them )
//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ї] = array();
while($currResult = mysql_fetch_row($result_app))
$row_appї] = $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) {
echo "Cannot perform query.";
exit;
}
if (mysql_num_rows($result) == 0) {
echo "<td><font class='small'>No applications setup - go to the Applications page to setup applications before setting up records</font></td>";
exit;
}
else {
echo "<td>";
echo "<select name='new_app_idї]' size='5' multiple>";
while($row = mysql_fetch_row($result)) {
if (in_array($rowї0], $row_app))
echo "<option value='".$rowї0]."' selected>".$rowї1]."</option>";
else
echo "<option value='".$rowї0]."'>".$rowї1]."</option>";
}
echo "</select>";
echo "</td>";
}
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!