www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Anyone got a better Solution

  1. #1
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125

    resolved [RESOLVED] Anyone got a better Solution

    Working on a website for a patch collector and i have the navigation for the states blank out when there is no patches in the mysql DB the way i have it set up just seems really slow.

    PHP Code:
    <?php $rc mysql_query("SELECT * FROM patch_table WHERE p_state = 'AL'"); $rc_rows mysql_num_rows($rc); if ($rc_rows 0) {?>
    <a class="statelink" href="show.php?loc=AL" >Alabama</a><br/><?php } else { ?><a style="color: #c3c3c3;" >Alabama</a><br/><?php ?>
    <?php $rc 
    mysql_query("SELECT * FROM patch_table WHERE p_state = 'AK'"); $rc_rows mysql_num_rows($rc); if ($rc_rows 0) {?>
    <a class="statelink" href="show.php?loc=AK" >Alaska</a><br/><?php } else { ?><a style="color: #c3c3c3;" >Alaska</a><br/><?php ?>
    <?php $rc 
    mysql_query("SELECT * FROM patch_table WHERE p_state = 'AZ'"); $rc_rows mysql_num_rows($rc); if ($rc_rows 0) {?>
    <a class="statelink" href="show.php?loc=AZ" >Arizona</a><br/><?php } else { ?><a style="color: #c3c3c3;" >Arizona</a><br/><?php ?>
    <?php $rc 
    mysql_query("SELECT * FROM patch_table WHERE p_state = 'AR'"); $rc_rows mysql_num_rows($rc); if ($rc_rows 0) {?>
    <a class="statelink" href="show.php?loc=AR" >Arkansas</a><br/><?php } else { ?><a style="color: #c3c3c3;" >Arkansas</a><br/><?php ?>
    <?php $rc 
    mysql_query("SELECT * FROM patch_table WHERE p_state = 'CA'"); $rc_rows mysql_num_rows($rc); if ($rc_rows 0) {?>
    <a class="statelink" href="show.php?loc=CA" >California</a><br/><?php } else { ?><a style="color: #c3c3c3;" >California</a><br/><?php ?>
    <?php $rc 
    mysql_query("SELECT * FROM patch_table WHERE p_state = 'CO'"); $rc_rows mysql_num_rows($rc); if ($rc_rows 0) {?>
    <a class="statelink" href="show.php?loc=CO" >Colorado</a><br/><?php } else { ?><a style="color: #c3c3c3;" >Colorado</a><br/><?php }?>
    goes on like that for quite a while. Anyone got a shorter way of doing it?
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  2. #2
    Join Date
    Jul 2011
    Location
    0xf72408d5eb9d0ae5
    Posts
    30
    does this help any?

    PHP Code:
    <?php
        $stateAbbrs
    =array('AL','AZ',....etc);
        
    $states=array('Alabama','Arizona',....etc);
        for(
    $i=0$i<sizeof($states);$i++)
        {
            
    $result mysql_query("SELECT * FROM patch_table WHERE p_state='".$stateAbbrs[$i]."'");
            if(
    mysql_num_rows($result)>0)
            {
                echo 
    '<a class="statelink" href="show.php?loc='.$stateAbbrs[$i].'" >'.$states[$i].'</a><br/>';
            }
            else
            {
                echo 
    '<a style="color: #c3c3c3;" >'.$states[$i].'</a><br/>';
            }
        }
    }
    ?>
    [NOT TESTED]

  3. #3
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125
    Worked perfectly, Thanks.
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

  4. #4
    Join Date
    Aug 2010
    Location
    Ohio
    Posts
    136
    Just personal preferance but I would have done it as such:
    PHP Code:
    $states = array( 'AL' => 'Alabama','AK' => 'Arkansas',...);
    foreach( 
    $states as $abbr => $name ) {
       
    $result mysql_query("SELECT * FROM patch_table WHERE p_state='".$abbr."'");
       if(
    mysql_num_rows($result)>0) {
          echo 
    '<a class="statelink" href="show.php?loc='.$abbr.'" >'.$name.'</a><br/>';
       } else {
          echo 
    '<a style="color: #c3c3c3;" >'.$name.'</a><br/>';
       }


  5. #5
    Join Date
    May 2011
    Location
    With Hobbits
    Posts
    125
    Great Solution, Most likely going to use Yours. Thanks
    Albert Einstein ~ "Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction."

    Things To help you on this Site

    1. Place all php code in [php] Tags
    2. Place all html code in [html] Tags
    3. Remember to mark all your Threads Resolved using thead tools at the top when your problem is solved

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles