0

Part of my Computing course requires me to build an entire PHP system. I have chosen to create one for a fictitious stock broking company which wants its clients to be able to view their assets (shares and bonds) via a web system.

For the menu, my first idea would have been to use some simple "hrefs" as the menu will be fixed to display page links like "View Shares; View Bonds; My Account, etc". However, my teacher is insistent that "hrefs" must NOT be used and the only possible way of making this work would be to use a switch statement (or as he calls it: case statement). Is this true? It should bear noting that my teacher has virtually no experience with PHP and we seem to be clashing on everything he says (He's been going on about PHP being event-driven and involving system loops, etc. I always though this was untrue...).

So, does anybody have any suggestions? Should I stick with "hrefs" against his will, or use an alternative method?

Edward
  • 95
  • 1
  • 1
  • 9

3 Answers3

2

I'm hoping there's just a communication failure between the two of you... href is an attribute of < a > tags. These are HTML elements and have absolutely nothing to do with PHP. It is simply the accepted method of "linking" to pages. If you're not allowed to use hrefs then in order to navigate between pages you'll either need AJAX or form POSTs. Neither of which make sense for a menu. There is no way in PHP to generate the functionality of an href, as PHP is a server side scripting language and hrefs are for client side navigation.

Kyle Buser
  • 363
  • 2
  • 8
  • Absolutely! This is what I've been saying to him all along. We've been having this same argument for 5 weeks now. His reasoning is that if I use HTML, it becomes an HTML project and not a programming project. I suspect this is because he has this idea that buttons are somehow built into the PHP code and it being event-driven. I always just thought that the interpreter ran through the code and then sent the necessary HTMl to the client. Is this correct? – Edward Dec 10 '12 at 21:29
  • 1
    I'm not certain the parameters of the project, but if you're supposed to design a website as part of the project, there is absolutely no way to do this strictly using PHP. HTML is the language browsers use to interpret and display information. You can use PHP to output HTML, such as: Home Option 3"; ?> Or something like that, but you're still using HTML to communicate with the browser. – Kyle Buser Dec 10 '12 at 22:01
1

I would say your professor is wrong and right. I am actually working with the college of engineering web dev team and we do everything in PHP. I don't think he is really correct though because from what it sounds like, hrefs are more than satisfactory for what you are doing. We primarily use switch case statements to pass POST statements via AJAX to themselves and then handle them through independent cases. An example of this would be like

if (isset($_POST['whatever'])) {
switch($_POST['whatever']) {
case "1":
//I can now do something if the POST variable passed as "whatever" was deemed '1"
break;
case "2":
//I can do something if the POST variable pass as "whatever" was deemed "2"
break;

etc etc...

So two main advantages of this is one it makes it really simple because you don't have to juggle millions of href sites. I'm sure for your application it might not be a big deal but when you really start doing large projects where each website is fairly similar to one another, it is nice to know you can count on php's dynamic framework. Another advantage is that people don't see your code as much ^_^ If I looked in the source and saw a ton of href links, I might be able to know a little more about your file tree structure and that's never good (there are ways to workaround this with redirects and using PHP/javascript but yeah...)

OVERALL though he IS your professor but just to let you know from what it sounds like you are trying to achieve, hrefs are totally fine. I know a lot of professional sites like Yahoo or even Stackoverflow use hrefs to link their sites (just look to the left at the Related Questions. they are all linked using hrefs).

You said the computer course wants you to create an entire PHP web system and just to let you know generally computer courses are for education rather than actual practicality. I think you are doing the right thing by asking questions, but for the sake of the class, your professor is probably encouraging you to learn PHP despite it being not the best way (this is always subjective though). Perhaps you can find a way to find a middle ground where you can develop in PHP but still use hrefs? Hope this helps.

aug
  • 11,138
  • 9
  • 72
  • 93
  • Thanks for your reply! I was actually the one to choose PHP. My teacher's never used it before. – Edward Dec 10 '12 at 21:25
  • 1
    oh sorry I misunderstood. In that respect, I feel your instructor is very wrong to exclude the use of hrefs... – aug Dec 10 '12 at 22:32
-1

Your question is not very clear, but for me. All I have to do, to use swich for menu is this way.

if(isset($_GET['id']) && !empty($_GET['id'])) {                     

    $id = htmlentities($_GET['id']);                    
    $id = mysql_real_escape_string($_GET['id']);


    switch($id) {


        case "about";       // About us
        echo $about;
        break;
case "timeline";        // About us
echo include('timeline.php');
break;
            case "participate";     // Rules
            echo  $participate;
            break;
case "reg_success";     // Register Success     
echo $reg_success;
break;

    default:    // Contribution
    echo err('Page not Found!');
    break; 
  • 2
    That is the worst possible way to build a website. – user229044 Dec 10 '12 at 20:55
  • 2
    lies, there are much worse :-) –  Dec 10 '12 at 20:58
  • it is easy to say what you said, but here we prefer examples, not plain opinions @meagar –  Dec 10 '12 at 21:00
  • What is the standard solution for menu options? – Edward Dec 10 '12 at 21:01
  • 2
    Links. The standard solution for linking from one page to another page is to use *links*. It doesn't matter how the links are grouped and displayed, you still use links. – user229044 Dec 10 '12 at 21:04
  • 1
    @meagar Links, as in "hrefs"? – Edward Dec 10 '12 at 21:15
  • @meager, That does not defend you exaggerated views –  Dec 10 '12 at 21:22
  • @Edward No, links as in *links*. I think we're talking about the same thing, but they're calling *links*, you know, the tag that goes `Google!`. That's a *link*, not an "href". – user229044 Dec 11 '12 at 02:08
  • @meagar I was using "hrefs" as an abbreviation. – Edward Dec 11 '12 at 11:02
  • @meagar I still can't understand how this is the 'worst possible way' to create a website. Can you elaborate a little bit, so I can take lessons from my mistake? –  Dec 11 '12 at 17:22
  • There is absolutely no benefit to lumping your entire application into a case statement. You're imposting a massive, ugly bloated construct on yourself when you could simply have had a sane one-page-per-file approach for such a simple site. If your site is so complex that you *need* some kind of up-front routing, there are massively better ways to achieve this than a huge bloated switch statement. – user229044 Dec 11 '12 at 18:10
  • You do realize that when you create dynamic websites which generate urls on the fly, there is not way to create a "one-page-per-file" system. What if a user registered and you need to say "thank you" thereafter? Are you going to create a thankyou.php just for that, or take the user to the same page but, with dymanic url like `register.php?action=thankyou` then get the `action` and respond respectively? I think, your reasoning on this one is of site. –  Dec 11 '12 at 18:17