CodeMama Posted August 3, 2009 Share Posted August 3, 2009 I can't get this to work and need extra eyes: if ($selectedLetter == "#") { $a= "1"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }else { $a= "2"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }else{ $a= "3"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }else { $a= "4"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }else { $a= "5"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }else { $a= "6"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }else { $a= "7"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }else { $a= "8"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }else { $a= "9"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }else { $a= "0"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; $result = mysql_query($sql) or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/ Share on other sites More sharing options...
dadamssg Posted August 3, 2009 Share Posted August 3, 2009 save your "else" statement for the very last option. Use "elseif" for the 2nd through next to last options Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889623 Share on other sites More sharing options...
Maq Posted August 3, 2009 Share Posted August 3, 2009 What exactly are you trying to do? This code makes no sense. Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889627 Share on other sites More sharing options...
darkfreaks Posted August 3, 2009 Share Posted August 3, 2009 try using a switch statement for $a instead of using a bazillion else statements <?php switch($a) case ($a==1): echo "$a equals one"; break; case ($a==2): echo"$a equals two"; break; case ($a==3): echo"$a equals three"; break; case($a==4): echo"$a equals four"; case($a==5): echo "$a equals five"; break; case($a==6): echo"$a equals six"; break; case($a==7): echo"$a equals seven"; break; case($a==: echo "$a equals eight"; break; case($a==9): echo"$a equals nine"; break; ?> Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889652 Share on other sites More sharing options...
dadamssg Posted August 3, 2009 Share Posted August 3, 2009 and repeating your $sql is pointless for every case. leave it out of the cases or else stmt. And right now the query only gets executed if $a = 0...probably not what you want Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889656 Share on other sites More sharing options...
Maq Posted August 3, 2009 Share Posted August 3, 2009 and repeating your $sql is pointless for every case. leave it out of the cases or else stmt. And right now the query only gets executed if $a = 0...probably not what you want I agree. What are the conditions? The only thing I see is that it must be a number be 0-9. In this case, you would just need a single check and a single query. if($selectedLetter >= 0 && $selectedLetter $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$selectedLetter}%'"; $result = mysql_query($sql) or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889659 Share on other sites More sharing options...
darkfreaks Posted August 3, 2009 Share Posted August 3, 2009 i think she is checking to see if selected letter has a number sign before it. after that i have no clue what she is doing. anyhow she could also use a simple switch statement for the $a variable. then use a singe query in the if statement <?php switch($a) case ($a==1): $a="1"; break; case ($a==2): $a="2"; break; case ($a==3): $a="3" break; case($a==4): $a="4"; case($a==5): $a="5"; break; case($a==6): $a="6"; break; case($a==7): $a="7"; break; case($a==: $a="8"; break; case($a==9): $a="9"; break; if($SelectedLetter=="#") { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; $result = mysql_query($sql) or die(mysql_error()); } ?> but i agree MAQ's code is much more compact then again i am not sure what she is trying to acomplish. Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889670 Share on other sites More sharing options...
CodeMama Posted August 3, 2009 Author Share Posted August 3, 2009 What I'm trying to do here is this: I have a browse page with links - A B C D E F and so on, I did have also 1 2 3 4 etc for any records that begin with a number instead of letter... well boss didn't want a numerical list only to have a A B C -Z #, so if the user clicked on # it would return any and all records that begin with a number instead of a letter, below I will post the myriad of ways I have tried to accomplish this all failing, it either breaks the menu completely or returns all records with the # being picked. //alphabetical pagination links //if the page is browse default to A's if on other pages just show pagination links and not default listings if ($_SERVER['SCRIPT_FILENAME'] = "browse.php" ) { $default = "A"; } else { $default = ""; } //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>"; //Show all restaurants that start with $letter $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$selectedLetter}%'"; /* //Show all restaurants that start with $letter not between "A" and "Z" if ($selectedLetter == "#") { $a= "1"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }elseif ($selectedLetter == "#"){ $a= "2"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }elseif ($selectedLetter == "#"){ $a= "3"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }elseif ($selectedLetter == "#"){ $a= "4"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }elseif ($selectedLetter == "#"){ $a= "5"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }elseif ($selectedLetter == "#"){ $a= "6"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }elseif ($selectedLetter == "#"){ $a= "7"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }elseif ($selectedLetter == "#"){ $a= "8"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }elseif ($selectedLetter == "#"){ $a= "9"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; }else{ $a= "0"; $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$a}%'"; } */ /*doesn't work returns all results for # if ($selectedLetter == "#"){ $other = range('0','9'); $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$other}%'"; } if ($selectedLetter =="#"){ $other = ctype_digit(range('0','9')); $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$other}%'"; } */ //$sql = "SELECT DISTINCT ID, name, address FROM restaurants left(name, 1) between '0' and '9'"; // $result = mysql_query($sql) or die(mysql_error()); /* if ($selectedLetter == "#"){ $other = range('0','9'); $values = implode(',', $other); $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$values}%'"; } */ /*//Show all restaurants that start with $selectedLetter if ($selectedLetter != "#") { //Letter was selected $where = "name LIKE '%{$selectedLetter}'" } else { //Number was selected $where = "SUBSTRING(name,1,1) IN ('" . implode("','", range(0,9)) . "')"; } $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE $where"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ $name = $row['name']; printf('<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address'] ); }*/ $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ $name = $row['name']; printf( '<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address'] ); } Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889678 Share on other sites More sharing options...
darkfreaks Posted August 3, 2009 Share Posted August 3, 2009 that is what i thought have you tried using a switch like above Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889679 Share on other sites More sharing options...
Maq Posted August 3, 2009 Share Posted August 3, 2009 Try this: if($selectedLetter == '#') { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name REGEXP '^[0-9].*' ORDER BY name ASC"; $result = mysql_query($sql) or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889680 Share on other sites More sharing options...
CodeMama Posted August 3, 2009 Author Share Posted August 3, 2009 Thanks for the try Maq, but it just returns all records, like my other tries do. grrrrrrrrrrr this is driving me nuts and I'm down to an hour and 20 min to have it done before the chew out starts...MONDAYS ARGH Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889727 Share on other sites More sharing options...
darkfreaks Posted August 3, 2009 Share Posted August 3, 2009 <?php if(!ctype_alpha($SelectedLetter) && $SelectedLetter=="#") { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name REGEXP '^[0-9].*' ORDER BY name ASC"; $result = mysql_query($sql) or die(mysql_error()); } ?> hopefully this should select everything that is not a letter and select everything that is a number sign Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889768 Share on other sites More sharing options...
CodeMama Posted August 3, 2009 Author Share Posted August 3, 2009 Here it is what finally worked: //Create array with letters AND number sign $letters = range('A','Z'); array_push($letters, 'nums'); $menu = ''; $selectedLetter = isset($_GET['letter']) ? $_GET['letter'] : null; foreach($letters as $letter) { if($letter == $selectedLetter && $selectedLetter != 'nums') { $menu .= sprintf('%s ', $letter); } else if($letter == $selectedLetter && $selectedLetter == 'nums') { $menu .= sprintf('%s ', '#'); } else { if($letter == 'nums') { $menu .= sprintf('<a href="browse.php?letter=%s">%s</a> ', 'nums', '#'); } else { $menu .= sprintf('<a href="browse.php?letter=%s">%s</a> ', $letter, $letter); } } } echo "<div align=\"center\"><b>{$menu}</b><br /></div>"; //Show all restaurants that start with $letter not between "A" and "Z" if ($selectedLetter == "nums") { for($i = 0; $i <= 9; $i++) { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '$i%'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $name = $row['name']; printf('<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address']); } } } else { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '$selectedLetter%'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $name = $row['name']; printf('<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address']); } } Quote Link to comment https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/#findComment-889778 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.