Page 1 of 1

count form content

Posted: Sun Jan 07, 2007 9:25 pm
by eaposztrof
hi all!
how i can count form content
or how i can simplify (optimize) this code:

Code: Select all

if (isset($_REQUEST['save_engine'])) {
  if (isset($_REQUEST['asd1'])) {save_config("asdB.php","asd1",$_REQUEST['asd1']);}
  if (isset($_REQUEST['asd2'])) {save_config("asdB.php","asd2",$_REQUEST['asd2']);}
//...
}

 echo "<form action=\"" method=post>\n";
 echo "<input type=text name=\"asd1\" value=\"".read_config("asd.php","asd1")."\"\n";  
 echo "<input type=text name=\"asd2\" value=\"".read_config("asd.php","asd2")."\"\n";  
//...
 echo "<input type=submit name=save_engine></form><td></tr>\n";

Posted: Sun Jan 07, 2007 9:33 pm
by volka
Please elucidate. What are you trying to achieve?

Posted: Sun Jan 07, 2007 10:54 pm
by eaposztrof
volka wrote:Please elucidate. What are you trying to achieve?
i don't want to type 1000 line of these:

Code: Select all

if (isset($_REQUEST['asd1'])) {save_config("asdB.php","asd1",$_REQUEST['asd1']);}
when int he form i have 1000 input:

Code: Select all

echo "<input type=text name="asd1" value="".read_config("asd.php","asd1").""\n";
when i can count the posted elements of form, i can generete these lines with the for() cycle, but how?

Posted: Sun Jan 07, 2007 11:06 pm
by neel_basu
If I have understood your question properly
I think $_POST is what you Want
count($_POST) holds The length

Posted: Sun Jan 07, 2007 11:45 pm
by volka
try

Code: Select all

<?php
function read_config($a,$b) {
	return 'val_'.$b;
}

function save_config($a,$b,$c) {
	echo "<div>save_config($a,$b,$c)</div>\n";
}
?>
<html>
	<head><title>php/html array</title></head>
	<body>
<?php	
if ( isset($_POST['asd']) && is_array($_POST['asd']) ) {
	foreach($_POST['asd'] as $key=>$value) {
		save_config('asdB.php', $key, $value);
	}
}
?>
		<form action="#" method="post">
			<div>
			<?php for ($i=0, $z=rand(10,15); $i<$z; $i++) { ?>
				<input type="text" name="asd[asd<?php echo $i; ?>]" value="<?php echo read_config('asd.php', 'asd'.$i); ?>" />
				<br />
			<?php } ?>
				<input type="submit" />
			</div>
		</form>
 	</body> 
</html>

Posted: Sun Jan 07, 2007 11:49 pm
by eaposztrof
neel_basu wrote: count($_POST) holds The length
it's usefull, and i wat to:

- generate #"count($_POST)" line, like this:

Code: Select all

if (isset($_REQUEST[$inputname1])) {save_config("asdB.php","$inputname1",$_REQUEST['$inputname1']);}

Posted: Sun Jan 07, 2007 11:54 pm
by neel_basu
1 moment
are you trying to generate/Write php codes on the fly/WhileRunning and compile it on the fly and then show it ??

Posted: Mon Jan 08, 2007 12:13 am
by eaposztrof
volka wrote:try

Code: Select all

<?php
function read_config($a,$b) {
	return 'val_'.$b;
}

function save_config($a,$b,$c) {
	echo "<div>save_config($a,$b,$c)</div>\n";
}
?>
<html>
	<head><title>php/html array</title></head>
	<body>
<?php	
if ( isset($_POST['asd']) && is_array($_POST['asd']) ) {
	foreach($_POST['asd'] as $key=>$value) {
		save_config('asdB.php', $key, $value);
	}
}
?>
		<form action="#" method="post">
			<div>
			<?php for ($i=0, $z=rand(10,15); $i<$z; $i++) { ?>
				<input type="text" name="asd[asd<?php echo $i; ?>]" value="<?php echo read_config('asd.php', 'asd'.$i); ?>" />
				<br />
			<?php } ?>
				<input type="submit" />
			</div>
		</form>
 	</body> 
</html>
sorry.. nice, but i can't generate the "input" lines,

Posted: Mon Jan 08, 2007 12:49 am
by eaposztrof
how i can put in array the name's and value's of inputs in the form? the form is static!

Code: Select all

echo "<form action=\"" method=post>\n";
 echo "<input type=text name=\"asd1\" value=\"asd1_value\""; 
 echo "<input type=text name=\"asd2\" value=\"asd2_value\"";  
...

Posted: Mon Jan 08, 2007 9:04 am
by feyd
eaposztrof, we're not asking for much. Just use the proper tags.

Code: Select all

should only be used if you cannot find the most suitable tag. We have many to choose from.

Posted: Mon Jan 08, 2007 10:59 am
by volka
eaposztrof wrote:sorry.. nice, but i can't generate the "input" lines,
That's only an example and I quite sure somehow you have to generate the input. Your example
eaposztrof wrote:echo "<form action="" method=post>\n";
echo "<input type=text name="asd1" value="".read_config("asd.php","asd1").""\n";
echo "<input type=text name="asd2" value="".read_config("asd.php","asd2").""\n";
//...
is straight forward and so is mine.
The idea is to give the elements names so they're arranged as an array when php parses the request parameters.
And if you send a static form to the browser

Code: Select all

<form action="#" method="post">
	<div>
		<input type="text" name="asd[asd1]" value="<?php echo read_config('asd.php', 'asd1'); ?>" /><br />
		<input type="text" name="asd[asd2]" value="<?php echo read_config('asd.php', 'asd2'); ?>" /><br />
		<input type="text" name="asd[asd3]" value="<?php echo read_config('asd.php', 'asd3'); ?>" /><br />
		<input type="text" name="asd[asd4]" value="<?php echo read_config('asd.php', 'asd4'); ?>" /><br />
		<input type="submit" />
	</div>
</form>
it will make no difference. The browser nevers cares about how the html it gets was created. It justs sends the parameters according to the form code it gets. And on the other hand php does not care "why" parameters are sent with a http request. It just parses them according to a simple ruleset. In this case php will put the values in $_POST['asd']['asd1'], $_POST['asd']['asd2'] .... and the processing part of the script will still work.

sajnos

Posted: Mon Jan 08, 2007 4:43 pm
by eaposztrof
volka wrote: The idea is to give the elements names so they're arranged as an array when php parses the request parameters.
And if you send a static form to the browser
but i can't change the name fields. too many and too complicated.. i don't create the page, but recreate, regretfully.

Posted: Mon Jan 08, 2007 10:31 pm
by eaposztrof
i try to rebuild all with your sample, and i stuck again:

i have these, like simple text:

Code: Select all

"$text_cpanel_"
and

Code: Select all

$i
with # value.
and how i can create operable

Code: Select all

$text_cpanel_2

Posted: Mon Jan 08, 2007 11:52 pm
by aaronhall
I'm not sure if this is what you're looking for...

Code: Select all

${'text_cpanel_' . $i}