Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HTML form and PHP V1

 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
sorry it seems very difficult for me can you please help to write this code so that we could progress further ?


Thanks much..
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Stephan broke it down into small steps - which of these steps are you having problems with? What problems, specifically? What have you tried to overcome those problems, and what where the results?
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
i tried below but now getting this error :-

"Fatal error: Uncaught Error: Undefined constant '“’\n”' in F:\Java Training\SW\Netbeans\Xampp installation location\htdocs\v1\Booktest.php:9 Stack trace: #0 {main} thrown in F:\Java Training\SW\Netbeans\Xampp installation location\htdocs\v1\Booktest.php on line 9"

for Book.class.php



and i tested it  by executing code for Booktest.php file .code for this is as followings which i tried and got above mentioned error:-





Thanks....


 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You are using the wrong quotation marks for string literals. is not the same as ".

By the way, seeing as you're using double quotes instead of single quotes for string literals, you don't need to use string concatenation to get the title and author into the string. You can just insert the variables directly into the string literal.

Finally, you didn't follow my instructions properly. You passed a string to the Book constructor for the author. You should have passed an instance of Person. You also should have used __toString() instead of getInfo().
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
ok, after removing "\n" this error was resolved so please ignore my last post for the same.

So could experts please advise further how to progress further towards resolution of my main problem here...


Thanks much....
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You haven't completed the task I've given you.
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
ok,

how could i insert the variables directly into the string literal here?
could you please provide example on the same?

also how could i have passed the  instance of Person. why should i have used __toString() instead of getInfo()?

what went wrong if i completed it using getInfo() also how could i have used __tostring() here any example please?


Thanks..
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

John Ken wrote:how could i insert the variables directly into the string literal here?


You literally put them inside the string literal:

also how could i have passed the  instance of Person


Exactly the same way you pass an instance of string, except you use a Person instead of a string. I gave an example of this in the post where I gave you the task.

why should i have used __toString() instead of getInfo()?


Because __toString() will automatically convert an object to a string when you use it inside string literals.

You would have known all this if you had read and understood the following two articles:

https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double
https://www.php.net/manual/en/language.oop5.magic.php#object.tostring
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
after following instructions i got below error:-


David

Fatal error: Uncaught Error: Using $this when not in object context in F:\Java Training\SW\Netbeans\Xampp installation location\htdocs\v1\Booktest.php:7 Stack trace: #0 {main} thrown in F:\Java Training\SW\Netbeans\Xampp installation location\htdocs\v1\Booktest.php on line 7

code for Person.class.php:-




and Code for Book.class.php



code for Booktest.php




Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Well, what object is $this on line 7 of Booktest.php referring to?
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
instead of passing a string to the Book constructor for the author. I have passed an instance of Person as per advice.

Thanks...
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What person is $this referring to then?
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
...sorry i am not sure but you only advised to pass instance of person.

Thanks..
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Do you know how to create an instance of an object? Do you know how to assign such an instance to a variable? Do you know how to pass the values of variables to method calls?
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I hope below code covers all the expectation,If it does not cover then please advise how to correct it to get desired output,I have been trying my best to speed up and to resolve my problem the soonest now.

Book.class.php




Code for Book.php





Thanks ..

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You passed a string to the book constructor. I told you to pass an instance of Person.

Create an instance of Person.

Assign it to a variable.

Pass the variable to the constructor when you create an instance of Book.
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Do you want me  to create a child class for the class Person or what, we can't pass an instance of Person without extending or creating a child class from Parent class.



Thanks..
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

John Ken wrote:Do you want me  to create a child class for the class Person or what, we can't pass an instance of Person without extending or creating a child class from Parent class.


No. I want you to create an instance of class Person.

Have you done any kind of object oriented programming before, in any language?
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
ok ,i tried this



but when i tried to execute this home.php file i got an error "Parse error: syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in F:\Java Training\SW\Netbeans\Xampp installation location\htdocs\v1\home.php on line 14"
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You need to pay attention to every single character. A ( is not the same as a {.

Line 27 looks suspect as well, what with the opening " without a matching closing ".

As to line 36, is "-->" a valid PHP operator?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Even without the syntax errors, why did you extend the Book class? And what is Parent1 supposed to represent? And what does it have to do with the instructions I gave you?
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Stephan van Hulst wrote:You passed a string to the book constructor. I told you to pass an instance of Person.

Create an instance of Person.

Assign it to a variable.

Pass the variable to the constructor when you create an instance of Book.



when end result is being achieved then unnecessarily why do you want to complicate it instead of this we should focus on resolving main problem ?

 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The main problem is that you do not know PHP. That's what we're trying to teach you.
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
that i know but just to learn i can't lose my time to resolve this problem right ,there are plenty of things to learn in this world and if one keeps on learning all of them then whole life will be spent but one will not be able to learn all of them right?
so we should be focused just to resolve our main problem within limited time period right?

so request experts to please be focused on resolution of our main problem so that we could resolve it now as soon as possible.



Thanks much..
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Sure, there are many things to learn. We're not forcing you to learn PHP. You can pay us and we'll solve your problem for you.
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
sound like a paid forum.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This forum teaches people programming for free. It doesn't do their work for them for free.

If you don't like it, you can go to a forum where people will work for you for free.
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Do you want me to define 3 variables and then want a child class
that  inherit from it and the child class must provide a third variable and altogether echo themlike this  I need to understand what you are trying to achieve by ignoring my last code which does desired functionality?


Thanks...

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I want you to create an instance of Person by calling its constructor with a name. Assign it to a variable named $author.

I want you to create an instance of Book by calling its constructor with a title and the value of the $author variable. Assign it to a variable named $book.

Finally, print the book. All you need to do there is call echo $book.
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
but it is so tough so unable to do it so need experts guidance to achieve it.
So experts could you please assist to achieve these goals soon?


Thanks much..
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You could start by answering my questions:

Stephan van Hulst wrote:Do you know how to create an instance of a class? Do you know how to assign such an instance to a variable? Do you know how to pass the values of variables to method calls?

 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
probably were below is the solution for our problem I mean in view of calling constructor ,creating instances etc.?

class Person{
     public $author;
     public function __construct($author){
            $this->author   = $author;
     }
}

class Book extends Person{
     public  $book;
     public   function __construct($book){
            parent::__construct("X");
            $this->book = $book;
    }
    public function getAuthorBook()  {
          return "Author name is {$this->author} Book is {$this->book}" ;
    }
}

$bar = new Book ("XYZ") ;
echo $bar->getAuthorBook() ;




Thanks..

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please just answer my questions.

Do you know how to create an instance of a class? If you do, explain to me what you think it means.

Do you know how to assign an instance of a created class to a variable?

Do you know how to pass the value of a variable to a method call?
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
i think this link has answers of all of your questions:-

https://www.w3schools.com/php/php_oop_classes_objects.asp
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
No, it does not. The question was whether you know all of this, not whether you know where to look it up. I suspect that the answer to the former question is "no", and you'll probably make no progress in learning you PHP unless, and until, you do. So: please answer the questions.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I'm not asking these question because *I* need to learn. I know how to do this stuff. I'm asking you because I want you to explain your understanding of these concepts in your own words. The reason I'm asking is because the assignment I've given you is not a difficult one, but you keep writing code that does something completely different from what I expect you to write, so maybe you don't understand what I'm asking from you.

So once again, answer my questions, in your OWN words.
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
ok, answer of 2nd question i can't answer clearly but for 1st and 3rd please find it as followings:-

1.Do you know how to create an instance of a class? If you do, explain to me what you think it means.

A class is a template for objects, and an object is an instance of class.







2.Do you know how to assign an instance of a created class to a variable?




Not exactly.





3.Do you know how to pass the value of a variable to a method call?

yes like below:-

 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
any updates by experts on the same please?

Thanks much..
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

John Ken wrote:A class is a template for objects, and an object is an instance of class.


Good. So what keyword do you use to create an object?

John Ken wrote:

Stephan van Hulst wrote:2.Do you know how to assign an instance of a created class to a variable?


Not exactly.


You assign the value of an expression to a variable using the '=' operator. In your example code on line 16, you created an instance of type Fruit and you assigned it to the variable $apple.

John Ken wrote:

Stephan van Hulst wrote:3.Do you know how to pass the value of a variable to a method call?


yes like below:-


Except you didn't. Nowhere in that code do I see you passing a variable to a method call. You pass string literals on lines 23 and 24, but you haven't passed a value that was first assigned to a variable. I mean something like this:

However, there are two things I want you to keep in mind going forward:

  • Don't use underscores in method names. Write setColor, not set_color.
  • Avoid setters. There are times where setters are appropriate, but most of the time you want to set the fields of an object in the constructor, and then never change them again.
  •  
    John Ken
    Ranch Hand
    Posts: 388
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Q:-what keyword do you use to create an object?
    A:-To create a new instance of an object, we use the "new" keyword. This keyword creates a new instance of an object, which we can then assign to a variable, or invoke methods.

    But my main problem still remain unresolved yet?

    How can i write code to solve my main problem?


     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
      Bookmark Topic Watch Topic
    • New Topic