Jump to content

CMS <select> option selected


black.horizons

Recommended Posts

I have written a CMS. woohoo all excitement awaits (since its not the hardest thing in the world to do i've discovered)...

Anyways! I've got a page "edit-post.php" and all it has is a title box, a category drop down (which is dynamically filled from a table in my DB) and the post itself.

When I want to edit a post though the only thing is that the category isn't selected by default:

This is what I had and it worked (no category selected):

<?php
    $topic_query="SELECT * FROM newstopics ORDER BY ID";
    /* Error occurred, return given name by default */
    $result=mysql_query($topic_query);
    while ($record = mysql_fetch_assoc($result)) $newstopics[] = $record;
      echo '<SELECT name="topicID">';
        foreach ($newstopics as $nt){
            echo "<OPTION value=\"{$nt[ID]}\" >{$nt['TopicName']}</OPTION>\n";
        }
      echo '</SELECT>';
?>

This is what I thought would work (category selected) but doesn't:

<?php
    $topic_query="SELECT * FROM newstopics ORDER BY ID";
    /* Error occurred, return given name by default */
    $result=mysql_query($topic_query);
    while ($record = mysql_fetch_assoc($result)) $newstopics[] = $record;
      echo '<SELECT name="topicID">';
        foreach ($newstopics as $nt){
          if ($post['TopicID']==({$nt[ID]})){
            echo "<OPTION value=\"{$nt[ID]}\" selected>{$nt['TopicName']}</OPTION>\n";
          }
          else{
            echo "<OPTION value=\"{$nt[ID]}\" >{$nt['TopicName']}</OPTION>\n";
          }
        }
      echo '</SELECT>';
?>
Link to comment
https://forums.phpfreaks.com/topic/26679-cms-option-selected/
Share on other sites

Give this a try:

[code]<?php
$topic_query="SELECT * FROM newstopics ORDER BY ID";
$result=mysql_query($topic_query);

echo '<select name="topicID">';
while ($record = mysql_fetch_array($result))
echo '<option value="'.$record['ID'].'">'.$record['TopicName'].'</option>\n';
echo '</select>';
?>[/code]

Orio.
Link to comment
https://forums.phpfreaks.com/topic/26679-cms-option-selected/#findComment-122041
Share on other sites

sorry my fault - didn't explain myself too well!

the top section of code works find - it selects all the options dynamically from the table in the database called newstopics - and sorts them by (topic)ID.

what i want is the topicID selected from the news article to match up from the newstopics topicID and if the two match then its then selected option.

e.g.

three ID's from newstopics 1 (general) 2 (ladies) 3 (juniors)
from newsarticle the topicID 1 (which links to the newstopic ID field).

output:

<option value="1" selected>General</option>
<option value="2>Ladies</option>
<option value="3">Juniors</option>

just i need the topicID from the newsarticle  value to be the selected value in the list.
Link to comment
https://forums.phpfreaks.com/topic/26679-cms-option-selected/#findComment-122068
Share on other sites

never worry i managed to get it working:

what i had:

<?php
    $topic_query="SELECT * FROM newstopics ORDER BY ID";
    /* Error occurred, return given name by default */
    $result=mysql_query($topic_query);
    while ($record = mysql_fetch_assoc($result)) $newstopics[] = $record;
      echo '<SELECT name="topicID">';
        foreach ($newstopics as $nt){
          if ($post['TopicID']==({$nt[ID]})){
            echo "<OPTION value=\"{$nt[ID]}\" selected>{$nt['TopicName']}</OPTION>\n";
          }
          else{
            echo "<OPTION value=\"{$nt[ID]}\" >{$nt['TopicName']}</OPTION>\n";
          }
        }
      echo '</SELECT>';
?>

what i changed:

?php
    $topic_query="SELECT * FROM newstopics ORDER BY ID";
    /* Error occurred, return given name by default */
    $result=mysql_query($topic_query);
    while ($record = mysql_fetch_assoc($result)) $newstopics[] = $record;
      echo '<SELECT name="topicID">';
        foreach ($newstopics as $nt){
          if ($post['TopicID']==($nt[ID])){
            echo "<OPTION value=\"{$nt[ID]}\" selected>{$nt['TopicName']}</OPTION>\n";
          }
          else{
            echo "<OPTION value=\"{$nt[ID]}\" >{$nt['TopicName']}</OPTION>\n";
          }
        }
      echo '</SELECT>';
?>


basically i removed the { } from around the $nt[ID]:

          if ($post['TopicID']==($nt[ID])){
Link to comment
https://forums.phpfreaks.com/topic/26679-cms-option-selected/#findComment-122072
Share on other sites

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.