Jump to content

Archived

This topic is now archived and is closed to further replies.

ger_mac74

Article name, with article url as its underlying link not appearing

Recommended Posts

Hi there
I am working on a php script for a webpage that will grab two columns from a database (articlename and articleurl) and then number and display just the article name column. This article name column will hopefully appear as a link and when clicked on, the page will then divert to the articles url. Also I am new enough to php and programming so be easy on me!

Here is the code that I have so far:


<?php
$db = mysql_connect("----", "----", "----");
mysql_select_db("-------", $db);



$query = "SELECT articlename articleurl FROM articles";

$result = mysql_query($query) or die("Couldn't execute query");



?>


<?php
echo "Current List Of Articles: <br>";

$count = 1;

while ($row= mysql_fetch_array($result)){
$artname = $row["articlename"];
$arturl = $row["articleurl"];
echo "$count.) <a href='$arturl'>$artname</a><br>";
$count++;
}
?>


Can anyone see what I am doing wrong? At the moment the code seems to be counting the number of entries in the database but no articlename links seem to be appearing!!

Thanks in advance!!!

Share this post


Link to post
Share on other sites
have you tried to execute your query in your database.
If it is working fine in there then you where to look next.

Share this post


Link to post
Share on other sites
That's great. Working fine now. I had just left out a comma after articlename in my select query. When it is printing out now the article name is beside the number listed. This is what I want but when it gets to number 10.) it is one space further out the page compared to 9.) Is there a piece of code that I could fit in here to fix this and make 1 to 9 appear one space further out? Not too worried about what happens when it gets to 100. Looks like this at the moment:

8.) Text......
9.) Text......
10.)Text......
11.)Text......
12.)Text......

Not as obvious here but after 9 text appears out one space further because of extra digit.

Hope you can help me out.

Regards...

Share this post


Link to post
Share on other sites
[code]while ($row= mysql_fetch_array($result)){
$artname = $row["articlename"];
$arturl = $row["articleurl"];
if ($count<10) { echo " "; }
echo "$count.) <a href='$arturl'>$artname</a><br>";
$count++;
}[/code]

Share this post


Link to post
Share on other sites
Thanks for that. I looked at the manual and cant find what to do. I have tried putting in a letter instead of " " and the letter prints out fine which means the loop and count are working ok. It just doesnt seem to want to print an empty space. I also tried echoing "\s" and "&nbsp;" but neither work. Is there another system I should use to actually print a space or do I use a differnt function.
Thanks again.

Share this post


Link to post
Share on other sites
Since your making an ordered list, you should use the <ol> and <li> HTML tags:
[code]<?php
echo '<ol>';
while ($row= mysql_fetch_array($result)){
$artname = $row["articlename"];
$arturl = $row["articleurl"];
echo "<li><a href='$arturl'>$artname</a></li>";
}
echo '</ol>';
?>[/code]
This way you don't have to worry about it.

Ken

Share this post


Link to post
Share on other sites

×

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.