Jump to content

PHP Page Generation question


Recommended Posts

Is it possible to generate page content through php where the actual default values are a variable?

 

What i'm hoping to do is something along the lines of this:

 

for($x=1;X<=5;x++)

{

echo "<input name='$variablename_".x."' type=text value='$_post['$variablename_".x."']'>";

}

 

 

I realise an array would make this completely easier, but i'm hoping to be able to pass to a function the variable name I want to be generated on a form as plain text and have yet to figure out how to accomplish this. Thanks in advance for any assistance I may receive.

 

-Moraff

 

 

 

Link to comment
Share on other sites

  • 4 weeks later...

You can't really do that. There is an alternative. You can get what the user submitted and use a switch/case statement to get required variable. Since you don't seem have an infinite number of variables, this should work. Sorry if I'm not really clear. I can't think of a better way to explain this. Maybe with some code?

 

switch $_POST['input'] {
    case 'a':
        $var = $a;
        break;
    case 'b':
        $var = $b;
        break;
// Repeat for the other 3...
}

Link to comment
Share on other sites

  • 2 weeks later...
Is it possible to generate page content through php where the actual default values are a variable?

 

What i'm hoping to do is something along the lines of this:

 

for($x=1;X<=5;x++)

{

echo "<input name='$variablename_".x."' type=text value='$_post['$variablename_".x."']'>";

}

 

 

I realise an array would make this completely easier, but i'm hoping to be able to pass to a function the variable name I want to be generated on a form as plain text and have yet to figure out how to accomplish this. Thanks in advance for any assistance I may receive.

 

-Moraff

 

 

I don't think I'm understanding you correctly, but yeah. You can use PHP and HTML interchangeably. So for example, say you have a variable X, and you want an HTML Input Form's default value to be X. You could do:

 

<?php
    for ($varX=1; $varX <= 5; $varX++) {
        echo "<input name='$varX' type='text' value='$varX'/>";
    }
?>

 

However, it would be easier, I think, to not use the echo, and just write the HTML:

 

<?php
for ($varX=1; $varX <= 5; $varX++) { ?>
	<input name='<?php $varX ?>' type='text' value='<?php $varX ?>'/>
<?}
?>

 

Even though the HTML is outside of the <?php ?> tags, it is still nested in the loop.

 

 

 

I'm not sure what you meant to do by using $_POST. Could you explain that briefly so I can understand the problem better?

 

Cheers,

~Derek

P.S. We really need a PHP code tag. I think it would be a great addition.

P.P.S. Yeah, I think this topic is dead...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...