Jump to content

Displaying PDO Fetch Array In an HTML List (individually hyperlinked)


ilkist

Recommended Posts

I'm trying to figure out how to do this correctly.

 

Here's the  function I've come up with:

 

<?php
function newstalkq(){
	extract($GLOBALS);

	$data = $DBH->query('SELECT Title FROM Clippings ORDER BY PubDate DESC');
	$data->setFetchMode(PDO::FETCH_ASSOC);

	while($row = $data->fetch()) {
		echo $row['Title'];
	}
}
?>

<html>
<li><a href="/newstalk/#>"><?php newstalkq() ?></a></li>
</html>

 

The way that snippet looks is something like this:

 

 

I've been trying with no luck to get it to look like this:

 

Although, I can get it to list out like that by adding the <li></li> tags around the $row['Title'] line inside the while loop,

but that just makes the whole list into one giant hyperlink. What I want to happen is have each Title on their own line

and each having a separate hyperlink.

 

 

Link to comment
Share on other sites

So why not put the anchor in the loop too?

while($row = $data->fetch()) {
echo '<li><a href="#">' . $row['Title'] . '</a></li>';
}

 

I'm sure that will work for now, but ultimately I want to have the function setup to where I can use it like this:

<li><a href="/newstalk/#<?php newstalkq('ClipNo)' ?>"><?php newstalkq('Title') ?></a></li>

 

So that each Title is linked to its designated ClipNo. If I could figure out how to get the listing done correctly,

then I'll move on to the next step.

Link to comment
Share on other sites

For starters, your function does not accept any parameters, and echo's all of the rows returned. If you want the markup for "each" row returned then you need to put the markup into the loop for "each" row. It's that simply, and there's no other way.

Link to comment
Share on other sites

I know, as of now, it doesn't accept any parameters, but don't worry because I'll change that as soon as I get this figured out. It's just mind boggling since my site is currently in the old .asp, and I could it to work easily with that. Since trying to pickup on php, i've run into tons of problems.

 

Here's what I had

<%
Set rstMN = Server.CreateObject("ADODB.RecordSet")
rstMN.ActiveConnection = SQLSrvConn2

rstMN.Source = "SELECT TOP 6 Clippings.ClipNo, Clippings.Title FROM Clippings WHERE Clippings.Edited=1 AND Clippings.InActive=0 ORDER BY Clippings.PubDate DESC"

rstMN.CursorType=adOpenStatic
rstMN.LockType=adLockReadOnly
rstMN.Open
%>

<html>
<li><a href="/newstalk/#<%= rstMN("ClipNo") %>"><%= rstMN("Title") %></a></li>
</html>

Link to comment
Share on other sites

oh dear how silly of me, I forgot this part of the code after the html <li> tags ><

 

<%
rstMN.MoveNext
Loop
rstMN.Close
Set rstMN = nothing
%>

 

Wasn't trying to frustrate anyone =P

 

 

So the whole snippet looks like this:

 

<%
Set rstMN = Server.CreateObject("ADODB.RecordSet")
rstMN.ActiveConnection = SQLSrvConn2

rstMN.Source = "SELECT TOP 6 Clippings.ClipNo, Clippings.Title FROM Clippings WHERE Clippings.Edited=1 AND Clippings.InActive=0 ORDER BY Clippings.PubDate DESC"

rstMN.CursorType=adOpenStatic
rstMN.LockType=adLockReadOnly
rstMN.Open
%>

<html>
</body>
<div class="col380r"> <!-- may not need class here -->
	<a href="/newstalk/"><img src="images/ntt_home_logo.jpg"  class="logo" alt="NTT logo" /></a>
	<ul id="newsTalk">
<%
Do While Not rstMN.EOF
%>
		<li><a href="/newstalk/#<%= rstMN("ClipNo") %>"><%= rstMN("Title") %></a></li>
<%	
rstMN.MoveNext
Loop
rstMN.Close
Set rstMN = nothing
%>
	</ul>
</div>
</body>
</html>

 

So you are correct about me having a loop in there, thanks for reminding me =) I guess I skimmed over that part yesterday and it slipped my mind.

Link to comment
Share on other sites

The concept remains the same for PHP.  You'll need to loop through each of your results in order to output the links like scootah showed you in post 2.

 

If you have a column for Title and one for Link it just becomes

[tt]<li>

    <a href="/newstalk/#<?php echo $row['Link']; ?>">

        <?php echo $row['Title'];  ?>

    </a>

</li>

Link to comment
Share on other sites

Now I just need to figure out how to loop my function >< I'm starting to think I don't need a function

 

The use of a function is determined by it's purpose.  I personally like functions a lot. I would do something like this:

<?php
function newstalkq( $limit = 10 ){ // give the function a default for S&G's
$data = $DBH->query('SELECT `Title`, `Link'  FROM `Clippings` ORDER BY PubDate DESC LIMIT $limit ');
return $data->setFetchMode(PDO::FETCH_ASSOC);
}

// retrieve headlines
$newstalk_headlines = newstalkq( 6 );
// you can either use this now, or pass it along to another object / function / template

//verify headlines
if( $newstalk_headlines ) {
foreach( $newstalk_headlines as $headline ) {
	echo '<li>< a href="newstalk/#' . $headline['Link'] . '">' . $headline['Title'] . '</a>';
}
}

Link to comment
Share on other sites

I tweaked my original function a bit, implemented some of your ideas, and here's what I wrote:

 

<?php
function newstalkq(){
	extract($GLOBALS);

	$data = $DBH->query('SELECT ClipNo, Title FROM Clippings ORDER BY PubDate DESC LIMIT 6');
	$data->setFetchMode(PDO::FETCH_ASSOC);

	while($row = $data->fetch()) {
		echo '<li><a href="/newstalk/#'.$row['ClipNo'].'">' . $row['Title'] . '</a></li>';
	}

	$DBH = null;
}
?>

<html>
<body>
			<div class="col380r"> <!-- may not need class here -->
				<a href="/newstalk/"><img src="images/ntt_home_logo.jpg"  class="logo" alt="NTT logo" /></a>
				<ul id="newsTalk"><?php newstalkq() ?></ul>
			</div>
</body>
</html>

 

It's simple, and it works exactly the way I want it to without me having to change anything but the database to update the list =)

Thanks for your help! I really do appreciate it.

Link to comment
Share on other sites

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.