Darkwoods Posted October 21, 2010 Share Posted October 21, 2010 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/216440-how-do-i-redirect-in-php-if-statement-true/ Share on other sites More sharing options...
trq Posted October 21, 2010 Share Posted October 21, 2010 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']; } } } Quote Link to comment https://forums.phpfreaks.com/topic/216440-how-do-i-redirect-in-php-if-statement-true/#findComment-1124715 Share on other sites More sharing options...
Colton.Wagner Posted October 21, 2010 Share Posted October 21, 2010 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/216440-how-do-i-redirect-in-php-if-statement-true/#findComment-1124716 Share on other sites More sharing options...
Colton.Wagner Posted October 21, 2010 Share Posted October 21, 2010 Thrope is absolutely correct don't waste your time redirecting unless the id needs to be changed. Thanks, Colton Wagner Quote Link to comment https://forums.phpfreaks.com/topic/216440-how-do-i-redirect-in-php-if-statement-true/#findComment-1124717 Share on other sites More sharing options...
Darkwoods Posted October 21, 2010 Author Share Posted October 21, 2010 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 for example id 1 would be English id 2 would be Spanish etc Quote Link to comment https://forums.phpfreaks.com/topic/216440-how-do-i-redirect-in-php-if-statement-true/#findComment-1124720 Share on other sites More sharing options...
Colton.Wagner Posted October 21, 2010 Share Posted October 21, 2010 Why not have the same I'd and set the language like: Index.php?id=1&lang=1or2 for like spanish or english that way the homepage can have the same content just in different languages. Quote Link to comment https://forums.phpfreaks.com/topic/216440-how-do-i-redirect-in-php-if-statement-true/#findComment-1124724 Share on other sites More sharing options...
Darkwoods Posted October 21, 2010 Author Share Posted October 21, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/216440-how-do-i-redirect-in-php-if-statement-true/#findComment-1124730 Share on other sites More sharing options...
Colton.Wagner Posted October 21, 2010 Share Posted October 21, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/216440-how-do-i-redirect-in-php-if-statement-true/#findComment-1124732 Share on other sites More sharing options...
Darkwoods Posted October 21, 2010 Author Share Posted October 21, 2010 oh I see what you mean now.. I will look into it but im still a total n00b lol anyway thanks Quote Link to comment https://forums.phpfreaks.com/topic/216440-how-do-i-redirect-in-php-if-statement-true/#findComment-1124739 Share on other sites More sharing options...
Colton.Wagner Posted October 21, 2010 Share Posted October 21, 2010 Also look into the google translate API if your gonna make multiple languages go big or go home! Thanks, Colton Wagner Quote Link to comment https://forums.phpfreaks.com/topic/216440-how-do-i-redirect-in-php-if-statement-true/#findComment-1124748 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.