Welcome to Gaia! ::

My Design :: Web and Graphic Design [HTML CSS PHP]

Back to Guilds

XHTML, CSS, jQuery, Javascript, PHP, mySQL, MORE! 

Tags: design, graphics, html, jquery, javascript 

Reply Server Side Programming
My First attempt... not going so well.

Quick Reply

Enter both words below, separated by a space:

Can't read the text? Click here

Submit

z!p

PostPosted: Fri Oct 06, 2006 4:47 am



;
;
;
;
;
;
;
;
;
;
;
;
;
$OMFG = 54800;
$PrismButterflyMantilla = 17050;
$G9Laptop = 13495;
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";

ItemOMFGPrism Butterfly MantillaG9 LaptopG-BotAngelbowAncient KatanaORLY? HatGwee the Dragon
Price${$OMFG}${$PrismButterflyMantilla}$G9Laptop$$$$

?>


Ok this is my first attempt of PHP. At then end I want it to add up all the numbers. I know that you have to do $.. . + $... etc. But what I have so far is not showing up. See here.

Thanks for the help.
PostPosted: Fri Oct 06, 2006 9:08 pm
















$OMFG = 54800;
$PrismButterflyMantilla = 17050;
$G9Laptop = 13495;
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "
ItemOMFGPrism Butterfly MantillaG9 LaptopG-BotAngelbowAncient KatanaORLY? HatGwee the Dragon
Price" . $OMFG . "" . $PrismButterflyMantilla . "" . $G9Laptop . "$$$$
";
?>



Ok, first point, you only need semi-colons inside ther tags, not outside.
Then, for everything inside the php tags that you want to be output, you must use echo.
and lastly, when outputting variables, you should seoerate them from the string like so:
echo "Hello " . $name . ", how are you?";

Hoggs
Vice Captain


z!p

PostPosted: Sat Oct 07, 2006 9:11 am


";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "
Thanks.
If you do $name = something in one PHP tag will another tag read the $name or do you have to redo the $name = something?

Now it is not adding correctly. It just shows 34534 +345 +34534 +3453 etc
$OMFG = 54800;
$PrismButterflyMantilla = 17050;
$G9Laptop = 13495;
$Gbot = "Unknown";
$Angelbow = 14000;
$AncientKatana = 23000;
$ORLYHat = 7700;
$Gweethedragon = 8950;
$totalitem = "$OMFG + $PrismButterflyMantilla + $G9Laptop + $Angelbow + $AncientKatana + $ORLYHat + $Gweethedragon";
$pure = 16000;
$total = "$totalitem + $pure";
echo "
" . $OMFG . "" . $PrismButterflyMantilla . " . $G9Laptop . " . $Gbot . "" . $Angelbow . "" . $AncientKatana . "" . $ORLYHat . "" . $Gweethedragon . "
";
echo "
I have $totalitem gold worth in items
";
echo "I have $pure gold
";
echo "I am worth " . $total . " gold
";
?>
PostPosted: Sat Oct 07, 2006 4:42 pm


echo '
' . $OMFG + $PrismButterflyMantilla + G9Laptop + $Gbot +Angelbow + $AncientKatana + $ORLYHat + $Gweethedragon . " gold
";


You said the div wasn't being displayed...

You shouldn't mix '.' and '+'. These two operators function on two completely different levels, String vs. Integer. If you are going to mix them make sure to use parentheses to organize the order of operations.

echo '
' . ( $OMFG + $PrismButterflyMantilla + G9Laptop + $Gbot +Angelbow + $AncientKatana + $ORLYHat + $Gweethedragon ) . " gold
";

MiniLain


MiniLain

PostPosted: Sat Oct 07, 2006 4:55 pm


Advanced Example
This example is a bet more the technical side, but it can help when learning a language to see more advanced solutions compared to simple solutions.

And while your version will work this is a version that uses control structure to reduce upkeep. With your version you would have to add to several sections of the code, this one all you have to do is add a line to the array at the top and the rest of the code will adapt to the change.

$items = array(
'OMFG' => 54800,
'Prism Butterfly Mantilla' => 17050,
'G9 Laptop' => 13495,
'gBot' => 0,
'Angel Bow' => 14000,
'Ancient Katana' => 23000,
'ORLY Hat' => 7700,
'Gwee The Dragon' => 8950,
'Gold on Hand' => 16000
)
$lineClass = 'even';
$addValue = 0;

echo '' . "\n";
foreach ($items as $name => $value)
{
$lineClass = ( $lineClass == 'even' ) ? 'odd' : 'even';
echo '';
echo '';
echo '' . "\n";
$addValue = $addValue + $value;
}
echo '' . "\n";
echo '
' . $name . '' . ( $value == 0 ? 'Unknown' : $value ) . '
' . $addValue . ' gold
';
?>


View it live: http://jukidon.no-ip.com/test.php
PostPosted: Sat Oct 07, 2006 5:40 pm


z!p
Thanks.
If you do $name = something in one PHP tag will another tag read the $name or do you have to redo the $name = something?


$name = 'hello';
?>



echo $name;
?>

//Output
hello


Most variables when called are global to all code on one page and any linked includes. There are exceptions and when you start learning functions and classes they will get extremely confusing.

MiniLain


z!p

PostPosted: Sat Oct 07, 2006 5:59 pm


Wow, thank you! I edited it alot and never changed the post, that is why the div showed up. That array thing is so much easier. I just started the other day, so I just coded what I knew would work. Man, this PHP is confusing.
PostPosted: Sat Oct 07, 2006 6:49 pm


*giggles* The PHP I wrote isn't all that important. Its just an example of how what you wrote would look like will all the bells and whistles. Its always important to code what you are familiar with. If you want to learn you have to experiment with the code yourself. The functions I used are quite common and a great starting place.

I don't know if you've looked at it yet, but the php manual is one of the be resources for the language reference.

Language Reference User Image - Blocked by "Display Image" Settings. Click to show.
Arrays User Image - Blocked by "Display Image" Settings. Click to show.
Foreach User Image - Blocked by "Display Image" Settings. Click to show.
Ternary Operator User Image - Blocked by "Display Image" Settings. Click to show.

MiniLain


Hoggs
Vice Captain

PostPosted: Sun Oct 08, 2006 1:19 am


You might want to note that gaia uses stripslashes()

All your "\n"s are slashless. xd
PostPosted: Sun Oct 08, 2006 8:20 am


Hoggs
You might want to note that gaia uses stripslashes()

All your "n"s are slashless. xd


I noticed that last night, but didn't think there was anything I could do about it. The character break seems to haved work though. What a messed up system. xp

MiniLain


z!p

PostPosted: Sun Oct 08, 2006 8:24 am


Right now I am reading a PHP book. I understand up to lesson 3. sweatdrop

I do not even know what you are talking about with the /n thingy. Could you explain?
PostPosted: Sun Oct 08, 2006 8:35 pm


z!p
Right now I am reading a PHP book. I understand up to lesson 3. sweatdrop

I do not even know what you are talking about with the /n thingy. Could you explain?

They put a line break in the code, the "new line" character.

If you were to type this in php:
echo "1
";
echo "2
";
echo "3
";
echo "4
";
?>


Then it would output:

1
2
3
4


But if you were to place \n after each line, you would get this:

1

2

3

4

Hoggs
Vice Captain


z!p

PostPosted: Sun Oct 08, 2006 8:58 pm


Hoggs
z!p
Right now I am reading a PHP book. I understand up to lesson 3. sweatdrop

I do not even know what you are talking about with the /n thingy. Could you explain?

They put a line break in the code, the "new line" character.

If you were to type this in php:
echo "1
";
echo "2
";
echo "3
";
echo "4
";
?>


Then it would output:

1
2
3
4


But if you were to place n after each line, you would get this:

1

2

3

4
Oh, thanks for clearing that up.
Reply
Server Side Programming

 
Manage Your Items
Other Stuff
Get GCash
Offers
Get Items
More Items
Where Everyone Hangs Out
Other Community Areas
Virtual Spaces
Fun Stuff
Gaia's Games
Mini-Games
Play with GCash
Play with Platinum