vchris 3 Posted July 5, 2006 Report Share Posted July 5, 2006 Ok so I have a mysql query that returns a link description (Google) and an address (www.google.com). What I want is have all the links displayed in 2 equal ul list when total # of links are even and when odd there would be 1 more in the first list.I know this involves a loop but I'm unsure exactly how to do that. Quote Link to post Share on other sites
aspnetguy 30 Posted July 5, 2006 Report Share Posted July 5, 2006 something like this //already have db data in 2 arrays $desc and $url$ul1 = '<ul>';$ul2 = '<ul>';for($i=0;$i<sizeof($desc);$i++){ if(($i % 2) == 0) { $ul1 .= '<li><a href="' . $url[$i] . '">' . $desc[$i] . '</a></li>'; } else { $ul2 .= '<li><a href="' . $url[$i] . '">' . $desc[$i] . '</a></li>'; }}$ul1 .= '</ul>';$ul2 .= '</ul>'; Quote Link to post Share on other sites
vchris 3 Posted July 5, 2006 Author Report Share Posted July 5, 2006 What about the first part, right after I queried the DB. How do you split it in 2 arrays? what about when there is only 1 link? Quote Link to post Share on other sites
justsomeguy 1,135 Posted July 5, 2006 Report Share Posted July 5, 2006 You would have to loop through the dataset and just push everything into arrays. But you wouldn't necessarily have to do that either. You can replace this:for($i=0;$i<sizeof($desc);$i++)with the dataset loop, and just skip the middle man: $ul1 = '<ul>';$ul2 = '<ul>';$i = 0;while ($row = mysql_fetch_assoc($result)){ if(++$i % 2) { $ul1 .= '<li><a href="' . $row['url'] . '">' . $row['desc'] . '</a></li>'; } else { $ul2 .= '<li><a href="' . $row['url'] . '">' . $row['desc'] . '</a></li>'; }}$ul1 .= '</ul>';$ul2 .= '</ul>'; Quote Link to post Share on other sites
vchris 3 Posted July 5, 2006 Author Report Share Posted July 5, 2006 This seems to make sense :)I'll try that maybe tonight if I have time.Thanks guys! Quote Link to post Share on other sites
aspnetguy 30 Posted July 5, 2006 Report Share Posted July 5, 2006 yeah that would be more efficient. Thanks Quote Link to post Share on other sites
vchris 3 Posted July 5, 2006 Author Report Share Posted July 5, 2006 Works great!Is there a way to place the code echoed by php just as it would be in html in the source? Because if you take a look at the code in view source, it's all screwed up because of the php. Quote Link to post Share on other sites
justsomeguy 1,135 Posted July 5, 2006 Report Share Posted July 5, 2006 Well, it's not screwed up, there just aren't any linebreaks. You don't need linebreaks anyway though. Most of the time the output I send to the browser is all on one line, except for javascript. If you want to add line breaks, just add a "\n" after each line, but it has to be a double-quoted string, not single-quoted: if(++$i % 2) { $ul1 .= "<li><a href=\"" . $row['url'] . "\">" . $row['desc'] . "</a></li>\n"; } else { $ul2 .= "<li><a href=\"" . $row['url'] . "\">" . $row['desc'] . "</a></li>\n"; } Quote Link to post Share on other sites
vchris 3 Posted July 5, 2006 Author Report Share Posted July 5, 2006 Thanks Quote Link to post Share on other sites
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.