Question : Retain selected value from dynamic drop down php

I am trying to upgrade a content management page.
  • By means of introduction, the page I am concerned with allows administrators to view, add, edit and delete news items.

  • Any changes they make and "save" are then held in an "approve changes" page (at this stage their changes are not reflected in the database).

  • The "approve changes" page allows administrators to view, accept or decline their changes...everything defaults to this page before going live, be it an add, edit or delete, there is no time restriction between editing and viewing, items can be help for indefinite periods.


  • So, the news admin page historically enabled administrators to enter a client name in a text box, where the news was client-related.

  • In the database the client name was just stored in the news table along with the rest of that particular item.

  • This seemed an odd approach and in order to improve the site I have now built a client table to hold client names and incremental ids, I have added a column to the news table  "client_id" and manually populated it to bring upto date.

  • I want to amend the admin page so that going forward I can remove the client names from the news table and rely on the "client_id" with a join to the client table.

  • I am part way through incorporating a dynamic drop-down taking client "name" and "id" from the client table (the drop-down displays name but holds the id as the value) and then if a client is selected the id will be saved in the news table as "client_id", or at least thats the plan.

  • I found a code sample that appears to work re. creating and populating the dynamic drop-down but i'm struggling to find a way to retain the selected value between the "news admin" page and the approve "changes page" (i'm an asp.net coder so am a bit lost).

I attach below the full code for both pages, in the hope they clarify anything I haven't, (the "global.php" that is called on both stores all the functions).  
 
This is the News Admin page with my changes so far
 
 
 
This is the Approve Changes page
 

Answer : Retain selected value from dynamic drop down php

Firstly, you have a duplicate </td> on line 146

Secondly, when you look at the dropdown in the source, if all is well you should see

<select name="client_id" size="1">
      <option value="" selected="selected">Select client</option>
      <option value="1">Client name 1</option>
      <option value="2">Client name 2</option>
      <option value="3">Client name 3</option>
</select>

i.e. the values of the select options should be populated with the ids of the corresponding clients

Thirdly, your select query for the news edit allows for a cartesian product. Unless you have a reason I'm not aware of you should change it to:

SELECT news.*, clients.name FROM news
INNER JOIN clients ON news.client_id = clients.id
WHERE news.id=$id

Number 4, you no longer need the $clientoptions code block in lines 52 to 65

And for number 5, the cherry on the icing, change the dropdown to this:

<select name="client_id" size="1">
      <option value="">Select client</option>
<? do { ?>
      <option value="<? echo $rs['id']?>" <?php echo( $rs['id'] == $rec['client_id'] ? " selected=\"selected\"" : "" ); ?>>><? echo( $rs['name'] )?></option>

i.e. remove the selected="selected" from the initial option i.e.
      <option value="">Select client</option>

and the next line will compare the client_id from the news record being edited to the id form the clients dynamic dropdown and if there is a match, will mark that value as selected.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
<?
	require '../lib/global.php';
	$dbh = GetDBConn();

	if (!isset($_SESSION['user']) || !CheckUserPerm($dbh, $_SESSION['user'], $_SERVER['SCRIPT_NAME'])) {
		Redirect('index.php');
	}

	$message = "";
	if (isset($_REQUEST['id'])) {
		$id = $_REQUEST['id'] + 0;
		$content = CleanContent($_REQUEST['content']);
		$updater = $_REQUEST['updater'] + 0;

		if ($updater > 0) {
			$sql = "UPDATE news SET title='" . Prep($_REQUEST['title']) . "', posted='" . Prep(GetInputDate("posted")) . "', content='" . Prep($content) . "', client_id='" . Prep($_REQUEST['client_id']) . "', deal='" . Prep($_REQUEST['deal']) . "', value='" . Prep($_REQUEST['value']) . "', pic1='" . Prep($_REQUEST['pic1']) . "', pic2='" . Prep($_REQUEST['pic2']) . "', pic3='" . Prep($_REQUEST['pic3']) . "' WHERE id=$id";

			mysql_query($sql, $dbh);

			Redirect("approve.php");
		} else {

			if ($id > 0) {
				$sql = "DELETE FROM news WHERE updatefor=$id";
				mysql_query($sql, $dbh);
			}

			$sql = "INSERT INTO news (title, posted, content, client_id, deal, value, pic1, pic2, pic3, updatefor) VALUES ('" . Prep($_REQUEST['title']) . "', '" . Prep(GetInputDate("posted")) . "', '" . Prep($content) . "', '" . Prep($_REQUEST['client_id']) . "', '" . Prep($_REQUEST['deal']) . "', '" . Prep($_REQUEST['value']) . "', '" . Prep($_REQUEST['pic1']) . "', '" . Prep($_REQUEST['pic2']) . "', '" . Prep($_REQUEST['pic3']) . "', $id)";

			mysql_query($sql, $dbh);

			$message = "<p>Your changes have been queued for approval.</p>";
		}
	}

	AdminHeader($dbh, "News administration", null);
	echo $message;

	if (isset($_REQUEST['del'])) {
		$id = $_REQUEST['del'] + 0;

		$sql = "DELETE FROM news WHERE updatefor=$id";
		mysql_query($sql, $dbh);

		$sql = "UPDATE news SET updatefor=-1 WHERE id=$id";
		mysql_query($sql, $dbh);

		echo "<p>Your changes have been queued for approval.</p>";
	}

	if (isset($_REQUEST['edit'])) {
		# Edit/Insert
		$id = $_REQUEST['edit'] + 0;
		$sql = "SELECT * FROM news, clients WHERE news.id=$id" ;

		$res = mysql_query($sql, $dbh);
		$rec = mysql_fetch_array($res);

    	$updater = 0;
    	if ($rec['updatefor'] != null){
      		$updater = 1;
		}
?>

<script type="text/javascript">
	wysSetHeaderContent('<link rel="stylesheet" href="../styles.css" />');
	wysConvertTextarea('content', 1, 'WYSBODY template4');
</script>

<form action="<? echo $_SERVER['SCRIPT_NAME'] ?>" method="post">
	<input type="hidden" name="id" value="<? echo $id ?>" />
	<input type="hidden" name="updater" value="<? echo $updater ?>" />
	<table>
		<tr>
			<td>Title</td>
			<td>
				<input type="text" name="title" value="<? echo str_replace('"', '&quot;', $rec['title']) ?>" size="100" maxlength="200" style="width: 700px;" />
			</td>
		</tr>
		<tr>
			<td>Date</td>
			<td>
<? 
	if ($id > 0) {
		echo MakeInputDate("posted", $rec['posted']);
	} else {
		echo MakeInputDate("posted", date("Y-m-d"));
	}
?>
			</td>
		</tr>
		<tr>
			<td>Details</td>
			<td>
				<textarea name="content" id="content" rows="5" cols="80" style="width: 700px; height: 350px;"><? echo $rec['content'] ?></textarea>
			</td>
		</tr>
		<tr>
			<td>Pictures</td>
			<td>
<?
	echo MakeFileSelector("pic1", array("$CFG_ADMINIMAGES/people", "$CFG_ADMINIMAGES/images", "$CFG_ADMINIMAGES/oldimages"), $rec['pic1'], "showPreview(this.options[this.selectedIndex].value)", '/\.(jpg|jpeg|gif|png)$/i', true);
	echo MakeFileSelector("pic2", array("$CFG_ADMINIMAGES/people", "$CFG_ADMINIMAGES/images", "$CFG_ADMINIMAGES/oldimages"), $rec['pic2'], "showPreview(this.options[this.selectedIndex].value)", '/\.(jpg|jpeg|gif|png)$/i', true);
	echo MakeFileSelector("pic3", array("$CFG_ADMINIMAGES/people", "$CFG_ADMINIMAGES/images", "$CFG_ADMINIMAGES/oldimages"), $rec['pic3'], "showPreview(this.options[this.selectedIndex].value)", '/\.(jpg|jpeg|gif|png)$/i', true);
?>
			</td>
		</tr>
		<tr>
			<td>Client</td>
			<td>
<?
$strSQL	= "SELECT id, name FROM clients ORDER BY name ASC";
$query	= mysql_query($sql, $dbh);
$rs		= mysql_fetch_assoc($query);
?>
				<select name="client_id" size="1">
					<option value="">Select client</option>
<? do { ?>
					<option value="<? echo $rs['id']?>" <?php echo( $rs['id'] == $rec['client_id'] ? " selected=\"selected\"" : "" ); ?>>><? echo( $rs['name'] )?></option>
<?
} while ($rs = mysql_fetch_assoc($query));
	$rows = mysql_num_rows($query);
	if($rows > 0) {
		mysql_data_seek($query, 0);
		$rs = mysql_fetch_assoc($query);
	}
?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Deal</td>
			<td>
				<input type="text" name="deal" value="<? echo str_replace('"', '&quot;', $rec['deal']) ?>" size="100" maxlength="250" style="width: 700px;" /><br /><small>Leave blank if this news doesn't relate to a specific deal</small>
			</td>
		</tr>
		<tr>
			<td>Value</td>
			<td>
				<input type="text" name="value" value="<? echo str_replace('"', '&quot;', $rec['value']) ?>" size="32" maxlength="64" />
			</td>
		</tr>
		<tr>
			<td colspan="2" align="right">
				<input type="submit" value="Save" />
			</td>
		</tr>
	</table>
</form>

<?
	} else {
?>
<table class="full">
	<tr>
		<th>Title</th>
		<th>Date</th>
		<th>Client</th>
		<th>Deal</th>
		<th>Value</th>
		<th></th>
	</tr>
<?
		$sql = "SELECT id, title, posted, client, deal, value FROM news WHERE updatefor IS NULL OR updatefor=-1 ORDER BY posted DESC, id DESC";
		$res = mysql_query($sql, $dbh);
		if (mysql_num_rows($res)) {
			$cnt = 0;
			while ($rec = mysql_fetch_array($res)) {
				echo "<tr class=\"" . ($cnt++ % 2 ? 'even' : 'odd') . "\">";
				echo "<td>" . $rec['title'] . "</td>";
				echo "<td>" . FormatDate($rec['posted']) . "</td>";
				echo "<td>" . $rec['client'] . "</td>";
				echo "<td>" . AbbrevStr($rec['deal'], 50) . "</td>";
				echo "<td>" . $rec['value'] . "</td>";
				$id = $rec['id'];
				echo "<td><a href=\"" . $_SERVER['SCRIPT_NAME'] . "?edit=$id\">Edit</a> ";
				echo "<a href=\"" . $_SERVER['SCRIPT_NAME'] . "?del=$id\" onclick=\"return confirmDel('article');\">Delete</a></td>";
				echo "</tr>";
			}
		} else {
			echo "<tr><td colspan=\"6\">No news found</td></tr>";
		}
?>
</table>
<p>
	<a href="<? echo $_SERVER['SCRIPT_NAME'] ?>?edit=0">Add new news item</a>
</p>
<?
	}

	AdminFooter($dbh);
?>
Random Solutions  
 
programming4us programming4us