Jump to content


Very Simple Select Help (ASAP UNI WORK)


4 replies to this topic

#1 richie17

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 07 January 2009 - 08:23 PM

Right i been learning php and mysql for about 3 week and have a problem i keep have a few as it goes but its for uni work and i cant seem to fix this one problem on my update page.

basically it doesn't preselect the current status to show up in the box 1st why? I feel i doing this right whats the problem

thanks to anyone that helps

$eventid = $_GET['id'];
	
$result = "SELECT * FROM events
		LEFT JOIN artists ON events.artist_id=artists.artist_id
		LEFT JOIN venues ON events.venue_id=venues.venue_id
		WHERE event_id = $eventid";

$result = mysql_query ( $result );
	
		  $row = mysql_fetch_assoc ( $result );

			  $current_status = $row['status'];

// created a drop down box for status and pre select current status
	
$status .=	'<select name="status">';
$status .=	'<option value="Available" '.(($row['status']==$current_status)?'selected="selected"':'').'>Available</option>';
$status .=	'<option value="Sold Out" '.(($row['status']==$current_status)?'selected="selected"':'').'>Sold Out</option>';
$status .=	'<option value="Cancelled" '.(($row['status']==$current_status)?'selected="selected"':'').'>Cancelled</option>';
$status .=	'</select>';
	
echo "$status";


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 07 January 2009 - 11:07 PM

// Since:
$current_status = $row['status'];

($row['status']==$current_status) // Is always true
If you look at your html source, you should see that everything in the select box is being set with 'selected="selected"'.

What you need to do is either make a loop that loops through the 3 possible status's (the method i would probably do, but don't want to explain), or remove the $current_status variable (which equals $row['status']), and replace all instances of it with a corrisponding string (i.e. 'Sold out').

#3 richie17

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 07 January 2009 - 11:30 PM

View Postrc69, on Jan 8 2009, 04:07 AM, said:

// Since:
$current_status = $row['status'];

($row['status']==$current_status) // Is always true
If you look at your html source, you should see that everything in the select box is being set with 'selected="selected"'.

What you need to do is either make a loop that loops through the 3 possible status's (the method i would probably do, but don't want to explain), or remove the $current_status variable (which equals $row['status']), and replace all instances of it with a corrisponding string (i.e. 'Sold out').

Hey I think I understand what your saying but I don't have any idea how I would be able to write it could someone help me to write this loop please, it would help me alot...? thanks

#4 023-jimmy

    Young Padawan

  • Members
  • Pip
  • 44 posts

Posted 08 January 2009 - 11:14 AM

I've got the code for you, I don't have time to explain it all right now, because I have to go to work. But when you want something explained, just ask and I'll anser asap.

 $status . = "<select name=\"status\">";
	$num_items = 3; // Amount of options that will be in the dropdown box
	for($i = 1; $i <= $num_items; $i++){
		
		  /* List the options. You can add more options here, just keep counting up from 3, don't forget to change the variable $num_items to the amount of options you have. */
		  $option_1 = "Available";
		  $option_2 = "Sold Out";
		  $option_3 = "Cancelled";
		  
		  if($current_status == ${"option_" . $i}){  
			  $selected = "selected=\"selected\"";
		  } else {
			  $selected = "";
		  }
			  
		  $status .=	"<option value=\"".  ${"option_" . $i} ."\" ". $selected .">".  ${"option_" . $i} ."</option>";
	}
	
	$status .= "</select>";

I've also created a version that works with a array, just choose the one that you think is best for you. I prefer the version with the array, you can just add new options to the array and the rest will change automatic.

$status .= "<select name=\"status\">";
 
 $options = array ("Available", "Sold&nbsp;out", "Cancelled");
 
 for($i = 0; $i < count($options); $i++){
 
	   if($current_status == $options[$i]){
		   $selected = "selected=\"selected\"";
	   } else {
		   $selected = "";
	   }
		   
	   $status . = "<option value=\"".  $options[$i] ."\" ". $selected .">".  $options[$i] ."</option>";
 }
 
 $status .= "</select>";

Edited by 023-jimmy, 08 January 2009 - 05:34 PM.


#5 richie17

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 08 January 2009 - 06:48 PM

View Post023-jimmy, on Jan 8 2009, 04:14 PM, said:

I've got the code for you, I don't have time to explain it all right now, because I have to go to work. But when you want something explained, just ask and I'll anser asap.

 $status . = "<select name=\"status\">";
	$num_items = 3; // Amount of options that will be in the dropdown box
	for($i = 1; $i <= $num_items; $i++){
		
		  /* List the options. You can add more options here, just keep counting up from 3, don't forget to change the variable $num_items to the amount of options you have. */
		  $option_1 = "Available";
		  $option_2 = "Sold Out";
		  $option_3 = "Cancelled";
		  
		  if($current_status == ${"option_" . $i}){  
			  $selected = "selected=\"selected\"";
		  } else {
			  $selected = "";
		  }
			  
		  $status .=	"<option value=\"".  ${"option_" . $i} ."\" ". $selected .">".  ${"option_" . $i} ."</option>";
	}
	
	$status .= "</select>";

I've also created a version that works with a array, just choose the one that you think is best for you. I prefer the version with the array, you can just add new options to the array and the rest will change automatic.

$status .= "<select name=\"status\">";
 
 $options = array ("Available", "Sold&nbsp;out", "Cancelled");
 
 for($i = 0; $i < count($options); $i++){
 
	   if($current_status == $options[$i]){
		   $selected = "selected=\"selected\"";
	   } else {
		   $selected = "";
	   }
		   
	   $status . = "<option value=\"".  $options[$i] ."\" ". $selected .">".  $options[$i] ."</option>";
 }
 
 $status .= "</select>";

THANK YOU SO MUCH DONE ! ;)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users