Page 1 of 1

HELP! Urgent... Using php with forms and database...

Posted: Wed Jul 23, 2003 3:18 am
by parkin
hi.. I'm new at php.. got stuck wif something.. it's kinda urgent.. :cry:
the idea:
1) have a form wif a text box.. enter a prod_name ($form[prod_id])
2) then retrieve and display the prod_id of tht prod_name from the database..

I do know how to do a textbox and take the value.. Then here's the prob.. I'm not very good at retrieving from the database..
this i know:

connecting;

Code: Select all

&#1111;php]<?php
$prod = getProd("product"); // this is a function

// how to retrieve the results and display the prod_id of the prod_name/
?>&#1111;/php]

Code: Select all

&#1111;php]<?php
//this is the getProd function
function getProd($table)
&#123;
	global $prod_link;
	$query = "SELECT * from $table ORDER BY prod_name";
	$result = mysql_query($query, $prod_link);
	if ( ! $result)
		&#123;
		print "failed to open $table<p>";
		return false;
		&#125;
	$ret=array();
	$temp="";
	while($row=mysql_fetch_array($result))
		&#123;
   // if the prod name not blank.. and the prod family is not special
			if($row&#1111;prod_name]!=$temp && $row&#1111;prod_family]!="special")
			&#123;
				$row&#1111;prod_name]=$row&#1111;prod_name];
				array_push($ret,$row);
			&#125;
			$temp=$row&#1111;prod_name];
		&#125;			
	return $ret;
&#125;
?>&#1111;/php]
then I'm not sure on how to retrieve and display the prod_id from the function.. and not sure if this is correct.. Can anyone help? Thanks.. :)

Posted: Tue Jul 29, 2003 8:27 pm
by jmarcv
function getProd($table)
{
global $prod_link;
$query = "SELECT prod_id,prod_name,prod_family from $table ORDER BY prod_name";
$result = mysql_query($query, $prod_link);
if ( ! $result)
{
print "failed to open $table<p>";
return false;
}
$ret=array();
$temp="";
while(list($prod_id,$prod_name,$prod_family)=mysql_fetch_array($result))
{
// if the prod name not blank.. and the prod family is not special
if($prod_name!=$temp && $prod_family!="special")
{
array_push($ret,$prod_name.'|'.$prod_id);
}
$temp=$prod_name;
}
return $ret;
}
?>


The | becomes a delimiter.

To split it out, do this:
$prod = getProd("product");
while (list($key,$value) = each($prod )) {
list($name,$id)=split("\|",$value);
echo "Product name: $name Product id:$id<br>";
}

Posted: Tue Jul 29, 2003 8:47 pm
by parkin
Thanks a whole lot.. :)

Posted: Tue Jul 29, 2003 9:00 pm
by jmarcv
Sure:
On other thing. if you do this:

SELECT prod_id,prod_name,prod_family from $table ORDER BY prod_name group by prod_name

you will get only ONE record for each product name