Jump to content

Understanding PHP actions

Sergio45
Go to solution Solved by LyyK,
3 hours ago, Sergio45 said:

So does the case statements have an order that there executed or are they executed the moment that there is a match of the case. I get that 


 case "firstPage":
        include_once("Views/firstPage.php");
        break;

Makes it show the firstPage.php when the action which is the name of the variable and firstPage which  is what is assigned to that variable (action).

In the order of precedence. The block of the first matching case is run and no other case of the switch should be evaluated afterward if there's a break in the block.

 

Quote

Short version:I don't see the action firstPage being declared anywhere other than here:


$_REQUEST['action'] = (isset($_REQUEST['action']) ?$_REQUEST['action'] : "firstPage");

Which is in the Index.php: I know that Index is the first thing that is loaded.

So does the above code start off assigned with the string value of "firstPage" the moment it is loaded....Right? 

As long as $_REQUEST['action'] has not been set — which is, in your project's case (no pun intended), always — it will have "firstPage" assigned to it. The reason the "firstPage" case didn't run before was simply because of the lowercase 'p' :)

Quote

So that is why we don't have to declare the action "firstPage" anywhere in the firstPage.php file because it is declared right of the bat and matches the case statement and loads the firstPage.php...Right?

 

Correct. Have you learned about compile vs runtime yet? At runtime, the code in firstPage.php is effectively considered as code within whatever file it was included by. I think what the assignment is trying to show with the header and footer is that instead of having to have the code from the header/footer files in each file that require it, you can simply include it instead. This makes for editing of code that is used by multiple files quicker and much more efficient.

 

If you ever have any questions about coding related topics, you're very welcome to message me and I'll link you my discord or Steam etc. I'm by no means the best web developer out there but I'm always happy to help with this sort of stuff :)

Hey guys,

 

I am trying to get my head around the Model View Controller structure for PHP. 

I keep hitting a wall when it comes to the cases and actions part of the php. 

So I have a  index file with the following code:

<?php
include("Models/utilities.php");
include("Models/connectSalesRepDataBase.php");

$action = (isset($_REQUEST['action'])) ? $_REQUEST['action']:"firstPage";

switch (true)
{
    case strpos($action,"SalesRepCases")===0:
        include("Models/salesRepCases.php");
        break;
        
        default:
            echo "Unexpected action $action found.<br>";
            break;
}




?> 

and than I have a Case file name SalesRepCases.php with the following code:

<?php

switch($action)
{
    case "firstPage":
        include ("Views/firstPage.php");
        break;
        case "firstPageResults":
            includ ("Views/salesRepInfo.php");
            break;
            default:
                echo "Action $action is not recognized";
                break;
      
        
}




?>

Now with  what was posted I keep thinking that the action on the index.php is firstPage. So in the cases file (salesRepCases.php) the case is "firstpage" and when that is the case it should show the file called "firstPage.php" . The code on the firstPage.php is the following:

<?php include ("Views/header.php");?>

<?php include("VIews/footer.php");?>

I got noting in there other than my footer and header, but when I the site and I get the following message , " Unexpected action firstPage found."

Shouldn't it be showing my FirstPage case file? 

 

Any help would be appreciated .

Link to comment
Share on other sites

Link to post
Share on other sites

Remove parentheses from the includes.

 

EDIT: A method you can use to find out if any file is actually found by your include statements:

var_dump(include_once 'file.php');

 

If the file doesn't exist, false is returned.

 

I don't really know what you are trying to do with the switch so I have nothing to really say about it other than that one of the cases contain a include statement with a typo in it. 

[ Intel i7-6700K ][ Asus TUF Sabertooth Mk1 Z170 ][ Gigabyte GTX 780ti ][ 32GB (2x16GB) 3200MHz G.Skill Trident Z ][ Corsair Obsidian 900D ]

[ EVGA SuperNOVA 1000 P2 80+ PLATINUM ][ 1x512GB Samsung 960 Pro M.2 (OS) ][ 3x512GB Samsung 850 Pro RAID 0 ][ 1x5TB WD Black ]

Link to comment
Share on other sites

Link to post
Share on other sites

I dont know what you are trying to achieve here but here is the logic:

I dont know what your client side is doing with 'action' but a conditional is returning false and setting $action = "firstpage"

8 minutes ago, Sergio45 said:

$action = (isset($_REQUEST['action'])) ? $_REQUEST['action']:"firstPage";

Then a conditional checks if "salesrepcases" is in position 0 of $action is obviously going to go to the default switch.

7 minutes ago, Sergio45 said:

case strpos($action,"SalesRepCases")===0:

include("Models/salesRepCases.php");

break;

 

default:

echo "Unexpected action $action found.<br>";

break;

why the string position function call?

what are you trying to do?

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, SCHISCHKA said:

why the string position function call?

what are you trying to do?

I am trying to follow an example that I am working with to understand the structure, but I see where You are coming from. 

SO let me re-phrase my question, How do I set the action on the firstpage.php file to match the case firstpage on the salesRepCase.php.

Link to comment
Share on other sites

Link to post
Share on other sites

27 minutes ago, Sergio45 said:

I am trying to follow an example that I am working with to understand the structure, but I see where You are coming from. 

SO let me re-phrase my question, How do I set the action on the firstpage.php file to match the case firstpage on the salesRepCase.php.

why does it have to be in a seperate php file?

just make a function and pass in the variable

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, SCHISCHKA said:

why does it have to be in a seperate php file?

just make a function and pass in the variable

I will do that thanks man. 

Link to comment
Share on other sites

Link to post
Share on other sites

34 minutes ago, Sergio45 said:

I am trying to follow an example that I am working with to understand the structure, but I see where You are coming from. 

SO let me re-phrase my question, How do I set the action on the firstpage.php file to match the case firstpage on the salesRepCase.php.

$action = (isset($_REQUEST['action'])) ? $_REQUEST['action']:"firstPage";

The above evaluates and assigns to the $action var either $_REQUEST['action'] (if set) or the string "firstPage". I can't tell for sure but you might have a scope issue, in which case I would suggest the following changes:

 

In index.php

$_REQUEST['action'] = (isset($_REQUEST['action']) ? $_REQUEST['action']:"firstPage");
case strpos($_REQUEST['action'],"SalesRepCases")===0:

in  SalesRepCases.php

switch($_REQUEST['action'])

 

I'm still not quite sure what you are trying to do and/or why but I suppose, if it's for an assignment of sort, I understand the obscurity.

[ Intel i7-6700K ][ Asus TUF Sabertooth Mk1 Z170 ][ Gigabyte GTX 780ti ][ 32GB (2x16GB) 3200MHz G.Skill Trident Z ][ Corsair Obsidian 900D ]

[ EVGA SuperNOVA 1000 P2 80+ PLATINUM ][ 1x512GB Samsung 960 Pro M.2 (OS) ][ 3x512GB Samsung 850 Pro RAID 0 ][ 1x5TB WD Black ]

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, LyyK said:

 

I'm still not quite sure what you are trying to do and/or why but I suppose, if it's for an assignment of sort, I understand the obscurity.

You're correct. I will give that a shot also. Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

So I got this so far for the index

<?php
include("Models/utilities.php");
include("Models/connectSalesRepDataBase.php");

$_REQUEST['action'] = (isset($_REQUEST['action']) ?$_REQUEST['action']:"firstpage");

switch($_REQUEST['action'])
{
    case strpos($action,"SalesRepCases")===0:
        include("Models/salesRepCases.php");
        break;
        
        default:
            echo "Unexpected action $action found.<br>";
            break;
}




?> 

and for the cases

<?php

switch($_REQUEST['action'])
{
    case "firstPage":
        include "Views/firstPage.php";
        break;
        case "firstPageResults":
            includ ("Views/salesRepInfo.php");
            break;
            default:
                echo "Action $action is not recognized";
                break;
      
        
}




?>

But I still get the unexpected action found error.'

I did change 

switch(true)

to 

switch($_REQUEST['action'])

on the cases file.

 

What would the be the action for the firstPage.php ?

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Sergio45 said:

So I got this so far for the index


<?php
include("Models/utilities.php");
include("Models/connectSalesRepDataBase.php");

$_REQUEST['action'] = (isset($_REQUEST['action']) ?$_REQUEST['action']:"firstpage");

switch($_REQUEST['action'])
{
    case strpos($action,"SalesRepCases")===0:
        include("Models/salesRepCases.php");
        break;
        
        default:
            echo "Unexpected action $action found.<br>";
            break;
}




?> 

and for the cases


<?php

switch($_REQUEST['action'])
{
    case "firstPage":
        include "Views/firstPage.php";
        break;
        case "firstPageResults":
            includ ("Views/salesRepInfo.php");
            break;
            default:
                echo "Action $action is not recognized";
                break;
      
        
}




?>

But I still get the unexpected action found error.'

I did change 

switch(true)

to 

switch($_REQUEST['action'])

on the cases file.

 

What would the be the action for the firstPage.php ?

 

 

 

I was sort of scratching my head about the "views" but I think I just realized what the point of your assignment is. Are you supposed to be using PHP namespaces? That would explain why I thought you possibly had scope issues.

[ Intel i7-6700K ][ Asus TUF Sabertooth Mk1 Z170 ][ Gigabyte GTX 780ti ][ 32GB (2x16GB) 3200MHz G.Skill Trident Z ][ Corsair Obsidian 900D ]

[ EVGA SuperNOVA 1000 P2 80+ PLATINUM ][ 1x512GB Samsung 960 Pro M.2 (OS) ][ 3x512GB Samsung 850 Pro RAID 0 ][ 1x5TB WD Black ]

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, LyyK said:

I was sort of scratching my head about the "views" but I think I just realized what the point of your assignment is. Are you supposed to be using PHP namespaces? That would explain why I thought you possibly had scope issues.

Yes I am. Sorry I should of said that before. 

So what I am going to do is upload the the finish example that I was using as a reference. When I was doing the assignment I was confused on what the actions and Cases were doing and how the firstpage was assigned a case and action. 

 

Week3.zip

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Sergio45 said:

Yes I am. Sorry I should of said that before. 

So what I am going to do is upload the the finish example that I was using as a reference. When I was doing the assignment I was confused on what the actions and Cases were doing and how the firstpage was assigned a case and action. 

 

Week3.zip

I'll have a look. Give me a few moments.

[ Intel i7-6700K ][ Asus TUF Sabertooth Mk1 Z170 ][ Gigabyte GTX 780ti ][ 32GB (2x16GB) 3200MHz G.Skill Trident Z ][ Corsair Obsidian 900D ]

[ EVGA SuperNOVA 1000 P2 80+ PLATINUM ][ 1x512GB Samsung 960 Pro M.2 (OS) ][ 3x512GB Samsung 850 Pro RAID 0 ][ 1x5TB WD Black ]

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, LyyK said:

I'll have a look. Give me a few moments.

No problem man, Take your time I just appreciate the help.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Sergio45 said:

No problem man, Take your time I just appreciate the help.

if( isset($_REQUEST['action']) ) switch( $_REQUEST['action'] )
{
    case "firstPage":
        include("Views/firstPage.php");
        break;
    case "firstPageResults":
         include("Views/salesRepInfo.php");
         break;
    default:
         echo "Action $action is not recognized";
         break;
}

For starters, try this and let me know if anything changes in terms of errors. Also, what IDE are you using?

[ Intel i7-6700K ][ Asus TUF Sabertooth Mk1 Z170 ][ Gigabyte GTX 780ti ][ 32GB (2x16GB) 3200MHz G.Skill Trident Z ][ Corsair Obsidian 900D ]

[ EVGA SuperNOVA 1000 P2 80+ PLATINUM ][ 1x512GB Samsung 960 Pro M.2 (OS) ][ 3x512GB Samsung 850 Pro RAID 0 ][ 1x5TB WD Black ]

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, LyyK said:

if( isset($_REQUEST['action']) ) switch( $_REQUEST['action'] )
{
    case "firstPage":
        include("Views/firstPage.php");
        break;
    case "firstPageResults":
         include("Views/salesRepInfo.php");
         break;
    default:
         echo "Action $action is not recognized";
         break;
}

For starters, try this and let me know if anything changes in terms of errors. Also, what IDE are you using?

I will give it a try. I am using Cloud9 to do this. I have a local copy that I can use on Brackets also.

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, LyyK said:

if( isset($_REQUEST['action']) ) switch( $_REQUEST['action'] )
{
    case "firstPage":
        include("Views/firstPage.php");
        break;
    case "firstPageResults":
         include("Views/salesRepInfo.php");
         break;
    default:
         echo "Action $action is not recognized";
         break;
}

For starters, try this and let me know if anything changes in terms of errors. Also, what IDE are you using?

I will give it a try. I am using Cloud9 to do this. I have a local copy that I can use on Brackets also.

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, LyyK said:

if( isset($_REQUEST['action']) ) switch( $_REQUEST['action'] )
{
    case "firstPage":
        include("Views/firstPage.php");
        break;
    case "firstPageResults":
         include("Views/salesRepInfo.php");
         break;
    default:
         echo "Action $action is not recognized";
         break;
}

For starters, try this and let me know if anything changes in terms of errors. Also, what IDE are you using?

It worked! :) Now Can You explain to me why it work, if you don't mind?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, LyyK said:

if( isset($_REQUEST['action']) ) switch( $_REQUEST['action'] )
{
    case "firstPage":
        include("Views/firstPage.php");
        break;
    case "firstPageResults":
         include("Views/salesRepInfo.php");
         break;
    default:
         echo "Action $action is not recognized";
         break;
}

For starters, try this and let me know if anything changes in terms of errors. Also, what IDE are you using?

 

1 minute ago, Sergio45 said:

It worked! :) Now Can You explain to me why it work, if you don't mind?

Never Mind. I spoke to soon, it gives me a blank page. With no error message. 

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Sergio45 said:

 

Never Mind. I spoke to soon, it gives me a blank page. With no error message. 

It only runs the switch if

isset($_REQUEST['action'])

is true. As for the blank page, I'll have a look.

 

1 minute ago, Sergio45 said:

This is what I am working on so far. 

week6 (8).zip

This should help. Un momento.

[ Intel i7-6700K ][ Asus TUF Sabertooth Mk1 Z170 ][ Gigabyte GTX 780ti ][ 32GB (2x16GB) 3200MHz G.Skill Trident Z ][ Corsair Obsidian 900D ]

[ EVGA SuperNOVA 1000 P2 80+ PLATINUM ][ 1x512GB Samsung 960 Pro M.2 (OS) ][ 3x512GB Samsung 850 Pro RAID 0 ][ 1x5TB WD Black ]

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, LyyK said:

It only runs the switch if


isset($_REQUEST['action'])

is true. As for the blank page, I'll have a look.

 

This should help. Un momento.

Here is the css file my bad.

restaurant.css

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, Sergio45 said:

Here is the css file my bad.

restaurant.css

NP. Try changing your header accordingly and let me know if the page is still blank

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

EDIT: scratch that, they are the same. I'm switching to my devbox real quick so I can actually compile the PHP. brb

[ Intel i7-6700K ][ Asus TUF Sabertooth Mk1 Z170 ][ Gigabyte GTX 780ti ][ 32GB (2x16GB) 3200MHz G.Skill Trident Z ][ Corsair Obsidian 900D ]

[ EVGA SuperNOVA 1000 P2 80+ PLATINUM ][ 1x512GB Samsung 960 Pro M.2 (OS) ][ 3x512GB Samsung 850 Pro RAID 0 ][ 1x5TB WD Black ]

Link to comment
Share on other sites

Link to post
Share on other sites

So this is what I did to get the firstpage to show up.

Index.php:

<?php
include("Models/utilities.php");
include("Models/connectSalesRepDataBase.php");

//if( isset($_REQUEST['action']) ) switch( $_REQUEST['action'])
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : "firstPage";
switch(true)
{
    case "firstPage":
        include("Views/firstPage.php");
        break;
    /*  
    case "firstPageResults":
         include("Views/salesRepInfo.php");
         break;
         */
    default:
         echo "Action $action is not recognized";
         break;
}



?> 

firstPage.php:

<?php include ("Views/header.php");?>




 <form id="frmWeight" method="post" action=".">
     	<input type="hidden" name="action" value=" ">
     <P>S</P>

          
        </form>



<?php include("Views/footer.php");?>

salesRepCases.php:

<?php

switch($_REQUEST['action'])
{
    case "firstPage":
        include ("Views/firstPage.php");
        break;
        case "firstPageResults":
            include ("Views/salesRepInfo.php");
            break;
            default:
                echo "Action $action is not recognized";
                break;
      
        
}




?>

Now For firstPage.php 

I included the following as I was following the week3.zip example

------------------------
 <form id="frmWeight" method="post" action=".">********THE ACTION IS A '.' NOT firstPage
         <input type="hidden" name="action" value=" ">**********THE VALUE IS BLANK! NOT firstPage
     <P>S</P>

          
        </form>

----------------------------------------------------------------

With that being said. How the heck is firstPage action being assigned to the firstpage if its not in any of the values and actions on the firstPage.php.?

 

Also 

5 minutes ago, LyyK said:

NP. Try changing your header accordingly and let me know if the page is still blank


<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

I did try that and no luck.

Link to comment
Share on other sites

Link to post
Share on other sites

I will be back at it again tomorrow. I have to get ready for bed for work. 

Thanks for the help.

LyyK

Appreciate it man.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Sergio45 said:

I will be back at it again tomorrow. I have to get ready for bed for work. 

Thanks for the help.

LyyK

Appreciate it man.

I will figure this out, I'm determined haha check back tomorrow and I'll have your answer

[ Intel i7-6700K ][ Asus TUF Sabertooth Mk1 Z170 ][ Gigabyte GTX 780ti ][ 32GB (2x16GB) 3200MHz G.Skill Trident Z ][ Corsair Obsidian 900D ]

[ EVGA SuperNOVA 1000 P2 80+ PLATINUM ][ 1x512GB Samsung 960 Pro M.2 (OS) ][ 3x512GB Samsung 850 Pro RAID 0 ][ 1x5TB WD Black ]

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×