calling data into a form?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mull300
Forum Newbie
Posts: 3
Joined: Thu Nov 03, 2005 11:07 pm

calling data into a form?

Post by mull300 »

I was wondering i have this shopping cart that i am working on but i am having trouble with a form that i need to create. there is a .php file that calls a .inc.php file and a .tpl file and in the .inc.php file i have a function for an sql database. what im trying to do is query info from the sql database and place it in an option tag in a form. would you know how to do this or can it not be done? :?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

of course it can be done

if your database info is in the file just do a normal query for whatever you want

Code: Select all

$array = mysql_fetch_assoc(mysql_query("SELECT field FROM table"));
$field = $array['field'];

// putting it as an option

echo "<select name=\"name\"><option>$field</option></select>";
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
mull300
Forum Newbie
Posts: 3
Joined: Thu Nov 03, 2005 11:07 pm

Post by mull300 »

yea but it is two seperate files i can get the information to show up but i cant get it to position where i want it to
Sanoz0r
Forum Newbie
Posts: 3
Joined: Thu Nov 03, 2005 10:20 am
Location: Cape Town, South Africa

Post by Sanoz0r »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


If you including those other files, it essentially become one file.  You can call the function in your .inc.php anywhere in ur .php file.

In your INC.PHP file:

Code: Select all

function getName($clientId) {
  $row=mysql_fetch_array(mysql_query("select name from tbl where id=".$clientId);
  return $row[0];
}
In you .PHP file

Code: Select all

<option><?=getName(3)?></option>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
mull300
Forum Newbie
Posts: 3
Joined: Thu Nov 03, 2005 11:07 pm

Post by mull300 »

here is the .inc.php file i can get the form to show up but it will only show up on the top left of the screen which isnt good. do i even need the form here or just the statements? and how do i call it in a form on a .tpl page

Code: Select all

$products=new XTemplate ("skins/".$config['skinDir']."/styleTemplates/global/products.tpl");

$latestProducts = $db->select("SELECT DISTINCT cat_name, cat_id FROM ".$glob['dbprefix']."CubeCart_category");
$selCatID = $_POST['category'];
$selProdID = $_POST['products'];

echo "<form name='selCat' action=" . basename($PHP_SELF) . " method='POST'>
	<select name='category' size='1' onchange=selCat.submit()>";
for($i=0;$i<count($latestProducts);$i++){
	
		$catID = $latestProducts[$i]['cat_id'];
		$catName = $latestProducts[$i]['cat_name'];
		
		if ($catID == $selCatID) { $selected = "selected"; }
		else { $selected = ""; }
		echo "<option value=$catID $selected>$catName</option>";
}
echo "</select></form>";

if (is_null($selCatID) == false)
{

	$latestProducts = $db->select("SELECT productId, name FROM ".$glob['dbprefix']."CubeCart_inventory WHERE `cat_id` = $selCatID ORDER BY productId DESC");

	echo "<br><form name='selProduct' action=" . basename($PHP_SELF) . " method='POST'>
			<select name='products' size='1' onchange=selProduct.submit()>";
	for($i=0;$i<count($latestProducts);$i++){
	
		$prodID = $latestProducts[$i]['productId'];
		$prodName = $latestProducts[$i]['name'];
		
		if ($prodID == $selProdID) { $selected = "selected"; }
		else { $selected = ""; }
		echo "<option value=$prodID $selected>$prodName</option>";
	}
		echo "</select><input type='hidden' name='category' value=$selCatID></form>";
}

if (is_null($selProdID) == false)
{
	echo "<br><form name='submitProd' action='products.php?act=viewProd&productId=$selProdID' method='POST'>
		<input type='submit' value='View Product Info.'></form>";
}
	

?>

in other words this php is fine it gets the data and displays it properly but the infor is dynamic its not one piece of information its bringing up. i need to know how to pull that information either into a seperate page or somehow make the php show up in a certian spot on a page.
Post Reply