Jump to content

select with php


SpireLink

Recommended Posts

Hi,

I have made a script to edit an article in a list, the thing thet the article category (Author Name)  is brought from another table, and to modify the data I get the catigories list from that table and list it all , i want to make the current value selected..

 

<table border='0' width='100%'>
<form name='edit_news' method='post' action='base.php?crl=article_edit.php'>
<tr>
<td align='right'><b>Title:</b>
</td>
<td align='right'><input name='title' type='text' value='<? echo "$title"; ?>'  >
</td>
</tr>

<tr>
<td align='right' valign='top'><b>intro :</b>
</td>
<td align='right'><textarea rows='5' cols='60' name='intro'><? echo $intro ?></textarea>
</td>
</tr>
<tr>
<td align='right' valign='top'><b>Article :</b>
</td>
<td align='right'><textarea rows='30' cols='60' name='article'><? echo $article ?></textarea>
</td>
</tr>


<tr><td align='right'><b>Author :</b></td>
<td align='right'>
<select name='category'>
<?
$artcat = mysql_query("SELECT * FROM artcat ORDER BY id DESC",$db);
while ($artcat_data = mysql_fetch_array($artcat))
{
$artcat_id=$artcat_data["id"];
$artcat_title=$artcat_data["title"];
?>
<option value='<? echo "$artcat_id"; ?>'><? echo "$artcat_title"; ?></option>
<?
}
?>
</select>
</td></tr>

<tr><td align='right'></td><td align='right'><input name='id'  type="hidden"  value='<? echo "$id"; ?>'></td></tr>
<tr><td align='left'><input type='submit' name='submit' value='Update'></td><td align='right'></td></tr>
</form>
</table>

 

Link to comment
Share on other sites

When you select the title, intro and article I assume that you are pulling them from a database table and I assume you have the current category stored there as well.

 

If so, I would try something like this... (assume $category is the current category id pulled from your database)

 

<select name='category'>

<?

$artcat = mysql_query("SELECT * FROM artcat ORDER BY id DESC",$db);

while ($artcat_data = mysql_fetch_array($artcat))

{

  $artcat_id=$artcat_data["id"];

  $artcat_title=$artcat_data["title"];

 

  if($artcat_id = $category)

  {

    echo('<option value="'.$artcat_id.'" selected="selected">'.$artcat_title.'</option>');

  }

  else

  {

    echo('<option value="'.$artcat_id.'">'.$artcat_title.'</option>');

  }

}

?>

</select>

Link to comment
Share on other sites

Ooops... make that

 

<select name='category'>

<?

$artcat = mysql_query("SELECT * FROM artcat ORDER BY id DESC",$db);

while ($artcat_data = mysql_fetch_array($artcat))

{

  $artcat_id=$artcat_data["id"];

  $artcat_title=$artcat_data["title"];

 

  if($artcat_id == $category)

  {

    echo('<option value="'.$artcat_id.'" selected="selected">'.$artcat_title.'</option>');

  }

  else

  {

    echo('<option value="'.$artcat_id.'">'.$artcat_title.'</option>');

  }

}

?>

</select>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.