Array heading

Discussion in 'PHP' started by trenttdogg, Feb 20, 2013.

  1. #1
    Hello,
    I have a question about arrays. I have everything set up where a query gets the data from the db and places into an array which is displayed on my page. The problem is that the "array title" does not change with the page.
    Here is the piece of code that places the "array heading" on my page.
    <?php
    echo '
            Worksheets for ',(
                isset($_GET['queryData']) ?
                htmlspecialchars($_GET['queryData']).' ' :
                'Math'
            ),'';
    ?>
    Code (markup):
    So for example if I'm on page test.php?subject=math&category=counting I would want the heading to reflect "Worksheets for Math-Counting" or something to that effect. Right now it will say Worksheets for Math on every page(the subject and category variables do not display). I've tried removing the word math from the ' ' and putting a GET there. Don't work.
    Thanks in advance for any help.
     
    trenttdogg, Feb 20, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,898
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    echo out the "worksheets for" and then do a foreach

    It doesn't have to be in one statement. Don't be afraid of white space and making code readable. Any impact on speed will be more than overcome by the bandwidth and browser processing speeds.
     
    sarahk, Feb 20, 2013 IP
  3. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #3
    I'm very new at php. Can you give example of foreach statement?
    Thanks,
     
    trenttdogg, Feb 20, 2013 IP
  4. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #4
    
     
    $get = '';
    foreach($_GET as $key => $value) {
    $temp = (string) trim(strip_tags($value));
    if(!empty($temp)) $get .= htmlspecialchars($temp) . ' ';
    }
    printf("Worksheet for %s Math", $get);
     
    
    PHP:
     
    crivion, Feb 21, 2013 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,898
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #5
    If you don't know the basics of the syntax check out php.net, in this case you'd go to
    www.php.net/foreach
    Code (markup):
     
    sarahk, Feb 21, 2013 IP