[solved] Need help with Arrays Tutorial

  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

hi,
im learning php now, with this brilliant tut. I would like some help, so every problem i have i want it placed in this topic so it dont look messy (the board). So please once a new post is on here check up on it becase it may be me in need of help.

I am learning about arrays and made a perfect code:

PHP Code: [ Select ]
<h4>Example Thirteen</h4>
 
<?php
 
$cities = array (
 
    'france' => 'paris',
 
    'germany' => 'berlin',
 
    'uk' => 'london'
 
);
 
echo $cities['france'] . " " . $cities['germany'] . " " . $cities['uk'];
 
?>
  1. <h4>Example Thirteen</h4>
  2.  
  3. <?php
  4.  
  5. $cities = array (
  6.  
  7.     'france' => 'paris',
  8.  
  9.     'germany' => 'berlin',
  10.  
  11.     'uk' => 'london'
  12.  
  13. );
  14.  
  15. echo $cities['france'] . " " . $cities['germany'] . " " . $cities['uk'];
  16.  
  17. ?>


This of course was within my html tags.

But then i wanted to start experimenting and came up with this...

PHP Code: [ Select ]
<html>
 
<head>
 
<title>My First PHP page</title>
 
</head>
 
<body>
 
 
 
<H4>yum yum</H4>
 
 
 
<?php
 
$cities = array(
 
   'france' => 'paris',
 
   'germany' => 'berlin',
 
   'uk' => 'london',
 
$food = array (
 
   'indian' => 'somosa',
 
   'italian' => 'pizza',
 
   'chinese' => 'noodles',
 
);
 
echo $cities['france'] . " " . $cities['germany'] . " " . $cities['uk]  . "
 
 
 
" . $food['indian'];
 
?>
 
 
 
</body>
 
</html>
  1. <html>
  2.  
  3. <head>
  4.  
  5. <title>My First PHP page</title>
  6.  
  7. </head>
  8.  
  9. <body>
  10.  
  11.  
  12.  
  13. <H4>yum yum</H4>
  14.  
  15.  
  16.  
  17. <?php
  18.  
  19. $cities = array(
  20.  
  21.    'france' => 'paris',
  22.  
  23.    'germany' => 'berlin',
  24.  
  25.    'uk' => 'london',
  26.  
  27. $food = array (
  28.  
  29.    'indian' => 'somosa',
  30.  
  31.    'italian' => 'pizza',
  32.  
  33.    'chinese' => 'noodles',
  34.  
  35. );
  36.  
  37. echo $cities['france'] . " " . $cities['germany'] . " " . $cities['uk]  . "
  38.  
  39.  
  40.  
  41. " . $food['indian'];
  42.  
  43. ?>
  44.  
  45.  
  46.  
  47. </body>
  48.  
  49. </html>


Now i have checked through the code, and got to admit when i added a new array i thought it may be right.

Is what im doign right or wrong? If wrong then please explain why.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

PHP Code: [ Select ]
<?php
 
$cities = array(
 
   'france' => 'paris',
 
   'germany' => 'berlin',
 
   'uk' => 'london',
 
$food = array (
 
   'indian' => 'somosa',
 
   'italian' => 'pizza',
 
   'chinese' => 'noodles',
 
);
 
 
  1. <?php
  2.  
  3. $cities = array(
  4.  
  5.    'france' => 'paris',
  6.  
  7.    'germany' => 'berlin',
  8.  
  9.    'uk' => 'london',
  10.  
  11. $food = array (
  12.  
  13.    'indian' => 'somosa',
  14.  
  15.    'italian' => 'pizza',
  16.  
  17.    'chinese' => 'noodles',
  18.  
  19. );
  20.  
  21.  

goes to
PHP Code: [ Select ]
<?php
 
$cities = array(
 
   'france' => 'paris',
 
   'germany' => 'berlin',
 
   'uk' => 'london'
 
);
 
$food = array (
 
   'indian' => 'somosa',
 
   'italian' => 'pizza',
 
   'chinese' => 'noodles'
 
);
 
 
  1. <?php
  2.  
  3. $cities = array(
  4.  
  5.    'france' => 'paris',
  6.  
  7.    'germany' => 'berlin',
  8.  
  9.    'uk' => 'london'
  10.  
  11. );
  12.  
  13. $food = array (
  14.  
  15.    'indian' => 'somosa',
  16.  
  17.    'italian' => 'pizza',
  18.  
  19.    'chinese' => 'noodles'
  20.  
  21. );
  22.  
  23.  


problems, you had not closed the brackets. Also the last element in the array must not have a comma after, else it will expect another element.

Hope this helps
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

oh so, within each array there must be


PHP Code: [ Select ]
 
array (name) (
 
 
 
);
  1.  
  2. array (name) (
  3.  
  4.  
  5.  
  6. );


???
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

PHP Code: [ Select ]
 
<html>
 
<head>
 
<title>My First PHP page</title>
 
</head>
 
<body>
 
 
 
<H4>yum yum</H4>
 
 
 
<?php
 
$cities = array(
 
   'france' => 'paris',
 
   'germany' => 'berlin',
 
   'uk' => 'london'
 
);
 
 
 
$food = array(
 
   'indian' => 'somosa',
 
   'italian' => 'pizza',
 
   'chinese' => 'noodles'
 
);
 
 
 
echo $cities['france'] . " " . $cities['germany'] . " " . $cities['uk]  . " " . $food['indian'];
 
?>
 
 
 
</body>
 
</html>
 
 
  1.  
  2. <html>
  3.  
  4. <head>
  5.  
  6. <title>My First PHP page</title>
  7.  
  8. </head>
  9.  
  10. <body>
  11.  
  12.  
  13.  
  14. <H4>yum yum</H4>
  15.  
  16.  
  17.  
  18. <?php
  19.  
  20. $cities = array(
  21.  
  22.    'france' => 'paris',
  23.  
  24.    'germany' => 'berlin',
  25.  
  26.    'uk' => 'london'
  27.  
  28. );
  29.  
  30.  
  31.  
  32. $food = array(
  33.  
  34.    'indian' => 'somosa',
  35.  
  36.    'italian' => 'pizza',
  37.  
  38.    'chinese' => 'noodles'
  39.  
  40. );
  41.  
  42.  
  43.  
  44. echo $cities['france'] . " " . $cities['germany'] . " " . $cities['uk]  . " " . $food['indian'];
  45.  
  46. ?>
  47.  
  48.  
  49.  
  50. </body>
  51.  
  52. </html>
  53.  
  54.  


It still does not work.
I may have made a mistake again, i am learning in notepad and type it out so i get to grips with it. (better than copy n paste)
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

thats the one.

Each one is it's own thing separate thing - you have to finish off one before you start the next. Unless you are trying to create multidimensional arrays, but you don't wanna be worrying about those just yet :wink:

Another thing to bear in mind is that you should always have the same number of opening brackets as closing brackets. It's always a good thing to look out for - thats part of the reason i spotted this one so fast.
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

could you also reply to the post before yours please. And thank you for your advice.


i am currently using this tutorial... http://www.keithjbrown.co.uk/vworks/php/php_p1.php there are 6 sections.

It is very useful.

also, what brackets are you talking about?
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Sorry didn't see it there :oops: about the brackets I mean (, [ and { whenever they appear in code, they always need pairing up with a closing bracket of the correct flavour (I know they each have names but to me they are "normal", "square" and "curly"). If you have more closing brackets than opening brackets then you are going to get errors.

if you look at the code you have posted, that is colour coded, you might be able to see what is happening. It really helps a lot to use an editor that colours your code for you. I would stop using notepad and start using a "proper" editor. I use DW but someone posted one that is free a couple of weeks ago and colours the code for you. Not sure what it was called though.


If you can't see whats going wrong:
PHP Code: [ Select ]
 
<?php
 
echo $cities['france'] . " " . $cities['germany'] . " " . $cities['uk']  . " " . $food['indian'];
 
?>
 
 
  1.  
  2. <?php
  3.  
  4. echo $cities['france'] . " " . $cities['germany'] . " " . $cities['uk']  . " " . $food['indian'];
  5.  
  6. ?>
  7.  
  8.  


Uk was missing a single quote after it. It's such an easy mistake, I know I would spend twice as long debugging if it weren't for automagical colour coding - you will be amazed at how much it helps.
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

ohhh thats myfault too, i should have known that. i didnt see it.

i know

advanced html
advanced css
actionscript
and new to java...

i been using these langs for 5 years


i have never used php or mysql before.
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

PHP Code: [ Select ]
 
 
 
$food = array (
 
    'indian' => 'somosa',
 
    'chinese' => 'noodles'
 
 
  1.  
  2.  
  3.  
  4. $food = array (
  5.  
  6.     'indian' => 'somosa',
  7.  
  8.     'chinese' => 'noodles'
  9.  
  10.  


i forgot to add a , after 'somosa' :P
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

http://www.dhost.info/gmforum/php/mypage.php :D:D:D
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Image
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

im learning about variables now, so get ready for some help on that... if i need it

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 107 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.