Jump to content

get data from database


cinek

Recommended Posts

I need to get some data from the database and then asign it to the form, now I've got something like this to retrieve the data from mysql but it's only for 1 select list. I could use this but I can't see how I'd be able to assign this data to the form. Any ideas?

<?php																				mysql_connect('localhost', 'root', 'pass');																				mysql_select_db('ipsum');																				$sql = "SELECT Name FROM con";																				$result = mysql_query($sql);																		while($rows = mysql_fetch_array($result)){											$name = $rows[0];											echo "<option name=\"$name\">$name</option>";										}									?>

P.S the form has 6 select lists & 2 textboxes

Link to comment
Share on other sites

Add more fields to your SELECT statement.Instead of immediately echoing your option elements, append the new values for each field to a string. After the strings have been assembled, echo each string in the correct part of your form.

Link to comment
Share on other sites

thanks, I have changed the code to get all the needed values. How can I append the new values to a string?I'm not sure echoing the strings in the form will work as the form already has values...here's what I use to get the data now:

<?php	$Name = $_REQUEST['configName'];			mysql_connect('localhost', 'root', 'password') or die (mysql_error());		mysql_select_db('ipsum') or die (mysql_error());		$query = "SELECT * FROM con WHERE Name = 'config1'";		$sql = mysql_query($query) or die (mysql_error());		while($row = mysql_fetch_array($sql)){		echo "id ". $row['id'] . "<br />";		echo "Sections ". $row['Sections'] . "<br />";		echo "Fontcolor " . $row['Fontcolor'] . "<br />";		echo "Background color " . $row['BackgroundColor'] . "<br />";		echo "Font " . $row['Font'] . "<br />";		echo "Font size " .	$row['FontSize']  . "<br />";		echo "Line height " . $row['LineHeight']  . "<br />";		echo "Letter spacing " . $row['LetterSpacing']  . "<br />";		echo "FontStyle " .	$row['FontStyle']  . "<br />";		echo "Name " . $row['Name']  . "<br />";	}?>

instead of echoing you mean do something like this?

$name1. $row['Name']

Link to comment
Share on other sites

I don't know for sure, because I don't know what data is supposed to go in what parts of your form. So I'll invent something to give you an idea (watch out for errors--this is off the top of my head):

$name_str = "";$id_str = "";$city_str = "";while($row = mysql_fetch_array($sql)){   $name_str .= '<option value="' . $row['name'] . '">' . $row['name'] . '</option>';   $id_str .= '<option value="' . $row['id'] . '">' . $row['id'] . '</option>';   $city_str .= '<option value="' . $row['city'] . '">' . $row['city'] . '</option>';}echo '<select id="names">' . $name_str . '</select>';echo '<select id="ids">' . $id_str . '</select>';echo '<select id="cities">' . $city_str . '</select>';

This should output 3 select elements. The options for each select element all come from one field of the table. I think that's what you want?

Link to comment
Share on other sites

this is the code for the whole form... for the select lists, is it possible to compare the retrieved value to the values in the list and select one?

<!-- Form id div -->				<div id="form">									<form method="post" action="" id="mainForm">						<label for="sections" id="wybierz">Wybierz sekcje</label>						<select id="sections" name="sections">							<option value="sekcjaA">Sekcja A</option>							<option value="sekcjaB">Sekcja B</option>						</select>												<!-- Chose font & background color -->						<label for="fontcolor" id="colorsLabel">Wybierz kolor czcionki/ tla </label>						<input class="color" id="fontColor" name="fontColor" value="F0000F" /> <input class="color" id="bgcolor" name="bgcolor" value="ebebeb" />																		<!-- Chose font -->						<label for="font" id="choseFontLabel">Wybierz typ/ rozmiar czcionki<abbr title="Wybierz typ oraz rozmiar czcionki">(?)</abbr></label>							<select id="font" name="font">							<option value="Arial">Arial</option>							<option value="Comic Sans Ms">Comic Sans MS</option>							<option value="Courier New">Courier New</option>							<option value="Georgia">Georgia</option>							<option value="Impact">Impact</option>								<option value="Tahoma">Tahoma</option>							<option value="Times New Roman">Times New Roman</option>							<option value="Verdana">Verdana</option>							<option value="Geneva">Geneva</option>							<option value="Trebuchet MS">Trebuchet MS</option>						</select>							<!-- End chose font -->												<!-- Chose font size -->						<select id="fontSize" name="fontSize">							<option value="9">9px</option>							<option value="10">10px</option>							<option value="11">11px</option>							<option value="12">12px</option>								<option value="13">13px</option>							<option value="14">14px</option>							<option value="15">15px</option>							<option value="16">16px</option>							<option value="17">17px</option>							<option value="18">18px</option>								<option value="19">19px</option>							<option value="20">20px</option>						</select>						<!-- End chose font size -->												<!-- Chose line height -->						<label for="lineHeight" id="lineHeightLabel">Wybierz wysokosc interlini/ odstep miedzy znakami</label>						<select id="lineHeight" name="lineHeight">								<option value="8">8px</option>							<option value="10">10px</option>							<option value="11">11px</option>							<option value="12">12px</option>							<option value="13">13px</option>							<option value="14">14px</option>								<option value="15">15px</option>							<option value="16">16px</option>							<option value="17">17px</option>							<option value="18">18px</option>							<option value="19">19px</option>							<option value="20">20px</option>								<option value="21">21px</option>							<option value="22">22px</option>							<option value="23">23px</option>							<option value="24">24px</option>							<option value="25">25px</option>							<option value="26">26px</option>								<option value="27">27px</option>							<option value="28">28px</option>							<option value="29">29px</option>							<option value="30">30px</option>						</select>						<!-- End chose line height -->												<!-- Chose letter spacing -->							<select id="letterSpacing" name="letterSpacing">							<option value="1">1px</option>							<option value="2">2px</option>							<option value="3">3px</option>							<option value="4">4px</option>							<option value="5">5px</option>								<option value="6">6px</option>							<option value="7">7px</option>							<option value="8">8px</option>							<option value="9">9px</option>							<option value="10">10px</option>						</select>							<!-- End chose letter spacing -->													<select id="fontStyle" name="fontStyle">							<option value="normal">Normal</option>							<option value="italic">Italic</option>							<option value="bold">Bold</option>												</select>

Link to comment
Share on other sites

I am happy you brought up this topic because i'm actually interested something like this, My issue is i need to retrieve data from the database on my website as a SEARCH BAR or a DROP DOWN MENU.(This will be a DOG website where plp can go and read about different breeds of dog). Where the visitor of the site can type a name and click search and it will generate the name of that particular breed or an error message if it cannot be found. Not sure if i'making sense...but would i need to do something like this so my search bar or drop down menu work with my doggydatabase that i have created aready.....any suggestion? thanks

Link to comment
Share on other sites

I am happy you brought up this topic because i'm actually interested something like this, My issue is i need to retrieve data from the database on my website as a SEARCH BAR or a DROP DOWN MENU.(This will be a DOG website where plp can go and read about different breeds of dog). Where the visitor of the site can type a name and click search and it will generate the name of that particular breed or an error message if it cannot be found. Not sure if i'making sense...but would i need to do something like this so my search bar or drop down menu work with my doggydatabase that i have created aready.....any suggestion? thanks
it does make sense, I think you're looking at something a bit more simple.Basically you can get the field (search box) in php
$search = $_REQUEST['searchBox'];

and then do a query to the db something like

$query = "SELECT * FROM petDB WHERE PetName = '$search'";

and you can echo this anywhere on the site

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...