Jump to content

[SOLVED] List menu selection


timmah1

Recommended Posts

In the database, the mainID is listed as so

3, 9, 2

 

I have a list menu, so when you edit the details of this page, I need to be able to keep the mainID fileds that are in the database to show up selected.

 

This is how I have it

<?php
$query = "SELECT * FROM sublinks WHERE id = '$id'";
	$portfolio = mysql_query($query);
	$numrows = mysql_num_rows($portfolio);
	while ($a = mysql_fetch_array($portfolio))	{ 

		$id1 = $a['id'];
		$name1 = $a['name'];
		$mainID = explode(', ', $a['mainID']);
$q = mysql_query("SELECT * FROM links ORDER BY name ASC");                    
                        while ($b = mysql_fetch_array($q)) {  

						$id2 = $b['id'];
						$name2 = $b['name'];

						foreach($mainID as $key){  

							if ($key == $id2) {	                   
							echo '<option value="'.$id2.'" selected>'.$name2.'</option>';
							}	
							else {
							echo '<option value="'.$id2.'">'.$name2.'</option>';			
							}
						}
				}
                    ?> 

 

It shows the three as selections, but it's showing each listing 3 times.

 

How can I get this to only show once instead of 3 times?

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/143768-solved-list-menu-selection/
Share on other sites

I simplified the code

<?php
$mainID = explode(', ', $a['mainID']);

foreach($mainID as $key){  
$selected = ($key == $id2) ? 'selected' : ''; 
echo '<option value="'.$id2.'" '.$selected.'>'.$name2.'</option>';
}
?>

 

It works fine if there is only 1 item listed for mainID, but if there is more than one, it shows the options more than once, for however many

is in mainID.

 

How can I fix this?

 

Thanks

Can anybody help me out?

 

Doing this only shows the last one

$mainID = explode(', ', $a['mainID']);
foreach($mainID as $key){    
$selected = ($key == $id2) ? 'selected' : ''; 
}
echo '<option value="'.$id2.'" '.$selected.'>'.$name2.'</option>';

 

but doing this

$mainID = explode(', ', $a['mainID']);
foreach($mainID as $key){    
$selected = ($key == $id2) ? 'selected' : ''; 
echo '<option value="'.$id2.'" '.$selected.'>'.$name2.'</option>';
}

 

Shows everything 3 times because there are 3 numbers in the database

Maybe this will help.

Here is the entire code.

 

Can someone PLEASE help me out here?

<?php
case "links":
	$query = "SELECT * FROM sublinks WHERE id = '$id'";
	$portfolio = mysql_query($query);
	$numrows = mysql_num_rows($portfolio);
	while ($a = mysql_fetch_array($portfolio))	{ 

		$id1 = $a['id'];
		$name1 = $a['name'];


	?>
                <form action="view_sublinks.php" method="post" enctype="multipart/form-data" name="admin" id="admin">
                <table width="100%" border="0" cellspacing="0" cellpadding="5" class="table_main">
                <tr>
                    <td align="left" valign="top" class="formText" colspan="2"><strong>Edit Subcategory</strong></td>
                  </tr>
                  <tr>
                    <td align="left" valign="top" class="formText"><strong>Name:</strong></td>
                    <td align="left" valign="top"><input type="text" name="name" id="text" class="text" value="<?=$name1;?>" /></td>
                  </tr>
                    <tr>
                    <td align="left" valign="top" class="formText"><strong> Main Category:</strong></td>
                    <td align="left" valign="top">
                    (Hold ctrl button to select more than one)<br />
                    <select name="mainID[]" size="10" multiple="multiple" id="mainID">
                    <?php

					$q = mysql_query("SELECT * FROM links ORDER BY name ASC"); 
					                 
						while ($b = mysql_fetch_array($q)) {  

							$id2 = $b['id'];
							$name2 = $b['name'];

							$mainID = explode(', ', $a['mainID']);
							foreach($mainID as $key){   
							$selected = ($key == $id2) ? 'selected' : '';
							echo '<option value="'.$id2.'" '.$selected.'>'.$name2.'</option>';
							}

						}
                    ?>                      
                    </select>                 </td>
                    </tr>
                  <tr>
                    <td align="left" valign="top"> </td>
                    <td align="left" valign="top">
                    <input type="hidden" name="id" value="<?=$id1;?>" />
                    <input type="submit" name="editLink" value="Edit Sub Link" id="buttons" class="buttons" /></td>
                  </tr>
                </table>
                </form>
    <?php
}
    break;
?>

I'm not really getting what you're trying to do, but I'll give it a stab:

 

$mainID = explode(', ', $a['mainID']); // main ID from earlier

// now, let's see if id2 is in the array, if so select the option, if not ignore
if(in_array($id2, $mainID))    
$selected = 'selected';
else 
$selected = '';
// show the option
echo '<option value="'.$id2.'" '.$selected.'>'.$name2.'</option>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.