mallen Posted December 11, 2012 Share Posted December 11, 2012 I am trying to do switch where it changes the name of the post title. I just can't seem to get the format correct. I tired the variable $P also and it doesn't work. function corepages() { $pages = get_pages(); global $blog_id; foreach ($pages as $p) { if ( file_exists( compat_get_plugin_dir( 'dir' ) . '/icons/' . $p['icon'] ) ) { $image = compat_get_plugin_url( 'dir' ) . '/icons/' . $p['icon']; } else { $image = compat_get_upload_url() . '/images/' . $p['icon']; } if ($p['post_title'] !== "") : { echo('<li><a href="' . get_permalink($p['ID']) . '"><img src="' . $image . '" alt="icon" />' . __($p['post_title']) . '</a></li>'); } else : switch($p['post_title']){ case 'Products Pages': $p = "Our Products"; break; case 'Contact Our Company': $p = "Contact Us"; break; } endif; } } Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/ Share on other sites More sharing options...
Stooney Posted December 11, 2012 Share Posted December 11, 2012 (edited) Are you getting an error? What exactly is going wrong with the switch? Also, amoungst your cases, try adding a default case so you know if none of your cases are being matched. <?php switch($p['post_title']){ default: $p = "Default Title"; break; case 'Products Pages': $p = "Our Products"; break; Edited December 11, 2012 by Stooney Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/#findComment-1398746 Share on other sites More sharing options...
mallen Posted December 11, 2012 Author Share Posted December 11, 2012 I got it to work but Ihave to hard code each page. I was trying to get something more dynamic to switch the name. function corepages() { $pages = get_pages(); global $blog_id; foreach ($pages as $p) { if ( file_exists( compat_get_plugin_dir( 'dir' ) . '/icons/' . $p['icon'] ) ) { $image = compat_get_plugin_url( 'dir' ) . '/icons/' . $p['icon']; } else { $image = compat_get_upload_url() . '/images/' . $p['icon']; } if ($p['post_title'] == "Products Pages") : { echo ('<li><a href="' . get_permalink($p['ID']) . '"><img src="' . $image . '" alt="icon" />' . 'Our Products' . '</a></li>'); } else : switch($p['post_title']){ case 'Contact Our Company': echo ('<li><a href="' . get_permalink($p['ID']) . '"><img src="' . $image . '" alt="icon" />' . 'Contact Us' . '</a></li>'); break; } case 'Our Locations': echo ('<li><a href="' . get_permalink($p['ID']) . '"><img src="' . $image . '" alt="icon" />' . 'Locations' . '</a></li>'); break; endif; } } Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/#findComment-1398747 Share on other sites More sharing options...
mrMarcus Posted December 11, 2012 Share Posted December 11, 2012 (edited) What you did in your first posted code would never work as you've placed your switch() within the else statement, which is conditional on $p['post_title'] not having a value. So, $p['post_title'] being empty, your switch() will not work (as expected): if ($p['post_title'] !== "") : echo('<li><a href="' . get_permalink($p['ID']) . '"><img src="' . $image . '" alt="icon" />' . __($p['post_title']) . '</a></li>'); else : switch($p['post_title']) { //nothing to "switch" as $p['post_title'] does not contain a value; use "default" to handle issues within a switch() case 'Products Pages': $p = "Our Products"; break; case 'Contact Our Company': $p = "Contact Us"; break; default: 'This is the default title'; $p = 'Some page'; break; } endif; Edit: And I noticed you're using a triple comparison operator: if ($p['post_title'] !== "") : I will assume you know what it does over a double comparison operator, however, if not, please see the following link to get a better understanding: http://php.net/manual/en/language.operators.comparison.php Edit #2: switch() is no different that 'if/else if' in logic. It is arguably a more readable structure, but that just boils down to personal preference. So don't go out of your way to get a switch() statement within your code just for the sake of it. Within a switch, the case is your if/else if, and default is your final else statement/condition. It's all the same, really. Edited December 11, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/#findComment-1398753 Share on other sites More sharing options...
mallen Posted December 11, 2012 Author Share Posted December 11, 2012 (edited) hanks for the info. I will leave the above as it is. I now realize the similarity of if/else and a switch. How about this example for a another part of my site. Same type of issue. I am printing a page title and I need to change the name dynamically. All I get is the page name its not changing based on my logic. $title = the_title(); if ($title == 'Contact Info'): { echo ("Contact Us"); } else: switch($title){ case 'Products Pages': echo ("Our Products"); break; case 'Find An Agent': echo ("Locate an Agent"); break; } endif; Edited December 11, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/#findComment-1398783 Share on other sites More sharing options...
mrMarcus Posted December 11, 2012 Share Posted December 11, 2012 (edited) All I get is the page name its not changing based on my logic. I don't understand what you're getting at here. What is your expected output? Have you echo'd $title to ensure it holds an expected value? You're are needlessly combining if/else with switch. $title = the_title(); switch ($title) { case 'Contact Info': echo 'Contact Us'; break; case 'Products Pages': echo 'Our Products'; break; case 'Find An Agent': echo 'Locate an Agent'; break; default: echo '$title not set'; // header('Location: /'); // possible redirect if $title not set/is empty //exit(0); break; } Edited December 11, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/#findComment-1398787 Share on other sites More sharing options...
mallen Posted December 11, 2012 Author Share Posted December 11, 2012 (edited) Yes I var_dump($title) and it has a value of NULL and original title for each page shows unchanged. Edited December 11, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/#findComment-1398788 Share on other sites More sharing options...
mrMarcus Posted December 11, 2012 Share Posted December 11, 2012 Yes I var_dump($title) and it has a value of NULL and original title for each page shows unchanged. So, if $title is null, what are we going on about here? You need to fix the code that is supposed to give $title a value/page title. Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/#findComment-1398790 Share on other sites More sharing options...
mallen Posted December 11, 2012 Author Share Posted December 11, 2012 Correct that is my issue. Its null and therefore not switching according to my logic. the_title() function holds a value but $title does not. Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/#findComment-1398791 Share on other sites More sharing options...
mrMarcus Posted December 11, 2012 Share Posted December 11, 2012 Well, I have no idea what the title() function is returning and/or why it is not working without seeing the function declaration. Did you build the site code? Or is it based off a 3rd-party app, ie. Wordpress, etc? Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/#findComment-1398793 Share on other sites More sharing options...
mallen Posted December 11, 2012 Author Share Posted December 11, 2012 I solved it using this: $title = the_title('', '', false); I guess since it was a WordPress function I should of looked there first. Quote Link to comment https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/#findComment-1398794 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.