tags:

views:

77

answers:

1

Hey all,

I'm looking to create a nice way to separate view and controller in php.

What I'd like to happen is people would go to: localhost/index.php?view=signup and they would see the whole page with a signup box.. and NOT a login box.

I'm wondering if anyone has made this before.. I'm specifically looking for a elegant way of letting Javascript handle the hides and shows after php initially loads the page. So maybe something like this:

<div class="login" style="display:<?php echo $login; ?>;">

The trick is that I want the default (localhost/index.php) to display everything. So if ?view=.. is not defined then show it all.. I really don't want to clutter my view with a ton of if statements with isset()..

Let me know your solutions to this problem!

Thanks, Matt Mueller

+1  A: 

Simply do something like this: (nothing to do with CSS)

<?php

// login.php

if(strtolower($_GET['v']) == 'signup'){

?>
<form>/* sign up form goes here */</form>
<?php

}else{

?>
<form>/* login form goes here */</form>
<?php


}

?>

Or you could have used the switch/select if you have too many pages in one php file:

<?php

// login.php

switch(strtolower($_GET['v'])){

case 'signup':
?>
<form>/* sign up form goes here */</form>
<?php    
break;

default:    
?>
<form>/* login form goes here */</form>
<?php
break;

}

?>

Cheerio!

UPDATE: with regards to ability to use AJAX, you can do something like this:

<?php

// login.php

$_showcontent = false;
if(isset($_GET['c'])){$_showcontent = true;}

if(!$_showcontent){
  // load the header, javascript components and whatever so on
  echo '<div id="view">';
}

switch(strtolower($_GET['v'])){

case 'signupstep1':
?>
<a href="#view" onclick="return go_step2();">Step 2</a>
<?php    
break;

case 'signupstep2':
?>
<form>/* sign up form goes here */</form>
<?php    
break;

case 'signupcomplete':
?>
  /* sign up complete page */
<?php    
break;

default:    
?>
<a href="#view" onclick="return go_signup();"></a>
<?php
break;

}

if(!$_showcontent){
  echo '</div>';    
  // footer and what other stuff you need here
}

?>

The Javascript code:

<script type="text/javascript">
/* <![CDATA[ */

function go_signup(){
  $("#view").load("<?php echo $_SERVER['PHP_SELF'] ?>?c&v=signupstep1");
  return false;
}

function go_step2(){
  $("#view").load("<?php echo $_SERVER['PHP_SELF'] ?>?c&v=signupstep2");
  return false;
}

/* ]]> */</script>
thephpdeveloper
Thanks for your time.. the only issue is that I need it to be able to react to javascript afterwards. If php doesn't load elements, then javascript cant react when I click the login/signup buttons. Right now, if you hit "signup" after the page loaded ?view=login, then it will fade the login box out, and fade in the signup box.
Matt
I still don't get what you're trying to accomplish ..
yoda
He still wants to have all of the elements loaded onto the page, but only the selected one showing when the page loads. He wants to be able to hide/show the elements after the pageload via JS. Eitheryou can set the display CSS like you were mentioning in your question, or you could also let AJAX handle the switching of views and have the controller spit out the appropriate HTML based on the GET input from the AJAX.
BraedenP
Thanks, how would I do an ajax call when the page is first loaded? Wouldn't that be a wasteful since I could have just loaded the page in the first place with PHP?
Matt
You would first load the page with PHP and load consecutive views with AJAX
andho
Well let me update the post to see how we can integrate with AJAX (jQuery)
thephpdeveloper
Awesome! Thanks man!
Matt
no problem at all!
thephpdeveloper