Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Inital Value in list taken from database

    Hi all. I have a form in which i want to the initial value of a select box set so when the page loads it is already selected. The values in the list box are taken from a databse though and arent listed. Here's the code:

    Code:
    <form name=\"GetTableForm\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
    		echo "<tr><td align=\"center\" valign=\"top\" rowspan=\"3\">";
    		echo "<strong>Select Table</strong><br><select name=\"GetTableName\" id=\"GetTableName\" size=\"".$getTableCount."\" onClick=\"GetTableListSubmit.focus(0);\">";
    		while($tablerow = mysql_fetch_array($getTableQuery))
    		{	if ((isset($_POST["GetTableName"]))&&($_POST["GetTableName"]==$tablerow[0]))
    					{	echo "<option value=\"".$tablerow[0]."\" selected>".$tablerow[0]."</option>";	}
    			else	{	echo "<option value=\"".$tablerow[0]."\">".$tablerow[0]."</option>";	}
    		}
    		echo "</select></td>";
    		echo "<td rowspan=\"3\">&nbsp;</td>";
    		echo "<td align=\"left\" valign=\"top\" colspan=\"2\">";
    		if (isset($_POST["GetTableListFieldCount"]))
    				{	echo "<strong>Number of fields</strong><br>(columns) to display<br><input type=\"text\" name=\"GetTableListFieldCount\" id=\"GetTableListFieldCount\" size=\"5\" value=\"".$_POST["GetTableListFieldCount"]."\" onblur=\"return isNum(this);\">";	}
    		else	{	echo "<strong>Number of fields</strong><br>(columns) to display<br><input type=\"text\" name=\"GetTableListFieldCount\" id=\"GetTableListFieldCount\" size=\"5\" value=\"4\" onblur=\"return isNum(this);\">";	}
    		echo "</td></tr>";
    		echo "<tr><td align=\"left\" valign=\"top\">";
    		if (isset($_POST["GetTableListRecordS"]) && $_POST["GetTableListRecordS"]!="")
    				{	echo "<strong>Starting Row Number</strong><br>(empty, 0, 1: beginning)<br><input type=\"text\" name=\"GetTableListRecordS\" id=\"GetTableListRecordS\" size=\"5\" value=\"".$_POST["GetTableListRecordS"]."\" onblur=\"return isNum(this);\">";	}
    		else	{	echo "<strong>Starting Row Number</strong><br>(empty, 0, 1: beginning)<br><input type=\"text\" name=\"GetTableListRecordS\" id=\"GetTableListRecordS\" size=\"5\" value=\"1\" onblur=\"return isNum(this);\">";	}
    		echo "</td><td align=\"left\" valign=\"top\">";
    		if (isset($_POST["GetTableListRecordT"]) && $_POST["GetTableListRecordT"]!="")
    				{	echo "<strong>Number of rows</strong><br>to display (empty or 0: all remaining)<br><input type=\"text\" name=\"GetTableListRecordT\" id=\"GetTableListRecordT\" size=\"5\" value=\"".$_POST["GetTableListRecordT"]."\" onblur=\"return isNum(this);\">";	}
    		else	{	echo "<strong>Number of rows</strong><br>to display (empty or 0: all remaining)<br><input type=\"text\" name=\"GetTableListRecordT\" id=\"GetTableListRecordT\" size=\"5\" value=\"0\" onblur=\"return isNum(this);\">";	}
    		echo "</td>";
    $GetTableListFieldCountText = <<<GetTableListFieldCountText
    			<small class="x">
    			If records exist in the table, the resulting table will display either this number of fields, or all the fields in the table, whichever is less.<br><br>
    			Number is to be greater than 0 and less than or equal to 99. Anything else, such as 0, will yield all fields in table no matter how many fields there are.
    			</small>
    GetTableListFieldCountText;
    		echo "</tr><tr><td colspan=\"3\" align=\"left\" valign=\"top\">".$GetTableListFieldCountText;
    		echo "<br><br><input type=\"submit\" name=\"GetTableListSubmit\" value=\"Show Records\">";
    		echo "</form>";
    anyone got any ideas?

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,798
    Thanks
    19
    Thanked 156 Times in 147 Posts
    Suggestion: There's no need to echo out huge blocks of HTML code. Separate your HTML and PHP code better. Then you won;t have to escape every other quote mark in your code with a '\'.

    There are other things you could do to optimize your code, but lets start with that...

    Then put your code into PHP tags for this post.

    Those two things alone will make your code more readable for the rest of us.

    finally, you claim that you are popluating your select menu by using a database. Check to make sure your db connection is solid, and echo out and check your queries for accuracy.

    Also, I noticed you are displaying the values for each db row array like this:

    PHP Code:
    $tablerow[0
    Do you really have a column in your database named "0" ??
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •