SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP - My first "Application" - HAVE A LOOK!

    Hi,

    I am a beginner at PHP and am just starting to love it!!!

    I decided to have a go with loops and the 'while' function and came up with this: LINKY

    INSTRUCTIONS: Pretty obvious... Just choose a couple of different colors and press submit!

    Source (Including PHP):

    PHP Code:
    <?php
    if(isset($_POST['obj1'])) $obj1 $_POST['obj1']; // putting posted variable into local variable
    if(isset($_POST['obj2'])) $obj2 $_POST['obj2']; // putting posted variable into local variable
    if(!isset($_POST['obj1'])|!isset($_POST['obj2'])) { // default colors for application
    $obj1='black';
    $obj2='white';
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test - Loops</title>
    <style type="text/css">
    <!--
    #floater {
        position: absolute;
        top: 10px;
        left: 10px;
    }
    body {
        margin:0;
        width: 500px;
        height: 300px;
        margin: 50px auto 0 auto;
        position: relative;
        border: 1px solid black;
    }
    -->
    </style>
    </head>
    <body>
    <div id="floater">
      <form id="form1" name="form1" method="post" action="color-thingy1.php">
        <select name="obj1" id="obj1">
          <option value="blue"<?php if($obj1=='blue') echo ' selected="selected"' ?> selected="selected" style="background-color:blue; color:white;">blue</option>
          <option value="red"<?php if($obj1=='red') echo ' selected="selected"' ?> style="background-color:red; color:white;">red</option>
          <option value="magenta"<?php if($obj1=='magenta') echo ' selected="selected"' ?> style="background-color:magenta; color:white;">magenta</option>
          <option value="pink"<?php if($obj1=='pink') echo ' selected="selected"' ?> style="background-color:pink; color:black;">pink</option>
          <option value="green"<?php if($obj1=='green') echo ' selected="selected"' ?> style="background-color:green; color:white;">green</option>
          <option value="yellow"<?php if($obj1=='yellow') echo ' selected="selected"' ?> style="background-color:yellow; color:black;">yellow</option>
          <option value="orange"<?php if($obj1=='orange') echo ' selected="selected"' ?> style="background-color:orange; color:white;">orange</option>
          <option value="black"<?php if($obj1=='black') echo ' selected="selected"' ?> style="background-color:black; color:white;">black</option>
          <option value="white"<?php if($obj1=='white') echo ' selected="selected"' ?> style="background-color:white; color:black;">white</option>
        </select>
        <select name="obj2" id="obj2">
          <option value="blue"<?php if($obj2=='blue') echo ' selected="selected"' ?> style="background-color:blue; color:white;">blue</option>
          <option value="red"<?php if($obj2=='red') echo ' selected="selected"' ?> style="background-color:red; color:white;">red</option>
          <option value="magenta"<?php if($obj2=='magenta') echo ' selected="selected"' ?> style="background-color:magenta; color:white;">magenta</option>
          <option value="pink"<?php if($obj2=='pink') echo ' selected="selected"' ?> style="background-color:pink; color:black;">pink</option>
          <option value="green"<?php if($obj2=='green') echo ' selected="selected"' ?> style="background-color:green; color:white;">green</option>
          <option value="yellow"<?php if($obj2=='yellow') echo ' selected="selected"' ?> style="background-color:yellow; color:black;">yellow</option>
          <option value="orange"<?php if($obj2=='orange') echo ' selected="selected"' ?> style="background-color:orange; color:white;">orange</option>
          <option value="black"<?php if($obj2=='black') echo ' selected="selected"' ?> style="background-color:black; color:white;">black</option>
          <option value="white"<?php if($obj2=='white') echo ' selected="selected"' ?> style="background-color:white; color:black;">white</option>
        </select>
        <input type="submit" name="button" id="button" value="Submit" />
      </form>
    </div>
    <?php 
    $counter
    =0//Set counter as 0
    $NumberOfObjects=375//number of squares to appear overall (Any number is okay - 375 happened to fit perfectly
    $Loop=0// Set Loop as 0
    $object=$obj1;
    while(
    $Loop<$NumberOfObjects) {
    if(
    $counter==1$object=$obj2;
    else 
    $object=$obj1;
    echo 
    '<span style="display: block; float: left; width: 20px; height: 20px; background:' $object ';"></span>';
    $Loop++; // Add 1 to loop variable
    if($counter==0$counter=1;
    else 
    $counter=0;
    }
    ?>
    </body>
    </html>
    Please let me know what you think... and if there are any ways to perhaps improve the code...? ...
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  2. #2
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll start it off you can use ternary operators for the first few lines, instead of if statements.
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  3. #3
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ...although there is an argument (that I agree with) that say that ternary operators are less readable in code :P

    The best type of loop to use would be a for loop - look them up on php.net - saves you having to use all the counter variables etc (or at least, puts them somewhere better)

    Instead of using $counter, just work out if $loop is an even number or not:

    if (($loop &#37; 2) == 0) {
    etc

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I've rewritten it. Maybe it can be useful to you to study this code. If you don't understand something feel free to ask

    PHP Code:
    <?php

    $c1 
    'black';
    $c2 'white';

    if(isset(
    $_GET['c1']))    $c1 $_GET['c1'];
    if(isset(
    $_GET['c2']))    $c2 $_GET['c2'];

    function 
    color_options($color) {
        foreach(array(
    'blue''red''magenta''pink''green'
            
    'yellow''orange''black''white') as $c
        {
            
    $sel = ($c == $color) ? 'selected' '';
            
    $bgr = ($c == 'black' || $c == 'blue') ? 'color:white' '';
            
    $opts[] = "<option value='$c' style='background-color:$c$bgr$sel>$c</option>";
        }
        return 
    implode("\n"$opts);
    }

    define('BLOCK_SIZE'20);
    define('GRID_WIDTH'21);
    ?>

    <html>
    <head>
    <title>Test - Loops</title>
    <style type="text/css">
    body {
        text-align: center;
    }
    div {
        margin: auto;
        display: block;
        width: <?php echo BLOCK_SIZE GRID_WIDTH?>px;
    }
    span {
        display: block; 
        float: left; 
        width: <?php echo BLOCK_SIZE?>px; 
        height: <?php echo BLOCK_SIZE?>px; 
    }
    </style>
    </head>

    <body>
        <div>
            <form>
                <select name="c1" onchange="this.form.submit()"><?php echo color_options($c1?></select>
                <select name="c2" onchange="this.form.submit()"><?php echo color_options($c2?></select>
                <input type="submit" value="ok" />
            </form>
            <?php 
                $n 
    GRID_WIDTH GRID_WIDTH;
                while(
    $n--) {
                    
    $c htmlspecialchars($n &#37; 2 ? $c2 : $c1);
                    
    echo "<span style='background-color:$c'></span>";
                }
            
    ?>
        </div>
    </body>
    </html>

  5. #5
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your ternary operator looks a bit dodgy there. Don't you mean &#37;, not # 37;?

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, of course it's &#37;, it's forum software that screwed it up.

  7. #7
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The checking if the $_GETs are set at the top is still utterly pointless even in frog's revised version because you're assuming they are set down in the HTML code. What if the GETs are not set and it gets down to the code where it uses $c1 and $c2? Error alert !
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If $_GET's are not set, it uses default values assigned to before (see first 2 lines).

  9. #9
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yeh, it works. I don't see the problem?

  10. #10
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think he was just posting to see if there was anything we could spot out that could have been done better

  11. #11
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is exactly what we offered.

  12. #12
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry

    I like seeing people excited about programming. When i tell people i program for a living they seem to think it's boring.

  13. #13
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I know exactly what you mean - but when you simply explain the money involved, the easy hours and that it's in everything they use on the web - they suddenly get interested, lol.

    Really glad to see you're enjoying it JimmyPaddy - also great to see you do your own thing. Fed up here of people not exploring; using existing stuff all the time and never really learning the actual language.

    If it wasn't for exploration, we wouldn't have America, drugs or rock and roll. Or PHP, as a matter of fact.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  14. #14
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the tips and advice...

    I will have a go at upgrading the code when i get some time. But seriously thanks for the tips .. especcially on the ternary function!

    Galen & Arkinstall -> PHP is great! ... I love exploring the ways you can pretty much do anything! Best thing of all is that it is so so LOGICAL!!! You can sit down and work out how something is going to work on a post-it note and then 10 mins later you'll have it working for real!

    If it wasn't for exploration, we wouldn't have America, drugs or rock and roll. Or PHP, as a matter of fact.
    Thank goodness for exploration then
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  15. #15
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NathanielB View Post
    I think he was just posting to see if there was anything we could spot out that could have been done better
    I know, I was replying to WildHoney, who said it would error, but it wouldn't.


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
  •