mishuk Posted February 16, 2007 Share Posted February 16, 2007 Hi I am having trouble with this code. The script that creates a drop down box populated with values from a table works fine on it own however when I have put it into the quotes and wanting to format it in a table it doesn't work. could I have some help with how to quote the query so that it will work. Thanks $message .= ' <TABLE BORDER="0" cellspacing="5"> <TR> <TD>Ethnicity:</TD> <TD> echo <select name="ethnicity">; $res=mysql_query("select * from tbl_ethnicity"); if(mysql_num_rows($res)==0) echo "there is no data in table.."; else echo"<option>Select:</option>"; for($i=0;$i<mysql_num_rows($res);$i++) { $row=mysql_fetch_assoc($res); echo"<option>$row[ethnicity]</option>"; } echo </select>; </TD> </TR> </TABLE> </FORM>'; Quote Link to comment Share on other sites More sharing options...
richardw Posted February 16, 2007 Share Posted February 16, 2007 for starters, fix this line: echo"<option>",$row[ethnicity]."</option>"; Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 16, 2007 Share Posted February 16, 2007 i took one look at that code a shuddered. thats quite messy, no offence. i'd look into cleaning it up a bit first. that might actually solve all your problems. Quote Link to comment Share on other sites More sharing options...
mishuk Posted February 16, 2007 Author Share Posted February 16, 2007 Thanks for your advice. As you can probably guess my experience with php has been very little. So if i was to take it in its simplest context with the code below. how would you suggest I tidy it up? Thanks <?php include("includes/db_connect.php"); echo '<select name="ethnicity">'; $res=mysql_query("select * from tbl_ethnicity"); if(mysql_num_rows($res)==0) echo "there is no data in table.."; else echo '<option>Select:</option>'; for($i=0;$i<mysql_num_rows($res);$i++) { $row=mysql_fetch_assoc($res); echo "<option>$row[ethnicity]</option>"; } echo '</select>'; ?> Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted February 16, 2007 Share Posted February 16, 2007 <?php include("includes/db_connect.php"); $res=mysql_query("select * from tbl_ethnicity"); if(mysql_num_rows($res)==0) { echo "there is no data in table.."; } else { echo '<select name="ethnicity">'; echo '<option>Select:</option>'; while($row=mysql_fetch_assoc($res)) { echo '<option value="' . $row['ethnicity_id'] . '">' . $row['ethnicity'] . '</option>'; } echo '</select>'; } ?> That is how I would do it. Well I would break out of php for some of the html but otherwise thats how I'd do it. Quote Link to comment Share on other sites More sharing options...
mishuk Posted February 16, 2007 Author Share Posted February 16, 2007 Ah right. Yeh that does seem better. however i am still left with my origional problem in that i am what i need to be able to put this code into a variable and call this variable into the html <?php $message = ' script '; ?> <HTML> <HEAD> </HEAD> <BODY> <? print $message; ?> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 16, 2007 Share Posted February 16, 2007 So what's the problem? Quote Link to comment Share on other sites More sharing options...
mishuk Posted February 16, 2007 Author Share Posted February 16, 2007 Whatever i try as far as getting the quotes correct so that it works. none of it does ? <?php include("includes/db_connect.php"); $message = '$res=mysql_query("select * from tbl_ethnicity"); if(mysql_num_rows($res)==0) { echo "there is no data in table.."; } else { echo '<select name="ethnicity">'; echo '<option>Select:</option>'; while($row=mysql_fetch_assoc($res)) { echo '<option value="' . $row['ethnicity_id'] . '">' . $row['ethnicity'] . '</option>'; } echo '</select>'; }'; ?> <HTML> <HEAD> </HEAD> <BODY> <? print $message; ?> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 16, 2007 Share Posted February 16, 2007 You're trying to put code in a string. Why don't you just put the code there? After the body, put your code, instead of trying to store it in a string and print it. Quote Link to comment Share on other sites More sharing options...
mishuk Posted February 16, 2007 Author Share Posted February 16, 2007 I am trying to have an input form that uses self submision so that i dont need to seperate my code from my html etc. I want it to work so that it will only post the form when certain conditions are true else post something else. Does that make sence ? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 16, 2007 Share Posted February 16, 2007 Yes, but you're still trying to store code in a string, instead of just writing the code. Stop doing that, and just write the code with if(){}else{} Quote Link to comment Share on other sites More sharing options...
mishuk Posted February 16, 2007 Author Share Posted February 16, 2007 ah right. il give it a go. Cheers Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted February 16, 2007 Share Posted February 16, 2007 Try that, ish. <?php include("includes/db_connect.php"); $res=mysql_query("select * from tbl_ethnicity"); if (mysql_num_rows($res)==0) { echo "there is no data in table.."; } else {?> <select name="ethnicity"> <option>Select:</option> <?php while($row=mysql_fetch_assoc($res)) {?> <option value="<?php echo $row['ethnicity_id']; ?>"> <?php echo $row['ethnicity']; ?> </option> <?php } ?> </select> <?php } ?> <HTML> <HEAD> </HEAD> <BODY> <? print $message; ?> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted February 16, 2007 Share Posted February 16, 2007 Jesus H.... If you want something doing properly..... It is easier to perform this task as jessie suggest and just have it in a code block that sits inside the html, BUT if you want to put that bit of un-necessary extra load on the server memeory then do this... include("includes/db_connect.php"); $res=mysql_query("select * from tbl_ethnicity"); if(mysql_num_rows($res)==0) { $mssg = "there is no data in table.."; } else { $mssg = '<select name="ethnicity">'; $mssg .= "\r\n\t" . '<option>Select:</option>'; while($row=mysql_fetch_assoc($res)) { $mssg .= "\r\n\t" . '<option value="' . $row['ethnicity_id'] . '">' . $row['ethnicity'] . '</option>'; } echo "\r\n" . '</select>'; } ?> <HTML> <HEAD> </HEAD> <BODY> <?php echo $message; ?> </BODY> </HTML> Now don't you worry about all those \r\n and \t's - they will just make it easier to read your html. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.