Page 1 of 1

Navigation where all links the same but last

Posted: Sun Aug 05, 2007 8:44 pm
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

Posted: Sun Aug 05, 2007 8:55 pm
by feyd
I would put the pipe at the beginning, then simply detect the fist iteration.

Posted: Sun Aug 05, 2007 9:02 pm
by the9ulaire
What do you mean by 'pipe'? Sorry, I'm still quite new to all of this.

Posted: Sun Aug 05, 2007 9:04 pm
by feyd
"|" is called a pipe (character.)

Posted: Sun Aug 05, 2007 9:20 pm
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);

Posted: Sun Aug 05, 2007 10:52 pm
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!

Posted: Mon Aug 06, 2007 12:20 am
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!