ilkist Posted June 14, 2012 Share Posted June 14, 2012 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: Title1Title2Title3Title4Title5Title6 I've been trying with no luck to get it to look like this: Title1 Title2 Title3 Title4 Title5 Title6 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. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/ Share on other sites More sharing options...
scootstah Posted June 14, 2012 Share Posted June 14, 2012 So why not put the anchor in the loop too? while($row = $data->fetch()) { echo '<li><a href="#">' . $row['Title'] . '</a></li>'; } Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1353903 Share on other sites More sharing options...
ilkist Posted June 14, 2012 Author Share Posted June 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1353905 Share on other sites More sharing options...
scootstah Posted June 14, 2012 Share Posted June 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1353908 Share on other sites More sharing options...
ilkist Posted June 14, 2012 Author Share Posted June 14, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1353913 Share on other sites More sharing options...
scootstah Posted June 14, 2012 Share Posted June 14, 2012 Forgive my ignorance of ASP but if that code generates 6 anchored list items then it is an act of wizardry. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1353917 Share on other sites More sharing options...
ilkist Posted June 14, 2012 Author Share Posted June 14, 2012 Haha it does indeed do so =) check it out http://recenter.tamu.edu/ The list under "NewsTalk Texas" is the one in question. And if you click on each individual title, it takes you to the corresponding ClipNo on another page. That site is all in asp. I'm currently learning and converting the whole thing into php. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1353919 Share on other sites More sharing options...
scootstah Posted June 14, 2012 Share Posted June 14, 2012 I really cannot fathom how the code you posted would generate 6 anchored list items, unless the entire snippet is in a loop. I guess you'll have to wait until someone replies who knows the dark arts. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1353964 Share on other sites More sharing options...
Mahngiel Posted June 14, 2012 Share Posted June 14, 2012 Haha it does indeed do so =) check it out I'm calling your bluff. I know jack-squat about ASP, but in every other language you need a loop to perform similar actions against multiple objects. on ASP Loops There's got to be some sort of templating system you're passing that back through. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1353975 Share on other sites More sharing options...
ilkist Posted June 15, 2012 Author Share Posted June 15, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1354185 Share on other sites More sharing options...
Mahngiel Posted June 15, 2012 Share Posted June 15, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1354188 Share on other sites More sharing options...
ilkist Posted June 15, 2012 Author Share Posted June 15, 2012 I'll try that. Thanks for patiently putting up with me Now I just need to figure out how to loop my function >< I'm starting to think I don't need a function, but just a sophisticated list of variables. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1354189 Share on other sites More sharing options...
Mahngiel Posted June 15, 2012 Share Posted June 15, 2012 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>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1354204 Share on other sites More sharing options...
ilkist Posted June 15, 2012 Author Share Posted June 15, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1354216 Share on other sites More sharing options...
Mahngiel Posted June 15, 2012 Share Posted June 15, 2012 yup. don't forget to close your php correctly <ul id="newsTalk"><?php newstalkq(); ?></ul> Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1354220 Share on other sites More sharing options...
ilkist Posted June 15, 2012 Author Share Posted June 15, 2012 Weird thing is, I noticed that if I only have one line a ";" isn't necessary. Is that shunned upon in the PHP community? Hahah Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1354221 Share on other sites More sharing options...
scootstah Posted June 15, 2012 Share Posted June 15, 2012 A semi-colon isn't required in that case but I still put one there anyway. It's just habit I guess. Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1354223 Share on other sites More sharing options...
Mahngiel Posted June 15, 2012 Share Posted June 15, 2012 sort of line one-liner if()s <?php if($condition) echo 'this'; if($condition) { echo 'this'; } if($condition): echo 'this'; endif; $condition ? 'this' : 'that'; all sorta comes down to habits Quote Link to comment https://forums.phpfreaks.com/topic/264193-displaying-pdo-fetch-array-in-an-html-list-individually-hyperlinked/#findComment-1354224 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.