Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to dynamic use printf together with input type textfields?

Status
Not open for further replies.

rogerzebra

Technical User
May 19, 2004
216
SE
Hi all I hope you're doing well,
I was hoping for some help with an issue I have on how to print a dynamic result from a query and use the query result together with input type textfields?
It seems that this needs an thorough explanation.

I have a formpage (step1)were a query has been generated based on how many textfields(classcodes)been filled in.The result from step1 is shown on a new page. here is some code
Code:
if ($myrow = mysql_fetch_array($result)) 
	{
        echo "<tr><td><b>ClassCode</b></td><td><b>Classification</b><td><b>Rate</b></td>...jada,jada
do {
   printf("<tr><td>%s</td><td>%s</td>...jada,jada

My problem is a) Part of the result has been generated from the database the "%s", So how to use prinf together with input type text fields and were the value is a variable? Something like
Code:
<td>%s</td><td><input type='text' name='PremiumBasis' value=\"$PremiumBase1\"</td>%s<td>

....and if it's more than one classcode. Generate next value like:
Code:
<td>%s</td><td><input type='text' name='PremiumBasis' value=\"$PremiumBase2\"</td>%s<td>

b) the input type fields value ($variable/variables)is supposed to be calculated in step 3. As I said, my formpage is dynamic, the number of filled in textfields generates the same number of rows.

So, my problem is how to generate variables in the input type textfields dynamicly? like textfield1 =$variable1 if textfield2 is filled out aswell give it the $variable2 etc..

I have those two pieces working separate but I can't get those two pieces to work together. Any ideas?
I understand if this is hard to stift thru so any help what so ever is greately welcome.
Thanks and have a nice day :)
 
here a small example where I build up a menu which is generated from the database. you can also use it for inputfields like above
Code:
$q_result = mysql_query("SELECT * from contenttable order by coorder");
while ($ary = mysql_fetch_assoc($q_result)) { //one
  while (list($key,$val) = each($ary)) { //two
   // put the var value in the variable fieldname
   $$key = $val;  
  }  // end two

   print"<tr><td><a href=index.php?coid=$coid>$cotitel</a><br></td></tr>"; 


} //end one
 
hi hos2,
thanks for your respond, I'll try that, so, $ary is a replace for row, right? I can't see how that is going to dynamicly increment the variable name of the input type fields though? Or is that what $$key is doing? Sorry for my confusion. Please explain
/rz
 
yep $ary holds the row results
it loops through all the fieldnames of you're select statement and puts the values ($value) into variables of their fieldname ($$key)

so if you have 10 fieldnames you get 10 (in this case select options) if you have 20 fieldnames you get 20 select options

but if you store the fieldname, value and name in like 3 fields then you must use if then to assign the values

 
hos2,
I'm sorry for a delayed post, but I'm relly trying before I'm posting back so it is a good thing :)

I tried your code like this:
Code:
$q_result = mysql_query("
	SELECT classcode_rate.ClassCode, classcode_rate.Classification, classcode_rate.Rate, carriernameid.CarrierName
	FROM classcode_rate 
	INNER JOIN carriernameid
	ON classcode_rate.CarrierID=carriernameid.CarrierID
	WHERE carriernameid.CarrierName='$CarrierName'
	AND classcode_rate.ClassCode='$ClassCode'
	UNION
	SELECT classcode_rate.ClassCode, classcode_rate.Classification, classcode_rate.Rate, carriernameid.CarrierName
	FROM classcode_rate 
	INNER JOIN carriernameid
	ON classcode_rate.CarrierID=carriernameid.CarrierID
	WHERE classcode_rate.ClassCode='$ClassCode2'
	UNION
	SELECT classcode_rate.ClassCode, classcode_rate.Classification, classcode_rate.Rate, carriernameid.CarrierName
	FROM classcode_rate 
	INNER JOIN carriernameid
	ON classcode_rate.CarrierID=carriernameid.CarrierID
	WHERE classcode_rate.ClassCode='$ClassCode3'");

while ($ary = mysql_fetch_assoc($q_result)) 
{   
while (list($key,$val) = each($ary)) {       
$$key = $val;  
} 
printf("<td><input type='text' name='ClassCode' value='$ClassCode'></td><td><input type='text' name='Classification' value='$Classification'></td><td><input type='text' name='PremiumBasis' value='$PremiumBasis'></td><td><input type='text' name='Rate' value='$Rate'></td><td><input type='text' name='EstAnnualPrem' value='EstAnnualPrem$'></td><td><input type='text' name='NetRate' value='$NetRate'></td>
				</tr>\n", $myrow["ClassCode"], $myrow["Classification"], $myrow["PremiumBasis"], $myrow["Rate"], $myrow["EstAnnualPrem"], $myrow["NetRate"]);
} 
?>
This code adds a new row for each filled in textfield, which is good but only the half way of my problem.
This is what I'm trying to accomplish: when 1 classcode is typed in the input type text field on page1 print this on next page;
Code:
td><input type='text' name='ClassCode' value='$ClassCode'></td><td><input type='text' name='Classification' value='$Classification'></td><td><input type='text' name='PremiumBasis' value='$PremiumBasis'></td><td><input type='text' name='Rate' value='$Rate'></td><td><input type='text' name='EstAnnualPrem' value='EstAnnualPrem$'></td><td><input type='text' name='NetRate' value='$NetRate'></td>
				</tr>\n", $myrow["ClassCode"], $myrow["Classification"], $myrow["PremiumBasis"], $myrow["Rate"], $myrow["EstAnnualPrem"], $myrow["NetRate"];

when 2 classcodes is typed in the input type text fields on page1 change all the values from $variable to $variable2 on next page;
Code:
td><input type='text' name='ClassCode' value='$ClassCode2'></td><td><input type='text' name='Classification' value='$Classification2'></td><td><input type='text' name='PremiumBasis' value='$PremiumBasis2'></td><td><input type='text' name='Rate2' value='$Rate2'></td><td><input type='text' name='EstAnnualPrem' value='$EstAnnualPrem2'></td><td><input type='text' name='NetRate' value='$NetRate2'></td>
				</tr>\n", $myrow["ClassCode"], $myrow["Classification"], $myrow["PremiumBasis"], $myrow["Rate"], $myrow["EstAnnualPrem"], $myrow["NetRate"];

The reason for this is they

If only 1 classcode use this fraction:
$EstAnnualPrem = ($PremiumBasis) *($Rate/100);
$totalClassPrem = ($EstAnnualPrem);

If 2 classcodes has filled in use this fraction:
$EstAnnualPrem2 = ($PremiumBasis2) *($Rate2/100);
$totalClassPrem = ($EstAnnualPrem) + ($EstAnnualPrem2);

If 3 ...$EstAnnualPrem3 = ($PremiumBasis3) *($Rate3/100);
$totalClassPrem = ($EstAnnualPrem) + ($EstAnnualPrem2)+ ($EstAnnualPrem3);
..etc

Any ideas?

"I finally took the step out to start working from home, and I have never looked back" :)
You can do it too. It's fun and you will never regret it. Send me an email and check out how I'm doing in my new business to roger@workfromhomemarketing.com
 
I think I need something like this:
if (isset($_POST['submit'])) {
$ClassCode = $_POST['ClassCode'];
$Classification = $_POST['Classification'];
$PremiumBasis = $_POST['PremiumBasis'];
$Rate = $_POST['Rate'];
$EstAnnualPrem = $_POST['EstAnnualPrem'];
$NetRate = $_POST['NetRate'];
for ($c = 1; $c <= $ClassCode; $c++) {
for ($d = 1; $d <= $Classification; $d++) {
for ($p = 1; $p <= $PremiumBasis; $p++) {
for ($r = 1; $r <= $Rate; $r++) {
for ($e = 1; $e <= $EstAnnualPrem; $e++) {
for ($n = 1; $n <= $NetRate; $n++) {
I can't get this to work though, would be great if anyone could help me clear this up :)
/rz

"I finally took the step out to start working from home, and I have never looked back" :)
You can do it too. It's fun and you will never regret it. Send me an email and check out how I'm doing in my new business to roger@workfromhomemarketing.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top