Results 1 to 6 of 6

Thread: Dynamic variable naming?

  1. #1
    Join Date
    Jun 2008
    Posts
    53
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Dynamic variable naming?

    Hey all,
    sort of new to php, but I am a c++ etc coder so I know general coding practice.

    If there a way for me to have a dynamic number of text inputs on a php page in a form, which I am naming foo$ct where $ct is postincremented in my while loop. Can I on the action= page load those variables as $foo$c where $c is a for loop on 0 -> $ct?

    Or should I just be storing everything with an array?

    Some code:
    Code:
    while($row = mysql_fetch_array($result)) {
    			?>
    				<tr>
    					<td>
    						<?php
    							$sht = trim($row['shortannounce']);
    							print "<input type=\"hidden\" name=\"key$ct\" value=\"$sht\" />";
    							print "<textarea rows=\"4\" cols=\"40\" class=\"small\" name=\"stannounce$ct\">".$sht;
    						?>
    theres a $ct++; at the end of the while loop.

    How should I better handle this issue of dynamicly named inputs for the action page?
    Last edited by Irishjugg; 12-08-2008 at 03:09 AM.

  2. #2
    Join Date
    Jun 2008
    Posts
    53
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Ive been looking around the interwebs, this problem is basically the what do I do after I make a dynamic form. I have all the different inputs to handle, how do I dynamically handle them?

  3. #3
    Join Date
    Jun 2008
    Posts
    53
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Nevermind, I found a solution.

    Thanks to anyone who read.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The correct method is to make the inputs into an array, by giving them the same name which should end with a []. If you ever find yourself using anything resembling variable variables, you're Doing It Wrong®.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jun 2008
    Posts
    53
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    I just ended up making a variable name as a string as $inputtemp = "thing$i" then loading $inputtemp or something like that.

    I guess its ALMOST like a variable variable, but not quite as I have no $$'s, for example:
    $st = trim($_POST['stannounce'.$i]);

    With the way using your array Im still a bit confused, but the solution I have works, is what Im doing bad coding practice?

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes. It's unnecessary here, and vastly overcomplicated. Input arrays are the way to go for variable numbers of inputs.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •