Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 what the option do is if select 1 enter value 1 into database if select 0 update value on the database to 0 this way the user can update between 0 and 1 the problem is i want the when edit the form to display exacly whatver value is on the database just like the date one Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115685 Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 This should work if the variable is $row['display'] <select name="display"> <option <?php if($row['display'] == '1') { echo 'selected'; } ?> value="1">1</option> <option <?php if($row['display'] == '0') { echo 'selected'; } ?> value="0">0</option> </select> Regards, Paul. Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115686 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 nope it always display the option 1 even if the value on the database is the 0.... it updates.... bt doesnt show when u edit it always display option value 1 Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115688 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 is there a way to do if row 1 display the option 1 else display the option 0 to wrk on the options Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115690 Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Try this... <?PHP if($row['display'] == '1') { $opt1='selected'; $opt2=''; } else { $opt1=''; $opt2='selected'; } ?> <select name="display"> <option <?=$opt1;?> value="1">1</option> <option <?=$opt2;?> value="0">0</option> </select> Regards, Paul. Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115692 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 it doesnt even update like that Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115695 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 i tried like this but no sucess <?php if if($row['display'] == '1') { echo "<option value="1">1</option>"; }else{ echo "<option value="0">0</option>"; } ?> Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115700 Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 You put 2 if's at the start <?php if($row['display'] == '1') { echo "<option value="1">1</option>"; }else{ echo "<option value="0">0</option>"; } ?> I don't understand what you want to happen fully, so I'm stabbing in the dark when giving you replies. Regards, Paul. Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115702 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 ok this what i have thats a simple form that enter the values in the database using the insert works fine but lest say you have a edit form and u want to edit it on the form i want to display the option of the value that is on the database had so if u have stored 0 on the database when u edit the form the firts option that will appear will be will be 0 yes u can change to 1 from the drop down list which when submited will update to 0 and if u edit the form again the first iption that will appear will be 0 basicaly show the option depending on the row display is that any clear??? <select name="display"> <option value="1">1</option> <option value="0">0</option> </select> Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115705 Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 So you want the final outcome to look like this <select name="display"> <option value="1">1</option> <option value="0" selected>0</option> </select> Or... <select name="display"> <option value="0">0</option> </select> Or... <select name="display"> <option value="0">0</option> <option value="1">1</option> </select> Regards, Paul. Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115707 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 <select name="display"> <option value="1">1</option> <option value="0" selected>0</option> </select> i want the selected option to depend on the value already stored on the database i tried like this but sintax error <?php echo '<select name="display">'; if($row['display'] == '1') { echo '<option selected value='1'>1</option>'; }else{ echo "<option selected value="0">0</option>"; } echo '</select>'; ?> Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115710 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 yeah like the one u said here earlier yeah For example if $row['display'] equals 1 you want <select name="display"> <optionvalue ="1" selected>1</option> <option value ="0">0</option> </select> else <select name="display"> <optionvalue ="1">1</option> <option value ="0" selected>0</option> </select> Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115712 Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Well you just pretty much did it yourself Just expand on your example... <?php if($row['display'] == '1') { echo "<select name='display'>\n", "<option value='1' selected>1</option>\n", "<option value='0'>0</option>\n", "</select>\n"; } else { echo "<select name='display'>\n", "<option value='1'>1</option>\n", "<option value='0' selected>0</option>\n", "</select>\n"; } ?> That in theory should work Try it, Paul. Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115714 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 doesnt update the database?? Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115718 Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Of course it won't, you will need to query to update the database, or change an existing query to update the display field. Regards, Paul. Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115721 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 ohh no it does update but it doesnt do the effect like if == 1 selected 1 else selected 2 doesnt work it always keeping 0 as selected no matter what avlue on the database <?php if($row['display'] == '1') { echo "<select name='display'>\n", "<option value='1' selected>1</option>\n", "<option value='0'>0</option>\n", "</select>\n"; } else { echo "<select name='display'>\n", "<option value='1'>1</option>\n", "<option value='0' selected>0</option>\n", "</select>\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115723 Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Just a thought, wouldn't changing the 1 or 2 to use Yes or No be alot simpler to use and understand? It would involve changing the database slightly, its something I have always done, instead of using 1:0 use Yes:No. But it should change, maybes if the is successful reload the page? When data is just posted the data in the page is not updated until next refresh. Regards, Paul. Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115724 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 i had the database has int changed to var chars and value yes and no but still nothing it seems like its only picking up the else statment Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115727 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 i think something wrong with the statment becasue i made simple echo but it walays echo NO so the if statment seem to have no first part of the if statment doesnt weem to be working is the sintax wrong?? <?php if($row['display'] == 'yes') { echo "working"; } else { echo "NO"; } ?> Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115731 Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Is the variable $row['display'] being set? Try using echo $row['display']; See what it outputs Regards, Paul. Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115735 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 nothing happen no display thats strange why??? but the update always worked Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115738 Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Try this also... echo $display; Tell me what you get? Regards, Paul. Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115739 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 still nothing thats weird i even tried to change the name of the column but nothing if i try echo a diffrent row it works bt that particular one doesnt wrk?? i changed the name of the column which it shouldnt show on a different page but it does on a different page Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115740 Share on other sites More sharing options...
PaulRyan Posted September 26, 2010 Share Posted September 26, 2010 Yeah that is strange, and I am stumped for ideas on it tbh. Try the following... echo '<pre>'; print_r($row); echo '</pre>'; Paste results here, I'll look into it. Regards, Paul. Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115743 Share on other sites More sharing options...
Lisa23 Posted September 26, 2010 Author Share Posted September 26, 2010 Array ( [0] => 254 [id] => 254 [1] => novo [name] => novo [2] => machester [location] => machester [3] => 0000-00-00 [date_of_birth] => 0000-00-00 [4] => renault [car_number] => renault [5] => monaco [favourite_track] => monaco [6] => least [least_favourite_track] => least [7] => Achievements [achievements] => Achievements [8] => Sponsors [sponsors] => Sponsors [9] => [email protected]. => [email protected]. [10] => [image] => ) this what the database looks like display varchar(30) latin1_swedish_ci No None this what the whole form looks like <?php define('ROOT_DIR', './'); define('THIS_SCRIPT', 'news'); define('PROPER', TRUE); /** * include common files */ include_once(ROOT_DIR. 'includes/common.inc.php'); ?> <?php // query to get records $id = $_GET['id'] ; // create query to delete record $query = "SELECT id, name, location, date_of_birth, car_number, favourite_track, least_favourite_track, achievements, sponsors, email, image FROM driversnew WHERE id = '$id' "; //Run the query $result = mysql_query($query); //see if any rows were returned if (mysql_num_rows($result) > 0) { // yes - Display Form $row = mysql_fetch_array($result); //Fetch the row //Display the form with original values ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>My xhtml page styled with CSS</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="style/style.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- body { background-color: #FFFFFF; } .style1 {font-size: 12px} --> </style> <h1 align="center"><a href="driverslist.php">Back</a> Admin Center <a href="index.php">Home</a></h1> <p align="center"><font face="Verdana" size="5"><strong>Edit driver form </strong></font></p> <p align="center"><font face="Verdana" size="2"><b><i>This page allow's you to modify drivers details</font></p> <p align="center"> </p> <body marginheight="0" marginwidth="0" leftmargin="0" topmargin="0"> <center> <form action = 'modifydriveraction.php?drivers=<?php echo $row['id']; ?>' method="POST" enctype="multipart/form-data"> <table cellspacing='16' cellpadding='0' border='0' > <tr> <p class="style1">ID field 'readonly' as cannot amend key field</p> <td class="form_field" valign='top' align='right'>ID: </td><td width='10' align='right' valign='top'> </td> <td class="form_text"> <input type="text" readonly="yes" name="id" class='text_box' value=<?php print $row["id"] ?>> </td> </tr> <tr> <td class="form_field" valign='top' align='right'>Name: </td><td width='10' align='right' valign='top'> </td> <td class="form_text"> <input type="text" name="name" class='text_box' value=<?php print $row["name"] ?>> </td> </tr> <tr> <td class="form_field" valign='top' align='right'>Location: </td><td width='10' align='right' valign='top'> </td> <td class="form_text"> <input type="text" name="location" class='text_box' value=<?php print $row["location"] ?>> </td> </tr> <tr> <td class="form_field" valign='top' align='right'>Date of Birth: </td><td width='10' align='right' valign='top'></td> <td class="form_text"> <?php $getDate = $row['date_of_birth']; $splitDate = explode('-',$getDate); $thisMonth = $splitDate[1]; $thisDay = $splitDate[2]; $thisYear = $splitDate[0]; $months = array('','January','February','March','April','May','June','July','August','September','October','November','December'); echo '<select name="month_of_birth">',"\n"; for ($i=1;$i<13;++$i) { if($i == $thisMonth) { $s = ' selected'; } else { $s=''; } echo '<option value="' ,$i, '"',$s,'>' ,$months[$i], '</option>',"\n"; } echo '</select>',"\n"; echo '<select name="day_of_birth">',"\n"; for ($i=1;$i<32;++$i) { if($i == $thisDay) { $s = ' selected'; } else { $s=''; } echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n"; } echo '</select>',"\n"; echo '<select name="year_of_birth">',"\n"; $year = date("Y"); for ($i = $year;$i > $year-50;$i--) { if($i == $thisYear) { $s = ' selected'; } else { $s=''; } echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n"; } echo '</select>',"\n"; ?> </td> </tr> <tr> <td class="form_field" valign='top' align='right'>Car Number: </td><td width='10' align='right' valign='top'> </td> <td class="form_text"> <input type="text" name="car_number" class='text_box' value=<?php print $row["car_number"] ?>> </td> </tr> <tr> <td class="form_field" valign='top' align='right'>Favourite Track: </td><td width='10' align='right' valign='top'></td> <td class="form_text"> <input type="text" name="favourite_track" class='text_box' value=<?php print $row["favourite_track"] ?>> </td> </tr> <tr> <td class="form_field" valign='top' align='right'>Least Favourite Track: </td><td width='10' align='right' valign='top'></td> <td class="form_text"> <input type="text" name="least_favourite_track" class='text_box' value=<?php print $row["least_favourite_track"] ?>> </td> </tr> <tr> <td class="form_field" valign='top' align='right'>Achievements: </td><td width='10' align='right' valign='top'></td> <td class="form_text"> <textarea name="achievements" rows=4 cols=25 ><?php print $row["achievements"] ?> </textarea> </td> </tr> <tr> <td class="form_field" valign='top' align='right'>Sponsors: </td><td width='10' align='right' valign='top'></td> <td class="form_text"> <textarea name="sponsors" rows=4 cols=25 ><?php print $row["sponsors"] ?> </textarea> </td> </tr> <tr> <td class="form_field" valign='top' align='right'>Email Address: </td><td width='10' align='right' valign='top'></td> <td class="form_text"> <input type="text" name="email" class='text_box' value=<?php print $row["email"] ?>> </td> </tr> <tr> <td class="form_field" valign='top' align='right'>Image: </td><td width='10' align='right' valign='top'></td> <td class="form_text"> <input type="file" name="image" value="" class='text_box'> <?php echo '<td><img class="picture" src="images/' . $row['image'] . ' " width="120" height="120 " /></td> '; ?> </td> </tr> <tr> <td class="form_field" valign='top' align='right'></td><td width='10' align='right' valign='top'></td> <td class="form_text"> <?php echo '<pre>'; print_r($row); echo '</pre>'; ?> </td> </tr> <tr><td colspan=3 align='center'><input type="submit" value="Submit" name="Submit" ></td></tr> </table> </form> <?php } //End if rows returned //No rows returned else print "No records were found"; ?> </center> </body> </html> Link to comment https://forums.phpfreaks.com/topic/214380-how-can-i-keep-values-of-this-type-drop-down-list-has-it-has-3-selects-for-date/page/2/#findComment-1115744 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.