Page 1 of 1

Inserting PHP into homelinks array

Posted: 2015-12-16 03:25
by TheNoLifer
Hi all,

I have written some horribly inefficient PHP, at the limit of my abilities, to select a random ID number from one of my tables, with the purpose of creating a URL which, when clicked, will take you to a random record in the table. I have it working in a test page, here - http://ada.abernyte.org/random.php

The code, for those interested, is;

Code: Select all

//connect to DB up here - not shown
$result = mysql_query("SELECT id FROM ContentItem ORDER BY RAND() LIMIT 1") or die(mysql_error());
if(is_resource($result) and mysql_num_rows($result)>0){
    $row = mysql_fetch_array($result);
//    echo $row["id"];
}
then in the html section;

Code: Select all

<p><?php echo "<a href='http://ada.abernyte.org/ContentItem_view.php?SelectedID=".$row['id']."'>Display a Random Content Item Record</a>" ?></p>
Now! I wish to add a new homelink to the main screen - something like this;
random_item_1.PNG
random_item_1.PNG (46.92 KiB) Viewed 3597 times
But I am having trouble inserting my $row['id'] code into the array which builds the homelinks;
homelinks_syntax.PNG
homelinks_syntax.PNG (6.83 KiB) Viewed 3597 times
I've tried many different combinations - I'm sure it's a syntax issue, but my php skills are poor and I'm not sure what I'm doing wrong. It most often just displays a white page when I try to add it.

Any ideas would be much appreciated!

Ally

Re: Inserting PHP into homelinks array

Posted: 2015-12-16 17:28
by TheNoLifer
Solved.

Syntax is;

Code: Select all

'url' => '/ContentItem_view.php?SelectedID='.$row[id].'',
Button is working. New ID is selected on each page reload.
random_item.JPG
random_item.JPG (22.25 KiB) Viewed 3581 times

Re: Inserting PHP into homelinks array

Posted: 2015-12-16 22:08
by a.gneady
Thanks for sharing your solution.
One little modification I'd recommend for more portable code is to remove the starting slash:

Code: Select all

'url' => 'ContentItem_view.php?SelectedID='.$row[id],
This would allow the link to work fine if your application is in a sub folder rather than the website root (and would still work in your case where the application is in the site root folder).