CodeMama Posted July 31, 2009 Share Posted July 31, 2009 I have this script that works, just a couple tweeks needed... 1. I don't want it to default to A being selected 2. I need the number range part to only display one # sign not 10 of them, somehow I have to have it pull any record that does not start with alpha <?php $letter = isset($_GET['letter']) ? $_GET['letter'] :"A"; echo '<div align="center"><b>'; foreach(range('A','Z') as $c){ ($letter == $c) ? printf('%s ',$c) : printf('<a href="browse.php?letter=%s">%s</a> ',$c,$c); } echo '<br>'; //Other foreach(range('0','9') as $n){ ($letter == $n) ? printf('%s ',$n) : printf('<a href="?letter=%s">#</a> ',$n,$n); } echo "</b><br></div><p>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/168303-foreach-range-problem/ Share on other sites More sharing options...
wildteen88 Posted July 31, 2009 Share Posted July 31, 2009 1. I don't want it to default to A being selected This line $letter = isset($_GET['letter']) ? $_GET['letter'] :"A"; Will assign the letter A to the $letter variable by default if the variable $_GET['letter'] doesn't exist. Use the following if you don't want $letter to have a default value $letter = isset($_GET['letter']) ? $_GET['letter'] : null; 2. I need the number range part to only display one # sign not 10 of them, somehow I have to have it pull any record that does not start with alpha This ($letter == $n) ? printf('%s ',$n) : printf('<a href="?letter=%s">#</a> ',$n,$n); Needs to be ($letter == $n) ? printf('%s ',$n) : printf('<a href="?letter=%s">%s</a> ',$n,$n); Now numbers will be displayed rather than #'s Quote Link to comment https://forums.phpfreaks.com/topic/168303-foreach-range-problem/#findComment-887733 Share on other sites More sharing options...
CodeMama Posted July 31, 2009 Author Share Posted July 31, 2009 Thanks, but I have the numbers displayed and boss wants just 1 # sign displayed and then the view page to list all records that don't start with alpha since there are really only a few of them that begin with a number... Quote Link to comment https://forums.phpfreaks.com/topic/168303-foreach-range-problem/#findComment-887779 Share on other sites More sharing options...
wildteen88 Posted July 31, 2009 Share Posted July 31, 2009 You'll have to add it on its own outside of your loops. Add this echo '<a href="?letter=#">#</a> '; Before this line foreach(range('0','9') as $n){ Now you'll get a list like # 0 1 2 3 4 5 6 7 8 9 Quote Link to comment https://forums.phpfreaks.com/topic/168303-foreach-range-problem/#findComment-887782 Share on other sites More sharing options...
CodeMama Posted July 31, 2009 Author Share Posted July 31, 2009 right but ALL i want (or all the boss wants) is just the # no numbers to show Quote Link to comment https://forums.phpfreaks.com/topic/168303-foreach-range-problem/#findComment-887799 Share on other sites More sharing options...
Psycho Posted July 31, 2009 Share Posted July 31, 2009 I suggest separating your logic from the actual display of the content. Makes it much easier in the log run. In any event, this should get you what you want. You can put the logic (the top part) in a separate page or at the top of your code. Then put the last line within the context of your page. <?php //Create array with letters AND number sign $letters = range('A','Z'); array_push($letters, '#'); $menu = ''; foreach($letters as $letter) { $menu .= ($letter == $_GET['letter']) ? sprintf('%s ', $letter) : sprintf('<a href="browse.php?letter=%s">%s</a> ', $letter, $letter); } echo "<div align=\"center\"><b>{$menu}</b><br /></div>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/168303-foreach-range-problem/#findComment-887803 Share on other sites More sharing options...
CodeMama Posted July 31, 2009 Author Share Posted July 31, 2009 well it does and doesn't work first i get this error: Notice: Undefined index: letter for each loop then it outputs the menu Quote Link to comment https://forums.phpfreaks.com/topic/168303-foreach-range-problem/#findComment-887827 Share on other sites More sharing options...
Psycho Posted July 31, 2009 Share Posted July 31, 2009 Ah,that was me being lazy. You need to define the "default" like you had before with wildteen's update. My code was referencing $_GET['letter'] without verifying it was set. <?php error_reporting(E_ALL); //Create array with letters AND number sign $letters = range('A','Z'); array_push($letters, '#'); $menu = ''; $selectedLetter = isset($_GET['letter']) ? $_GET['letter'] : null; foreach($letters as $letter) { $menu .= ($letter == $selectedLetter) ? sprintf('%s ', $letter) : sprintf('<a href="browse.php?letter=%s">%s</a> ', $letter, $letter); } echo "<div align=\"center\"><b>{$menu}</b><br /></div>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/168303-foreach-range-problem/#findComment-887841 Share on other sites More sharing options...
CodeMama Posted July 31, 2009 Author Share Posted July 31, 2009 mjdamato thanks you rock!! now all that is left is to tell it if its # to pull all records that begin with a number instead of a letter ! yay.... Quote Link to comment https://forums.phpfreaks.com/topic/168303-foreach-range-problem/#findComment-887862 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.