PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,109 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,043 people online right now. Registration is fast and FREE... Join Now!




isn't coming out correctly

 

isn't coming out correctly, something in the scripting

Mariko1222

4 Nov, 2009 - 07:04 PM
Post #1

New D.I.C Head
*

Joined: 19 Jun, 2009
Posts: 41

CODE
<html>
<head>
<title>CREATING ARRAY AND FINDING OUT THE SIZE</title>
</head>
<body>
<ul>
<?php
$color = array("red", "orange", "yellow", "green", "blue", "indigo", "violet");

foreach($color as $value){
    echo "My color is = $value?"
};
function count($color){
    $size = 7;
    echo "Total number of elements is = $size"
}    
?>
</ul>
</body>
</html>


This is what I've got. I think something is messed up around line function count.
I am to find out the size of your array by using the count() function. Declare a $size variable and assign it to the count () function. Within the count() function brackets, add the color array.
Add the echo() function and print to screen the following message, "Total number of elements is $size."


It should print out on my browser a bulleted list of :
My color is = Red
for each of the colors in the array, and lastly a bulleted line
Total number of elements is = 7.

Alas, it does not do this.
Can anyone help me fix this error?

Thanks,
Mary

User is offlineProfile CardPM
+Quote Post


no2pencil

RE: Isn't Coming Out Correctly

4 Nov, 2009 - 07:11 PM
Post #2

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,492



Thanked: 303 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
Look at how you are evaluating the size value:
QUOTE
CODE

   $size = 7;
    echo "Total number of elements is = $size"


I think you want somethign more like :

CODE

       $size = 7;
    echo "Total number of elements is = ".$size;


But it will always be 7

Uhm.... also I don't see where you call your count function. Ever.
User is online!Profile CardPM
+Quote Post

Dannyboy997

RE: Isn't Coming Out Correctly

4 Nov, 2009 - 07:16 PM
Post #3

D.I.C Head
**

Joined: 17 Apr, 2009
Posts: 77



Thanked: 2 times
My Contributions
QUOTE
CODE
foreach($color as $value){
    echo "My color is = $value";
};


you should put it like this for a bullet list:
CODE


foreach($color as $value){
    echo "<li>My color is = $value</li>";
};



and you put a an extra '?' that shouldn't be there!

hope this helps

This post has been edited by Dannyboy997: 4 Nov, 2009 - 07:18 PM
User is offlineProfile CardPM
+Quote Post

RPGonzo

RE: Isn't Coming Out Correctly

5 Nov, 2009 - 10:20 AM
Post #4

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
this works .. and its basically all your code ... BUT you were missing some closing semi colons and you CANNOT redeclare a base php function like count ... just use it ...

CODE

<html>
<head>
<title>CREATING ARRAY AND FINDING OUT THE SIZE</title>
</head>
<body>
    <ul> <!-- start main ul -->
    <?php
        $color = array("red", "orange", "yellow", "green", "blue", "indigo", "violet");
        foreach($color as $value){
            echo "<li>My color is = $value</li>";
        }  
    ?>
    <ul> <!-- start nested ul -->
        <?php echo "<li><b>Total Elements In Array = " . count($color) . "</li>"; ?>
    </ul> <!-- end nested ul -->
    
    </ul> <!-- end main ul -->
</body>
</html>


and if your bored like i was take a peak at this ...
CODE

<?php
    error_reporting(E_ALL);
    session_start();
    // define out base array
    $color = isset($_SESSION['color_array']) ? $_SESSION['color_array'] : array("red", "orange", "yellow", "green", "blue", "indigo", "violet");
    // if the user submitted something we deal with it
    if (isset($_POST['addcolor']) && $_POST['addcolor'] != "") {
        // add user color
        $color[] = $_POST['addcolor'];
        // make sure there are no duplicates
        $color = array_unique($color);
    // user wants to remove color
    } else if (isset($_GET['removecolor']) && isset($_GET['key'])) {
        if (isset($color[$_GET['key']])) {
            unset($color[$_GET['key']]);
        }
    } else if (isset($_GET['resetarray'])) {
        if ($_GET['resetarray'] == "true") {
            $_SESSION['color_array'] = $color = array("red", "orange", "yellow", "green", "blue", "indigo", "violet");
        }
    }
    // define the session variable
    $_SESSION['color_array'] = $color;
?>
<html>
<head>
    <title>CREATING ARRAY AND FINDING OUT THE SIZE</title>
    <style type="text/css">
        a, a:visited { color: #EE4000; font-size: 10px; }
        a:hover, a:visited:hover { color: #8B2500; }
        #resetlink, #resetlink:visited { color: #008080; font-size: 14px; }
        #resetlink:hover, #resetlink:visited:hover { color: #2F4F4F; font-size: 14px; }
    </style>
</head>
<body>
    <ul> <!-- start main ul -->
    <?php
        foreach($color as $key => $value){
            echo "<li>My color is = $value &nbsp; <a href='?removecolor=$value&amp;key=$key'>^remove this color?^</a></li>";
        }  
    ?>
    <ul> <!-- start nested ul -->
        <?php echo "<li><b style='color:#FF0000'>Total Elements In Array = " . count($color) . "</b></li>"; ?>
            <li>
                <b style='color:#488214'>Would you like to add a color? What color = </b>
                <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="display:inline">
                    <input type="text" value="" name="addcolor">
                    <input type="submit" value="Add Color"/>
                </form>
            </li>
            <li>
                <a id="resetlink" href="?resetarray=true">Reset array to default?</a>
            </li>
    </ul> <!-- end nested ul -->
    
    </ul> <!-- end main ul -->
</body>
</html>


This post has been edited by RPGonzo: 5 Nov, 2009 - 11:01 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 12:46PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month