Hello

I wonder if anyone could help me with the following please? I have scratched my head and played around but not been able to move forward.

I am creating a script to redirect a user to a random link on my site.

The first part of the code extracts the required fields from the database:

// Select random rows from the database
$result = mysql_query ("SELECT A.PID, A.gtitle, A.p1, A.price, B.name, B.seo FROM posts A, categories B WHERE A.category=B.CATID and A.active='1' ORDER BY RAND() LIMIT 1"); 

// For all the rows that you selected
while ($row = mysql_fetch_array($result))

I shuld tidy this code up as p1, price and name are not used - I was reusing another query.

I have then tried to print the result to the screen to check I can construct the url correctly.

I am using the following:

echo "<a href=http://www.mysite.com/\"" . $row["seo"] . $row["PID"] . $row["gtitle"] . "\">
Random Link
</a>" ;

This outputting the following link:
http://www.mysite.com/%22seoPIDgtitle

whereas it should be:
http://www.mysite.com/seo/PID/gtitle

So that is my first question - how do I get the format correct?

The second is that gtitle is only showing as one word whereas in the database it is more than one. For example 'replace text with buttons' is appearing as 'replace' - I don't understand why this?

In addition for the url I need gtitle rather than looking like 'replace text with buttons' to appear as replace-text-with-buttons' - not sure how to do this.

Finally, once I can construct the url correctly, I want to redirect to that url - the code I was going to use was:

header("Location: http://www.mysite.com/seo/PID/gtitle");

but again not sure how to achieve this or indeed if this is the best way to redirect after pulling a random link.


Sorry for so many questions - I was trying to break it down into logical steps and the echo part was really for testing puposes but was useful to show the problem with gtitle and the fact that I can't construct a URL properly.

Really grateful for any help or advice and thanking you all in advance.

Regards
Mark

Your whole format is wrong (missing " " and closing them too early etc). If you use " " for your echo statement it is much easier to format. Try this:

echo '<a href="http://www.mysite.com/'" . $row['seo'] . "'/'" . $row['PID'] . "'/'" . $row['gtitle'] . "'">
Random Link
</a>"';

Not sure why you are only getting the first word of the gtitle but to re-format use strreplace:

$row['gtitle'] = strreplace(" ", "-", $row['gtitle']);

For the header redirect you need to assign the created link to a variable, not just echo it out and then use that variable in your header redirect:

$link = // your link code here as I have reformatted above;
header("Location: {$link}");

THank you very much for this - I am working my way through it.

I copied the code

echo '<a href="http://www.mysite.com/'" . $row['seo'] . "'/'" . $row['PID'] . "'/'" . $row['gtitle'] . "'">Random Link</a>"';

but am getting this error:

Parse error: syntax error, unexpected '"', expecting ',' or ';'

Tried adjusting but making a mess - any quick help appreciated

Mark

Sorry I left an extra " at the end of the echo statement.

Change to:

</a>';

Sorry - still producing the same error

Parse error: syntax error, unexpected '"', expecting ',' or ';'

Thanks
Mark

Please put your row values back into " ", so

$row["seo"]

Sorry, I rushed this a bit when I replied to you.

Please put your row values back into " ", so

$row["seo"]

Sorry, I rushed this a bit when I replied to you.

Thanks - must be me but I am stil getting an error:

Parse error: syntax error, unexpected '"', expecting ',' or ';'

using

echo '<a href="http://www.mysite.com/'" . $row["seo"] . "'/'" . $row["PID"] . "'/'" . $row["gtitle"] . "'">Random Link</a>';

Sorry to be a nusiance.

Regards
Mark

Is the error definitely on this line?
I have also missed an underscore from the str_replace:

$row['gtitle'] = str_replace(" ", "-", $row['gtitle']);

Hi

It is from the first line as I have only used that line so far.

Regards
Mark

I have tested this one:

echo '<a href="http://www.mysite.com/' . $row['seo'] . '/' . $row['PID'] . '/' . $row['gtitle'] . '">Random Link</a>';

Thank you - that works :)

I will have a go at the other elements now and try not to bother you.

Regards
Mark

Thank you very much - all working perfectly now :)

Thanks again
Mark

No problem - sorry for my initial rushing and errors :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.