Jump to content

[SOLVED] HElp With Else statement


CodeMama

Recommended Posts

:facewall: 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());
                                                          
                                                              }
                                                              

Link to comment
https://forums.phpfreaks.com/topic/168639-solved-help-with-else-statement/
Share on other sites

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;
?>

 

 

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());
}

 

 

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.

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&nbsp', $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']
                                                                );
                                                            
                                                        }
                                                      
                                                     
                                                     
                                                     
                                                     
                                                    

<?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

 

 

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&nbsp', $letter);

}

else if($letter == $selectedLetter && $selectedLetter == 'nums')

{

$menu .= sprintf('%s&nbsp', '#');

}

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']);

 

}                         

    }

                                                   

                                                     

                                                     

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.