Page 1 of 1
URL Question
Posted: Fri Jul 23, 2010 12:40 pm
by Thizzle
READING THIS WON'T BE AS USEFUL AS READING MY LAST POST
Website:
http://gift-for-men.com/
PHP Version: 5.0
Alright, what my website does is automatically generate a "Gift" suggestion whenever someone visits "Gift-For-Men.Com." That's exactly what I wanted and exactly what I built. Anyways, the problem I ran into is I wanted to implement social features like the Facebook like button. After looking through books, google, and more I finally decided I can't figure it out on my own. So if you could please help me out by answering the question.
Question: What I want is when the person visits the page it automatically generates a "suggestion" as it's doing. But when it generates that suggestion it adds the primary id to the end of the URL. For Examample I want to URL to be "
http://gift-for-men.com/ "id?=1" The "id" is just text I added to make it look prettier instead of being
http://gift-for-men.com/1. The code I used to build the suggestions is below.
Explanation: The QUERY is stored in the variable $row. The primary key in the database is named id. Any ideas please?
Code: Select all
<?php
$query = "SELECT * FROM suggestions ORDER BY Rand() LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
?>
Re: URL Question
Posted: Fri Jul 23, 2010 12:49 pm
by Jade
You're going to have to do a check to see if there is an ID already and if not re-direct with an ID attached.
Code: Select all
<?php
//put this at the very top of the index page
if (!$_GET['id'])
{
//there was no ID found, randomly select one and redirect back to the index page
$result = mysql_query("SELECT id FROM suggestions ORDER BY Rand() LIMIT 1") or die (mysql_error());
$row = mysql_fetch_array($result);
header("Location: http://gift-for-men.com/?id=" . $row['id']);
exit;
}
//then wherever you're displaying the gift information you can do this
if ($_GET['id'])
{
$result = mysql_query("SELECT * FROM suggestions WHERE id='" . mysql_real_escape_string($_GET['id']) . "'") or die (mysql_error());
$row = mysql_fetch_array($result);
if (!$row['id']) //you know this id doesn't exist in your database (maybe it was a typo) so redirect back to the index page and try again
{
header("Location: http://gift-for-men.com/");
exit;
}
//otherwise you can display the gift information
print_r($row);
}
?>
Re: URL Question
Posted: Fri Jul 23, 2010 5:12 pm
by Thizzle
I'm getting this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/00/6453700/html/index.php:4) in /home/content/00/6453700/html/index.php on line 13
Thanks by the way.
Re: URL Question
Posted: Fri Jul 23, 2010 7:47 pm
by Thizzle
Alright I'm getting off so I thought I'd update you on where I'm at... I actually just started to learn PHP within the past few days when I had the idea for this website. Anyways, I went over your code and I couldn't exactly follow everything making the last few hours a waste trying to fix it. I think the route I was trying to go won't work so I need help getting started with the new program.
Basically as far as I can tell here is what I'm going to need to do: When someone visits the index.php (
http://gift-for-men.com/) a query is sent to the database. That data is then stored in an array like ($row). One of the data items is going to be the "id" which determines the page they are looking at. Thats just code, what I want is the user to be redirected to a URL (based on the id in query ) For example, gift-for-men.com/page?=1 (2, 500, 1000). (Note: the "page?" is just their to look pretty).
Now, let's say someone visits the URL of a page with an id... For example gift-for-men.com/page?=1. I want the pages to be static and not just randomly grab a row from the database as if they were visiting the index file.
Any help while I'm gone is useful.
Re: URL Question
Posted: Sat Jul 24, 2010 12:25 pm
by Thizzle
Based on what i want above I'm mapping out everything...
Let's see there are three possible scenarios.
Those scenario's are
1. Someone typed:
http://gift-for-men.com/ into the URL
2. Someone typed:
http://gift-for-men.com/page?=1 into the Url
3. Someone typed the wrong thing into the URL ( No page exist )
Based on those three things I need to wrote code for each of those possible scenario's in their respectable order.
1. Someone visits the index part of the website, they are then redirected to a random page.
2. Someone visits a page in the website and they are shown that page ( Meaning PHP Would need to check URL for the "id" and then pull the id's row and insert data from database into page. ( No <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> clue )
3. Someone typed wrong thing, they are then redirected to the index page and then is redirect to a random page.
I don't know, if anyone has any suggestions I would appreciate them.
Re: URL Question
Posted: Sun Jul 25, 2010 12:26 pm
by Thizzle
Back to work.
Re: URL Question
Posted: Sun Jul 25, 2010 2:07 pm
by califdon
This will sound cruel, but it has to be said: for a beginner, just learning PHP, an attempt to create a relatively complex web page like you describe is utterly pointless. There are a thousand things you have to learn first! Start out with something FAR simpler!
Re: URL Question
Posted: Sun Jul 25, 2010 3:45 pm
by Thizzle
Wow that's negative man but it's alright.
You know I think I understand the concept it's pretty simple I just need to learn the language. For example, I don't know any of the functions that would help me do this. Which is why I'm here, learning. Still, this is my goal with this website.
Re: URL Question
Posted: Tue Jul 27, 2010 11:52 am
by Jade
The error means you're trying to display header content and the re-direct to another page. You have to make sure that if you're going to redirect to another page that you don't have any blank spaces at the top of the page, that you've echoed something to the screen or displayed an HTML. The redirect must be the first thing that's output to the page because once the header goes out PHP cannot complete a redirect because the header has already gone out and it has to use that header to complete the redirect. The code I gave you above should work fine.
califdon wrote:This will sound cruel, but it has to be said: for a beginner, just learning PHP, an attempt to create a relatively complex web page like you describe is utterly pointless. There are a thousand things you have to learn first! Start out with something FAR simpler!
I have to disagree with you there. I think if you start off with something more challenging it may take you longer to complete it (and be a bit more frustrating) but you'll learn a helluva lot more in the long run.
Re: URL Question
Posted: Tue Jul 27, 2010 11:55 am
by Benjamin
Actually, you need to buy this book, and read it, and study it.
http://www.amazon.com/Programming-Colle ... 671&sr=8-1
Re: URL Question
Posted: Tue Jul 27, 2010 4:55 pm
by califdon
Jade wrote:I have to disagree with you there. I think if you start off with something more challenging it may take you longer to complete it (and be a bit more frustrating) but you'll learn a helluva lot more in the long run.
I suppose that I was pretty broad in my comment, and I grant you that there is something to be said for stretching to learn something complex, but I am biased by my years of teaching in college. In my experience, the greatest learning takes place when one learns in a progressive way, beginning with the basics and steadily expanding one's knowledge. I have seen all too many students think that they can tackle an advanced project without first understanding the fundamentals. They fail to really learn much useful skills, because there are always gaps in their understanding, so they constantly struggle to do anything productive. We may disagree and I respect your point of view, but I will always recommend beginning with simple projects and working upward to more advanced ones, because I've seen it work hundreds of times.
Re: URL Question
Posted: Wed Jul 28, 2010 11:52 am
by Jade
califdon wrote:In my experience, the greatest learning takes place when one learns in a progressive way, beginning with the basics and steadily expanding one's knowledge. I have seen all too many students think that they can tackle an advanced project without first understanding the fundamentals. They fail to really learn much useful skills, because there are always gaps in their understanding, so they constantly struggle to do anything productive.
I'm not discounting starting with the basics. Having a strong foundation is important and I'm happy to admit I learn something new at a basic level all the time even though I've been programming for over 10 years. I agree using a progressive learning style has it's benefits. However everyone learns differently and a progressive style isn't always feasible or practical for the situation at hand. Case in point, Thizzle may decide to hire an outside company or contract programmer to make the modifications for him instead of taking the time to change it himself. Either way that doesn't mean someone trying to accomplish something beyond their capabilities should be told
califdon wrote:an attempt to create a relatively complex web page like you describe is utterly pointless.
Re: URL Question
Posted: Wed Jul 28, 2010 12:41 pm
by califdon
@Jade: I accept your criticism. Sometimes I can be too rigid in my views. You are certainly correct in saying that everyone learns differently. I just hate to see someone waste their time trying to do a complex task without the framework of knowledge that makes it possible to understand what they are doing and why. Anyway, 'nuff said. Thanks for your honest criticism.