count form content

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
User avatar
eaposztrof
Forum Newbie
Posts: 12
Joined: Sun Jan 07, 2007 8:58 pm
Location: székelyudvarhely / erdélyország (transylvania)
Contact:

count form content

Post 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";
Last edited by eaposztrof on Sun Jan 07, 2007 10:59 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please elucidate. What are you trying to achieve?
User avatar
eaposztrof
Forum Newbie
Posts: 12
Joined: Sun Jan 07, 2007 8:58 pm
Location: székelyudvarhely / erdélyország (transylvania)
Contact:

Post 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?
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

If I have understood your question properly
I think $_POST is what you Want
count($_POST) holds The length
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>
User avatar
eaposztrof
Forum Newbie
Posts: 12
Joined: Sun Jan 07, 2007 8:58 pm
Location: székelyudvarhely / erdélyország (transylvania)
Contact:

Post 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']);}
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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 ??
User avatar
eaposztrof
Forum Newbie
Posts: 12
Joined: Sun Jan 07, 2007 8:58 pm
Location: székelyudvarhely / erdélyország (transylvania)
Contact:

Post 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,
User avatar
eaposztrof
Forum Newbie
Posts: 12
Joined: Sun Jan 07, 2007 8:58 pm
Location: székelyudvarhely / erdélyország (transylvania)
Contact:

Post 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\"";  
...
Last edited by eaposztrof on Mon Jan 08, 2007 3:37 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
eaposztrof
Forum Newbie
Posts: 12
Joined: Sun Jan 07, 2007 8:58 pm
Location: székelyudvarhely / erdélyország (transylvania)
Contact:

sajnos

Post 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.
User avatar
eaposztrof
Forum Newbie
Posts: 12
Joined: Sun Jan 07, 2007 8:58 pm
Location: székelyudvarhely / erdélyország (transylvania)
Contact:

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

I'm not sure if this is what you're looking for...

Code: Select all

${'text_cpanel_' . $i}
Post Reply