elementary PHP Script Help

Discussion in 'PHP' started by david_sakh, Dec 28, 2004.

  1. #1
    I know very little about php scripting (lets go with just about nothing other than basic syntax; I'm just experimenting here) so this might not even work. Basically I am going to write a few variables at the top of a page, $style determining the wrapper includes (heavy html formatting around each block of text), $paragraph0-4 containing the paragraph strings and any additional formatting. The values of $paragraph0-4 are put into PARarray and the PARarray paragraphs are then printed without me having to do much.
    The only bad part is it's not working. :(

    For some reason, it comes across my for loop and calls it "unexpected". Since this comes after my array declaration, I'm guessing maybe there's a syntax error there, but I can't find it.

    Any help would be great, and again, sorry if this post is just sad in its ametuerity

    
    
    <?
    //$style is set to either "article" or "box" depending on the content type
    //$paragraph is the paragraph content to be used in the array
    //
    //THIS PART GOES IN FILE (move later)
    $paragraph0 = "text of paragraph 1.";
    $paragraph1 = "Well here's paragraph 1";
    $paragraph2 = "Blahrdidy blardity paragraph 2";
    $paragraph3 = "Blahrdidy blardity paragraph 3";
    $paragraph4 = "Blahrdidy blardity paragraph 4";
    //puts the value of the paragraph string variables into an array known as:
    //$PARarray['PARarray'][i]
    $PARarray = array('PARarray' => array(0 => $paragraph0, $paragraph1, $paragraph2, $paragraph3, $paragraph4));
    //
    //END OF FILE PART
    //
    //this part prints out the values of each array value, calling an html wrapper for each
    //based on $style and $image
    //the name of the wrapper will be $style_wrapper.php i.e. box_wrapper.php
    (for $i=0; $i<4; $i++)
    {
    //include the top part of the specific wrapper
    require("$style_wrapper_top.php");
    //output the text for this paragraph
    print("$PARarray['PARarray'][$i]");
    //include the bottom part of the specific wrapper (includes spacing separation cell)
    require("$style_wrapper_top.php");
    }
    ?>
    
    
    Code (markup):
     
    david_sakh, Dec 28, 2004 IP
  2. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    //  The easiest way to build an array for this...
    //  $array_name[] = your_data;    this creates an array named $array_name and 
    //  assigns your_data to the first element. When a key to the array isn't
    //  specifieds, as in [], the PHP assigns a 0, or if it's not the first one then
    //  whichever number is next. in this example it would be 1.
    //
    //  So to reference this value, if your_data was this first item added to the array you would:
    //
    //  echo $array_name[0];
    //
    //  But since it doesn't appear you have a need to call a particular item out of the array
    //  we don't need to worry about keeping track of what the key is for each item.
    //
    //  Which all comes down to this.
    //
    //  $paragraph_arr[] = "text of paragraph 1.";
    //  $paragraph_arr[] = "Well here's paragraph 1";
    //  $paragraph_arr[] = "Blahrdidy blardity paragraph 2";
    //  $paragraph_arr[] = "Blahrdidy blardity paragraph 3";
    //  $paragraph_arr[] = "Blahrdidy blardity paragraph 4";
    //
    //  You have created an array with 5 elements or items.
    //
    //  To loop through an array use PHP's foreach loop instead of a for loop.
    //
    //  foreach ( $array_name as $key => $value ) {  your code }
    //  
    //
    //  in our example,
    //
    //  foreach ( $paragraph_arr as $key => $value )
    //  {
    //       echo "$key $value<br>";
    //  }
    //
    //  would output
    //  
    //  0 text of
    //  1 Well
    //  2 blah...2
    //  3 blah...3
    //  4 blah...4
    //
    //  you could also
    //
    //    foreach ( $paragraph_arr as $value ) { your code }
    //
    //  and just not have access to the key, it you don't need it.
    // 
    //  For your loop it would be 
    //
    //  foreach ( $paragraph_arr as $value )
    //  {
    //       echo top_part_box();
    //       echo "$value<br>";         //  this is your paragraph
    //       echo bottom_part_box();
    //  }
    //
    //  And instead of using a seperate file you could add these functions to your main file.
    //
    //  function top_part_box()
    //  {
    //     return "This is the top part<br>";
    //  }
    //  
    //  function bottom_part_box()
    //  {
    //     return "This is the bottom part<br><br>";
    //  }
    //
    //  and add functions for whatever different top and bottom flavors you want and call accordingly.
    //
    //  So final code would be...
    
    
    <?php
    
      $paragraph_arr[] = "text of paragraph 1.";
      $paragraph_arr[] = "Well here's paragraph 1";
      $paragraph_arr[] = "Blahrdidy blardity paragraph 2";
      $paragraph_arr[] = "Blahrdidy blardity paragraph 3";
      $paragraph_arr[] = "Blahrdidy blardity paragraph 4";
    
      foreach ( $paragraph_arr as $value )
      {
           echo top_part_box();
           echo "$value<br>";                //  this would be your paragraph
           echo bottom_part_box();
      }
    
      function top_part_box()
      {
         return "This is the top part<br>";
      }
    
      function bottom_part_box()
      {
         return "This is the bottom part<br><br>";
      }
    
    ?>
    
    PHP:
    I've posted the code here:

    http://www.tropicrentals.com/test.php

    Use http://www.php.net as a reference. Most everything you need to know right now can be found there.
     
    rvarcher, Dec 29, 2004 IP
    david_sakh likes this.
  3. david_sakh

    david_sakh Peon

    Messages:
    1,225
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Wow - that was amazingly generous of you!

    Thank you for taking the time to help me! :)
     
    david_sakh, Dec 30, 2004 IP
  4. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No problem at all. Glad to help out. :)
     
    rvarcher, Dec 30, 2004 IP