php4ever Posted June 6, 2007 Share Posted June 6, 2007 I have a situation where I have different pages and I want to echo different content on those pages by using either an include or simple parse change. The code I have looks tragically heavy. I'd like a lighter cleaner code snippet and I'm open to any suggestions on this. Please don't say "learn to program" that part I already know and I'm working on it. <?php $P=$_SERVER['REQUEST_URI']; switch ($P) { //case "/jared/index.html" || $P == "/" || "$P" == "/jared/index.php": case "/jared/index.php"; $data1 = 'current'; $data2 = ''; $data3 = ''; $data4 = ''; $data5 = ''; break; case "/jared/page2.html": $data1 = ''; $data2 = 'current'; $data3 = ''; $data4 = ''; $data5 = ''; break; case "page3.html"; $data1 = ''; $data2 = ''; $data3 = 'current'; $data4 = ''; $data5 = ''; $data = 'current'; break; case "page4.html": $data1 = ''; $data2 = ''; $data3 = ''; $data4 = 'current'; $data5 = ''; break; case "page5.html": $data1 = ''; $data2 = ''; $data3 = ''; $data4 = ''; $data5 = 'current'; break; default: $data = 'default'; } ?> Then it would be in the HTML as <h1><?php echo($data); ?></h1> (or something to this effect.) All I want the script snippet to do is IF its on a certain page then display the data for that. ~ Jared Ritchey Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 6, 2007 Share Posted June 6, 2007 What is the purpose of data1, data2, data3, etc? That seems to be the overkill here. Quote Link to comment Share on other sites More sharing options...
akitchin Posted June 6, 2007 Share Posted June 6, 2007 first off, you're using a semicolon rather than a colon for the first case. second, why not initialise all $data variables as empty at the start and simply re-assign the value that you intend to change? $data1 = ''; $data2 = ''; $data3 = ''; $data4 = ''; $data5 = ''; switch($P) { case 'first_case': $data1 = 'current'; } but i'll be honest, to make a nice set of code, i think you'll have to rethink what you're doing overall here. what are you using this snippet as a part of? Quote Link to comment Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 agrees with mjdamato...this would look nicer: <?php $data1 = ''; $data2 = ''; $data3 = ''; $data4 = ''; $data5 = ''; $P=$_SERVER['REQUEST_URI']; switch ($P) { //case "/jared/index.html" || $P == "/" || "$P" == "/jared/index.php": case "/jared/index.php": $data1 = 'current'; break; case "/jared/page2.html": $data2 = 'current'; break; case "page3.html"; $data3 = 'current'; $data = 'current'; break; case "page4.html": $data4 = 'current'; break; case "page5.html": $data5 = 'current'; break; default: $data = 'default'; } ?> Quote Link to comment Share on other sites More sharing options...
php4ever Posted June 8, 2007 Author Share Posted June 8, 2007 Sorry People I'm not getting my emails when you respond to this thread. Basically this little snippet is only to serve a single simple purpose. IF a person is on a specific web page display an advertisement and or header image specific to that page. The idea was that I would enter the name of the page and then set the contents for that page in a variable or in this case $data and the contents of that would load. I could then do a simple php include for various areas of the site too. Since the only objective here is to first get the http uri and then display the appropriate data accordingly. I made a mistake by posting that code snippet with "current" it was meant to be a generic thing. It could actually be header.php or advertisement.php or randomfooter.php for example. I just needed a simple way of controlling the contents on a per page basis. I'll do what you suggest. Quote Link to comment Share on other sites More sharing options...
php4ever Posted June 8, 2007 Author Share Posted June 8, 2007 Oh yeah, I tried like hell to get the line that is commented out to work and couldn't case "/jared/index.html" || $P == "/" || "$P" == "/jared/index.php": The idea is that if a person lands on the homepage without the file extension of either index.php or index.html they would still get the same $data1 results but for the life of me it doesn't work that way unless the actual page is displayed. Is my syntax wrong? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 8, 2007 Share Posted June 8, 2007 In this case (no pun intended) you would just list each "case" statement under one another: <?php $P=$_SERVER['REQUEST_URI']; switch ($P) { case '/jared/index.html': case '/': $data1 = 'current'; break; // // etc... // } ?> Ken Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 8, 2007 Share Posted June 8, 2007 You should have a break statement inside every case right? If not, it could keep processing if another case is hit ... say if you're updating a variable that your case is evaluating. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 8, 2007 Share Posted June 8, 2007 You should have a break statement inside every case right? If not, it could keep processing if another case is hit ... say if you're updating a variable that your case is evaluating. Not necessarily. You can have several case statements with a common result as kenrbnsn demonstrated. With a switch statement once a case matches the switch value ALL result code will be executed unless you have break statements: Example: <?php $var = 10; switch ($var) { case 10: echo "Value = TEN<br>"; break; case 8: echo "Value = EIGHT<br>"; break; } //Output: // Value = TEN $var = 10; switch ($var) { case 10: echo "Value = TEN<br>"; case 8: echo "Value = EIGHT<br>"; } //Output: // Value = TEN // Value = EIGHT ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 You should have a break statement inside every case right? If not, it could keep processing if another case is hit ... say if you're updating a variable that your case is evaluating. Not necessarily. You can have several case statements with a common result as kenrbnsn demonstrated. With a switch statement once a case matches the switch value ALL result code will be executed unless you have break statements: Example: <?php $var = 10; switch ($var) { case 10: echo "Value = TEN<br>"; break; case 8: echo "Value = EIGHT<br>"; break; } //Output: // Value = TEN $var = 10; switch ($var) { case 10: echo "Value = TEN<br>"; case 8: echo "Value = EIGHT<br>"; } //Output: // Value = TEN // Value = EIGHT ?> To expand on that example: <?php $var = 11; switch ($var) { // this wil lprint eleven and ten, since there is no break after 11 case 11: echo "Value = ELEVEN"; case 10: echo "Value = TEN<br>"; break; case 8: echo "Value = EIGHT<br>"; break; } //Output: // Value = TEN $var = "ten"; switch ($var) { case "ten": echo "value = TEN (Literally)<br />"; case 10: echo "Value = TEN (numerically)<br>"; break; case 8: echo "Value = EIGHT<br>"; break; } //Output: // Value = TEN // Value = EIGHT ?> Which is all valid, just thought I would add some extra flavor to mjdamato's nice example. Basically it will go through cases if a value is true until a break is hit. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 8, 2007 Share Posted June 8, 2007 What the hell! The second part of my example went missing. Oh well, frost picked up the ball. Quote Link to comment Share on other sites More sharing options...
php4ever Posted June 8, 2007 Author Share Posted June 8, 2007 All good information thanks a million. ~ Jared Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.