Jump to content


Photo

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


  • Please log in to reply
6 replies to this topic

#1 ger_mac74

ger_mac74
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 09 March 2006 - 06:50 PM

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!!!

#2 k4pil

k4pil
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 09 March 2006 - 07:12 PM

have you tried to execute your query in your database.
If it is working fine in there then you where to look next.

#3 ger_mac74

ger_mac74
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 10 March 2006 - 12:00 PM

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

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 10 March 2006 - 02:06 PM

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++;
}

Legend has it that reading the manual never killed anyone.
My site

#5 ger_mac74

ger_mac74
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 10 March 2006 - 02:50 PM

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.

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 March 2006 - 03:27 PM

Since your making an ordered list, you should use the <ol> and <li> HTML tags:
<?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>';
?>
This way you don't have to worry about it.

Ken

#7 ger_mac74

ger_mac74
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 10 March 2006 - 03:45 PM

Thanks for that. Perfect now.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users