Jump to content

how do i redirect in php if statement true?


Darkwoods

Recommended Posts

Hey here is my code im trying to redirect  the index.php to index.php?id=1

the way i have done it doesn't look really good is there a better way to do it please?

 

im trying to redirect to id=1 because otherwise i wont be able to echo out anything...

 

id 1 is default category so is there a way to tell it if  $id is empty echo from id 1?

 

<?php   

        $id = $_GET['id']; 
		if($id == ''){
			echo "<meta http-equiv=Refresh content=0;url=index.php?id=1>";

		}	
        $result = mysql_query("SELECT * FROM pages WHERE id='$id' ",$connect);
        while($row = mysql_fetch_assoc($result))
             {
        echo $row['anything1']; 
        echo $row['anything2']; 
} 
?>

Don't bother actually redirecting, just set a default.

 

You really need to check your query results before using them too.

 

if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
} else {
  $id = 1;
} 

if ($result = mysql_query("SELECT * FROM pages WHERE id='$id' ",$connect)) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_assoc($result)) {
      echo $row['anything1']; 
      echo $row['anything2']; 
    }
  }
}

I don't know that this is exactly what you are asking but if it is not please be more specific:

 

<?php   

        $id = $_GET['id']; 
            if($id == '1'){
                header("location: ./index.php?id=1");
            }
        $result = mysql_query("SELECT * FROM pages WHERE id='$id' ",$connect);
        while($row = mysql_fetch_assoc($result)){
            echo $row['anything1']; 
            echo $row['anything2']; 
        } 
?>

thanks this worked out really good as thrope made it :)

I was hoping to do it without having to redirecting it :)

thanks for both of you..

 

actually im doing a multiple language page  so that would be the easiest way for me to do it :P

for example id 1 would be English id 2 would be Spanish etc

 

 

 

 

sorry i didn't get what you mean

at the moment I have it this way:

 

index.php?id=1 will show the English content from the database

index.php?id=2 will show the Spanish content from the database..

 

I know this wouldn't be good if you got more than one page website...

That's what I mean like if you have like: home about us contact services portfolio. You could have it in a database like this.

 

home
$id = 1
about us
$id = 2
contact
$id = 3
etc...

Then you could have language settings for all of those with a simple $_GET string. Just a thought it would make it a lot easier to use. I would also recommend that you use an apache mod_rewrite for SEO purposes. Again they are all best practices and it is totally up to you.

Thanks,

Colton Wagner

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.