Jump to content

[SOLVED] Simple If Else Help


jjmusicpro

Recommended Posts

Ok i am sure someone can figure this out, I've been banging my head trying to figure it out.

 

 

The user will come to the index page, and type in a zip code. from there it will post to itself, and do either of 3 things.

 

If no results are found display "No results found"

If 1 result is found, i wanted it to redirect to that url, i cant seem to get that javascript to work.

and if 2 or more results are found, then loop through like i was doing, and give me the results.

 

I cant seem to get this right...it wont redirect when there is 1 result.

 

<?php
  if($_SERVER['REQUEST_METHOD'] == "POST"){ 
   require_once ('connect.php'); 
  require_once ('opendb.php'); 
  
  $zipcode = $_POST['zipcode_entered_search'];

  $query = "SELECT * FROM zipcodes WHERE zipcode='$zipcode'"; 
  $result = @mysql_query ($query); 
  $count = mysql_num_rows($result); //number of results 

if($count=="0"){ 
     echo "Nothing Found!"; 
}else if($count=="1"){ 

echo "<script type='text/javascript'>
window.location = 'http://www.google.com/'
</script>";

}else{ 
     echo "Located $count result(s)<br />"; 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
     echo $row['company_name'] . '<br>' .'<a href="'. $row['redirect_url'] . '"> Visit Website </a>'. ' ' .'<br>' . $row['notes_1'] . '<p>'; 
    }  
} 

  }else{ 
// nothing
  } 
?> 

Link to comment
Share on other sites

when you are using numbers you should usually do this:

if($x == 0)...

 

for the redirect use: header() or a meta refresh. Those might work a little better.

 

i cant use header() because this php is in the middel of my code.

 

How do i do a meta refresh?

Link to comment
Share on other sites

}elseif($count == 1){ 

echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=http://google.com\">";

}

 

you wouldn't want to put that in the else

 

thats where i need it to go becuase if it matches the zip code, and only brings back 1 result, i need it to redirct to the redirct_url thats in the database with that zip.

Link to comment
Share on other sites

Then wouldn't you want this:

 

}elseif($count == 1){ 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
$site = $row['redirect_url'] ;
echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=$site\">";
}
} 

 

because if you have more then 1 row returned you want a link, not a refresh.

Link to comment
Share on other sites

Then wouldn't you want this:

 

}elseif($count == 1){ 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
$site = $row['redirect_url'] ;
echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=$site\">";
}
} 

 

because if you have more then 1 row returned you want a link, not a refresh.

 

This is what i have now, and i cant seem to get it to go tot he new site.

the page flashes, but it wont go anywhere..

 

 

            <?php
  if($_SERVER['REQUEST_METHOD'] == "POST"){ 
   require_once ('connect.php'); 
  require_once ('opendb.php'); 
  
  $zipcode = $_POST['zipcode_entered_search'];

  $query = "SELECT * FROM zipcodes WHERE zipcode='$zipcode'"; 
  $result = @mysql_query ($query); 
  $count = mysql_num_rows($result); //number of results 

if($count=="0"){ 
 echo '<b>No locations found for your zip.</b><br>Please fill out the form below so we can help your further.<p>';
}else if($count=="1"){ 

$site = $row['redirect_url'] ;
echo "<META HTTP-EQUIV=Refresh CONTENT=\"4; URL=$site\">";

}else{ 
     echo "Located $count result(s)<br /><br><table width=\"300\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">"; 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
     echo '<tr><td><img src=mm_smaller.gif></td><td><b>'.$row['company_name'] . '</b>'. '<br>' .'<a href="'. $row['redirect_url'] . '"> Visit Website </a>'. ' ' .'<br>' . $row['notes_1'] . '</td><p></tr><p><p>'; 
    }echo '</tr></table>';
} 

  }else{ 
// nothing
  } 
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.