Jump to content

how to display radio buttons as selected according to value in db column


peppericious

Recommended Posts

I have a little blog which allows users reply to posts. Replies go into a 'replies' table, awaiting moderation. The table has a moderation_level column. Values in this column are 1, 2 or 3, denoting 'Queued', 'Accepted', 'Rejected', respectively.

 

moderation_queue.php will display recent replies and allow me to alter the moderation_level value by clicking a radio button.

 

Each reply on this page will have three radio buttons - 'Queued', 'Accepted' and 'Rejected' - one of which should be selected according to the current value of the moderation_level column in the db. (By default, all replies are initially assigned a value of '1'.)

 

However I can't figure out how to have the appropriate radio button for each reply selected, or not, according to this value.

 

Any help greated appreciated. Thanks in advance.

 

Here's my code on moderation_queue.php:

 

function get_replies_to_moderate() {
global $dbc;
$q = "SELECT 
	id,
	f_blogpost_id,
	body,
	f_responder,
	DATE_FORMAT(created, '%Y') AS year,
	DATE_FORMAT(created, '%b') AS month,
	DATE_FORMAT(created, '%d') AS day,
	DATE_FORMAT(created, '%H:%i') AS time,
	moderation_level
FROM blog_replies
WHERE moderation_level = 1
ORDER BY id DESC";
$r = mysqli_query($dbc, $q);
      if (mysqli_num_rows($r) > 0) {	
		  while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
			  ?>
  			    <tr>
  			      <td><?php echo $row['id']; ?></td>
  			      <td><?php echo $row['f_blogpost_id']; ?></td>
				  <td><?php echo $row['body']; ?></td>
  			      <td><?php echo $row['f_responder']; ?></td>
  			      <td><?php echo $row['time'] . ' ' . $row['day'] . ' ' . $row['month'] . ' ' . $row['year']; ?></td>
  			      <td>
  			        <label>
  			          <input name="moderation_level" type="radio" value="queued" <?php // not sure what to put here... ?> />
  			          Queued</label>
  			           
  			        <label>
					<label>
  			          <input name="moderation_level" type="radio" value="accept" <?php // not sure what to put here... ?> />
  			          Accepted</label>
  			           
  			        <label>
  			          <input name="moderation_level" type="radio" value="reject" <?php // not sure what to put here... ?> />
  			          Rejected</label>
  			        </td>
  			    </tr>
		  <?php
		  } // close WHILE
	  } // close if-posts-exist IF
}

Link to comment
Share on other sites

I like to answer such questions - when TS does something and needs to clarify just a small part :)

 

OK.

 

Change this

<?php // not sure what to put here... ?>

 

to

 

<?php if( any_condition ) echo 'selected="selected"'; ?>

 

any_condition depends on information from DB (if you like to set the default value for radio - it seems it's your case) or from the $_POST or $_GET array (if this script depends on previous one).

And of course just one condition must be true, for one radio from the set.

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.