SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Writing PHP Scripts - Working out how to do something...

    I've been learning php for about 3 months now. I still have plenty to learn, but I've learnt enough to be able to create a nice online shopping cart with plenty of nifty features. When I first started to learn php, it was all about learning the language and the functions to do what you wanted. But now that I know many of the functions and capabilities of php, the hard part is now thinking up of ways to do certain things. I guess you get better at working out methods and algorithms as you use php more and more. What are your thoughts on this? Do you guys sometime sit there blank headed for a a few minutes just trying to wrap your head around something only to forget it as soon as you think of a possible method/solution

    Here's a function I made today for a website I'm making.

    PHP Code:
    <?php
    function vehicles_list(){
    // The 'display text' goes on the left of the vertical line, and the 'link' goes on the right.
        
    global $url;
        
    $array[] = "Baja Mini|".$url."Vehicles/Mini/";
        
    $array[] = "Baja 150|".$url."Vehicles/150/";
        
    $array[] = "Baja 250|".$url."Vehicles/250/";
        
    $array[] = "Baja 650|".$url."Vehicles/650/";
        
    $array[] = "Baja 800|".$url."Vehicles/800/";
        
    $array[] = "Baja 1100|".$url."Vehicles/1100/";
        
    $array[] = "Baja 1300|".$url."Vehicles/1300/";
        
    $array[] = "Baja 1800|".$url."Vehicles/1800/";
        
    $array[] = "Baja Utility 250|javascript:;";
        
    $array[] = "Baja Utility 300|javascript:;";
        
    $array[] = "Baja Utility 500|javascript:;";
        
    $array[] = "Baja Utility 500XL|javascript:;";
        
    $array[] = "Baja Utility 650|javascript:;";

        
    $int 0;
        
    $result '<div class="fixed-width">'."\n".'<table width="100%" border="0" cellspacing="0" cellpadding="0">'."\n";
        while(
    $int count($array) || gettype($int 4) !== "integer"){
            if(
    gettype($int 4) == "integer" && $int !== 0){
                
    $result .= "\n</tr>";
            }
            if(
    gettype($int 4) == "integer"){
                
    $result .= "<tr>";
            }
            
    $link explode("|"$array[$int]);
            if(!empty(
    $array[$int])){
                
    $result .= "\n\t".'<td align="left"><a class="vehicles-list" href="'.$link[1].'" title="'.$link[0].'" id="'.$link[0].'" name="'.$link[0].'">'.$link[0].'</a></td>';
            }else{
                
    $result .= "\n\t".'<td align="left"></td>';
            }
            
    $int++;
        }
        
    $result .= "\n</tr>\n</table>\n</div>";
    return 
    $result;
    }
    ?>
    Basically, you put all the different products and their links in the array. Then the script turns it into a table which is four columns by a dynamic number of rows (depending on how many products there are). This was quite hard for me too do and so even though the function is small/short, it took me about 25 minutes (including a few interruptions and what not).

    While I'm at it, does anyone want to have a go and tweaking the above script? have I done anything wrong or can anyone see an area that could've been done easier?

    Cheers!

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    Toronto
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi Wardrop,

    First off, if you plan on expanding the number of products, I would suggest storing all product info in an external database (MySQL, flatfile, etc). It's always a good idea to separate data from code.


    To give you some ideas on improving your code, I cleaned it up a bit (note - I continued to store the vehicle data in the code)

    PHP Code:
    <?php
    function vehicles_list() {
        global 
    $url;
        
    $rows_per_col 4;
        
        
    $table_header '<div class="fixed-width"><table width="100%" border="0" cellspacing="0" cellpadding="0">';
        
    $table_footer '</tr></table></div>';
        
    $table_content '';    
       
         
    //store info as key=>value
         
    $vehicles = array( 
             
    "Baja Mini" => $url."Vehicles/Mini/",
             
    "Baja 150" => $url."Vehicles/150/",
             
    "Baja 250" => $url."Vehicles/250/",
             
    "Baja 650" => $url."Vehicles/650/",
             
    "Baja 800" => $url."Vehicles/800/",
             
    "Baja 1100" => $url."Vehicles/1100/",
             
    "Baja 1300" => $url."Vehicles/1300/",
             
    "Baja 1800" => $url."Vehicles/1800/",
             
    "Baja Utility 250" => "javascript:;",
             
    "Baja Utility 300" => "javascript:;",
             
    "Baja Utility 500" => "javascript:;",
             
    "Baja Utility 500XL" => "javascript:;",
             
    "Baja Utility 650" => "javascript:;"
        
    );

       
    $x 0;
        foreach(
    $vehicles as $key=>$value) {
            
    //see if we need to start a new row
            
    if($x == 0)
                
    $table_content .= "<tr>";
             
            
    //use sprintf() to keep the code cleaner
            
    $table_content .= sprintf('<td align="left"><a class="vehicles-list" href="%s" title="%s" id="%s" name="%s">%s</a></td>',
                                                    
    $value$key$key$key$key);
            
            
    //check if we need to start end the row
            
    if(++$x == $rows_per_col) {
                
    $table_content .= "</tr>";
                
    $x 0;
            }
        }
        return 
    $table_header.$table_content.$table_footer;
    }
    ?>
    If you have any questions i'd be happy to answer
    hope that helps


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
  •