Jump to content

Recommended Posts

Hi, I have extremely limited PHP knowledge, so I apologise in advance.

 

I want to create a list of recent articles posted to this site: http://www.f1times.co.uk

 

I have managed to create a list of title, and url's - http://www.f1times.co.uk/titles.php listed in this file, and managed to limit it to 20 results, and order them by DESC, this is where my knowledge ends.

 

What I want to now do, is make it so the titles are links to the articles. Which means somehow making the titles link to the url next to them. However my problem doesn't end their, the URL's are just the titles separated with dashes (-) I need to add a dash between that and the unique id which is the number, then add .html on the end.

 

Example, this is what I have - http://www.f1times.co.uk/titles.php

 

I want to turn that into this: <a href="http://www.f1times.co.uk/fota-seek-advice-on-2010-breakaway-125.html">FOTA Seek Advice On 2010 Breakaway</a> - without the http://www.f1times.co.uk/ part, which I only added for the example.

 

Does this make any sense?

 

The code I have so far:

<?php
$con = mysql_connect("localhost","private","private");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("f1times_articles", $con);

$result = mysql_query("SELECT * FROM n2s_article ORDER BY article_id DESC LIMIT 0,20");

while($row = mysql_fetch_array($result))
  {
  echo $row['article_title'] . " " . $row['article_url'] . " " . $row['article_id'];
  echo "<br />";
  }

mysql_close($con);
?>

 

Thanks to anyone who can help.

Hope this helps

ps not tested

<?php
$con = mysql_connect("localhost","private","private");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("f1times_articles", $con);

$result = mysql_query("SELECT * FROM n2s_article ORDER BY article_id DESC LIMIT 0,20");

while($row = mysql_fetch_array($result))
  {
    $fileurl = $row['article_url']."-".$row['article_id'].".html";
    echo "<a href='<?= $fileurl; ?>'>$row['article_title']</a>";
    // if(file_exists($fileurl)){echo "<a href='<?= $fileurl; ?>'>$row['article_title']</a>";}else{echo "NO FILE AVAILABLE";} //<- Try this to check to make sure the file exists.
    echo "<br />";
  }

mysql_close($con);
?>

You don't have to use

<?=  $fileurl; ?>

 

Since you're already within a PHP code block.  You also need to encase any array value you want to be parsed within a string (in this case, $row['article_title']) in curly braces.  So, with all that said, simply use:

 

$url = $row['article_url'] . "-" . $row['article_id'] . ".html";
echo "<a href='$url'>{$row['article_title']}</a>";

Thanks for the help, I tried both but get an error:

 

Parse error: syntax error, unexpected $end in /home/f1times/public_html/titles.php on line 21

 

Using this code:

<?php
$con = mysql_connect("localhost","private","private");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("f1times_articles", $con);

$result = mysql_query("SELECT * FROM n2s_article ORDER BY article_id DESC LIMIT 0,20");

while($row = mysql_fetch_array($result))
  {
    $url = $row['article_url'] . "-" . $row['article_id'] . ".html";
    echo "<a href='$url'>{$row['article_title']}</a>";
    // if(file_exists($fileurl)){echo "<a href='<?= $fileurl; ?>'>$row['article_title']</a>";}else{echo "NO FILE AVAILABLE";} //<- Try this to check to make sure the file exists.
    echo "<br />";
  }

mysql_close($con);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.