Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts

    dynamically accessing querystring variables

    The site I'm working on is mainly an Arabic-teaching site. It has lessons, vocabulary lists, grammar charts, etc.
    I've got an Admin Control Panel, and one of the options is to 'Add New Lesson'.
    When adding a lesson the user gets to choose whether they want to add any vocabulary associated with the current lesson or not. If yes, then the user is asked for the number of new vocabulary words. This number gets stored in $no_vocabs and reaches the next page via the query string. According to this number, a loop "(for $i=0; $i<no_vocabs; $i++)..." generates, for each vocabulary word to be entered, a separate row of 10 textboxes (for the word itself, its root meaning, and 8 different versions of the same root word...Arabic can get complicated, you see ). The textboxes are named according to another loop within the main loop. For example, if the user chooses to enter 3 vocabulary words, the next page will display 3 rows of 10 textboxes each, that's 30 boxes in all. The textboxes, through the second loop will be named like '0vcell0', '0vcell1', '0vcell2', '0vcell3'...until '0vcell9', and then onto the next row: '1vcell0', '1vcell1', '1vcell2', ..., then the next row, until the last cell in the last row, '2vcell9'.
    So the user enters the words, and submits the form, to go to the next page, 'addLesson2.php'.
    Everything works fine and dandy up till here.
    The problem arises in 'addLesson2.php', when it's time to access all the values in the textboxes on the previous page. They're all in the querystring, '0vcell0=whatever&0vcell1=whateverelse', etc etc. To access them I've used this loop:
    Code:
            for($j=0;$j<$no_vocabs;$j++){
                   for($k=0;$k<10;$k++){ //for the ten textboxes
                        "$".$j."vcell".$k = $_GET[$j."vcell".$k];           
    			    }   
    	                       	         }
    So that if $j is currently '1' and $k is, say, '5', then the statement should produce this: $1vcell5.

    But it doesn't work, I get this error:
    Fatal error: Maximum execution time of 30 seconds exceeded in c:\phpdev\www\addlesson2.php on line 75

    I'm not very old at php (help...i'm a newbie!j/k ), so i might be just doing something ridiculous here.
    'If you don't stand for something, you'll fall for anything.'

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it looks like an infinite loop. This new page doesn't seem to have the $no_vocabs variable set so it will loop forever. You might want to try and pass it into the new page. If you cant figure out how, use a hidden value for which you can call $_GET['hiddenValue'] on so that you might not get that infinite loop any more.


    Jason

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that's what i'm doing. $no_vocabs is the value of the hidden input element, 'no_vocabs' on the previous page.
    Using the same method as in my previous post, this works:

    Code:
    for($j=0;$j<$no_vocabs;$j++){
                   for($k=0;$k<10;$k++){ //for the ten textboxes
                            echo(" $".$j."vcell".$k."<br>"); 
    		                   }   
    	                       	 }
    i replaced
    Code:
     "$".$j."vcell".$k = $_GET[$j."vcell".$k];
    with almost the same statement, except that the new statement doesn't assign any variables anything, it just prints, and it works. I do get all the previous page's input boxes' names printed out on the screen all right. So maybe it isn't the loop problem. Maybe I'm trying to do something the wrong way?
    Last edited by ASAAKI; 03-13-2003 at 04:21 PM.
    'If you don't stand for something, you'll fall for anything.'

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you might try and replace this line
    for($j=0;$j<$no_vocabs;$j++){
    with something like
    for($j=0;$j<$_GET['no_vocabs'];$j++){

    cause it would seem that unless you did an assignment outside the for loop with something like $no_vocabs=$_GET['no_vocabs'] you would still get an infinite outer loop. So it would give you all the data it just wouldn't leave the loop.


    Jason

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also - I think you'd need to eval the assignment statement

    PHP Code:
    eval("\$".$j."vcell".$k." = \$_GET['".$j."vcell".$k."'];"); 
    which would translate as

    $0vcell0 = $_GET['0vcell0'];

    which of course is invalid PHP as variable names cannot start with numerics

    Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, the eval function was just what i needed
    'If you don't stand for something, you'll fall for anything.'


  •  

    Posting Permissions

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