Navigation where all links the same but last

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Navigation where all links the same but last

Post by the9ulaire »

I'd like to make a navigation where it displays:

Code: Select all

while ($row = mysql_fetch_array($result)) {
	echo "<a href=\"index.php?p=" . $row['page_id'] . "\" title=\"" . $row['page_desc'];
	echo "\">";
	echo $row['page_name'];
	echo "</a>";
	echo " | ";
}
Except on the last one it leaves out the final echo, thus all links have a line between them and there isn't one hanging off the end.

Thank you for your time! I greatly appreciate it!
Luke
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would put the pipe at the beginning, then simply detect the fist iteration.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Post by the9ulaire »

What do you mean by 'pipe'? Sorry, I'm still quite new to all of this.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"|" is called a pipe (character.)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

If the result set is small, I would just stick the results into an array and make use of implode()

Code: Select all

$result = array();
while ($row = mysql_fetch_array($result)) {
  $result[] = "<a href=\"index.php?p=" . $row['page_id'] . "\" title=\"" . $row['page_desc'] ."\">". $row['page_name'] ."</a>";
}

echo implode('|', $result);
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Post by the9ulaire »

feyd wrote:"|" is called a pipe (character.)
Oh, of course! I knew that. Goodness, I certainly make things more complicated than they really are.
Jcart wrote:If the result set is small, I would just stick the results into an array and make use of implode()

Code: Select all

$result = array();
while ($row = mysql_fetch_array($result)) {
  $result[] = "<a href="index.php?p=" . $row['page_id'] . "" title="" . $row['page_desc'] ."">". $row['page_name'] ."</a>";
}

echo implode('|', $result);
Thank you! I tried that and it works perfectly; thanks!
aileenguan
Forum Newbie
Posts: 3
Joined: Sun Aug 05, 2007 10:52 pm
Location: P.R.C.

Post by aileenguan »

Jcart wrote:If the result set is small, I would just stick the results into an array and make use of implode()

Code: Select all

$result = array();
while ($row = mysql_fetch_array($result)) {
  $result[] = "<a href="index.php?p=" . $row['page_id'] . "" title="" . $row['page_desc'] ."">". $row['page_name'] ."</a>";
}

echo implode('|', $result);
Yes!
Post Reply