wemustdesign Posted March 8, 2010 Share Posted March 8, 2010 I have some text in a database table. Each point has a new line: oranges lemons When I output this is want to make them into a list so they will output like <li>oranges</li> <li>lemons</li> How would I warap these variables in the <li> when outputted from the database? Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/ Share on other sites More sharing options...
aeroswat Posted March 8, 2010 Share Posted March 8, 2010 I have some text in a database table. Each point has a new line: oranges lemons When I output this is want to make them into a list so they will output like <li>oranges</li> <li>lemons</li> How would I warap these variables in the <li> when outputted from the database? echo "<li>" . $var . "</li>"; if ur reading from the database it should be in a loop $resultset = mysql_query("SELECT * FROM tblFruits"); while($row=mysql_fetch_array($resultset)) { echo "<li>" . $row['row_name'] . "</li>"; } Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023058 Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 I have some text in a database table. Each point has a new line: oranges lemons When I output this is want to make them into a list so they will output like <li>oranges</li> <li>lemons</li> How would I warap these variables in the <li> when outputted from the database? You have to alter the results to include <li> before and </li> after like so: $res = mysql_query("select * from table1"); if(mysql_num_rows($res) > 0){ $list = ''; while($fres = mysql_fetch_assoc($res)){ $list .= '<li>'.$fres['fruit'].'</li>'; } echo $list; } Or you can return '$list'. Never 'echo' within a loop though as it is far more flexible and extend-able to temporarily store the results in an array or variable. Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023060 Share on other sites More sharing options...
wemustdesign Posted March 8, 2010 Author Share Posted March 8, 2010 Sorry I don't think I was clear enough. The variables are not in seperate rows, they are in 1 field within a single row Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023061 Share on other sites More sharing options...
aeroswat Posted March 8, 2010 Share Posted March 8, 2010 I have some text in a database table. Each point has a new line: oranges lemons When I output this is want to make them into a list so they will output like <li>oranges</li> <li>lemons</li> How would I warap these variables in the <li> when outputted from the database? You have to alter the results to include <li> before and </li> after like so: $res = mysql_query("select * from table1"); if(mysql_num_rows($res) > 0){ $list = ''; while($fres = mysql_fetch_assoc($res)){ $list .= '<li>'.$fres['fruit'].'</li>'; } echo $list; } Or you can return '$list'. Never 'echo' within a loop though as it is far more flexible and extend-able to temporarily store the results in an array of variable. This is not true at all. For someone to print anything and only print something it makes 1000x more sense to loop through it and print the results out as they are fetched. Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023062 Share on other sites More sharing options...
aeroswat Posted March 8, 2010 Share Posted March 8, 2010 Sorry I don't think I was clear enough. The variables are not in seperate rows, they are in 1 field within a single row Then you will have to use the explode function. What is between each name? What's your delimiter? Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023064 Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 I have some text in a database table. Each point has a new line: oranges lemons When I output this is want to make them into a list so they will output like <li>oranges</li> <li>lemons</li> How would I warap these variables in the <li> when outputted from the database? You have to alter the results to include <li> before and </li> after like so: $res = mysql_query("select * from table1"); if(mysql_num_rows($res) > 0){ $list = ''; while($fres = mysql_fetch_assoc($res)){ $list .= '<li>'.$fres['fruit'].'</li>'; } echo $list; } Or you can return '$list'. Never 'echo' within a loop though as it is far more flexible and extend-able to temporarily store the results in an array of variable. This is not true at all. For someone to print anything and only print something it makes 1000x more sense to loop through it and print the results out as they are fetched. I'm not going to argue and that wasn't directed at you by the way. I just wrote that out as you typed up your's. But, I do completely disagree. To echo within a loop is the 'procedural' way. Say you enclosed this block of code within a function - you now have to change all your echo statements because you only want to return results, not echo them out as the function is run. Believe me, it's far more standard. as is using mysql_num_rows to check results which you didn't do - and only using "" where you want to parse php variables within strings, and indentation, and using mysql_fetch_assoc instead of mysql_fetch_array. Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023066 Share on other sites More sharing options...
wemustdesign Posted March 8, 2010 Author Share Posted March 8, 2010 I don't have a seperator at the moment, each variable is on a new line. I think it may be easier to include a seperator (such as |) thinking about it, I wasn't sure if you could some how distinguish a new line in a mysql field in php Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023069 Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 Sorry I don't think I was clear enough. The variables are not in seperate rows, they are in 1 field within a single row Sorry boot that As aeroswat said, you will have to use the explode() function to turn your string into an array, and then loop through that array: $string = 'fruit1, fruit2, fruit3'; $fruit_arr = explode(',', $string); foreach($fruit_arr as $fruit){ echo '<li>'.$fruit.'</li>'; #hehe } That's it. Hope that helps. Oh, you can separate by new line by using this explode in replace of above: $fruit_arr = explode('\n', $string); But, I must say, I've never done this so not sure if it would work. In principle it should. If not, just separate each item with something that will never feature within the text, sot he explode function doesn't split up something it shouldn't. I use | most of the time. Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023070 Share on other sites More sharing options...
aeroswat Posted March 8, 2010 Share Posted March 8, 2010 I don't have a seperator at the moment, each variable is on a new line. I think it may be easier to include a seperator (such as |) thinking about it, I wasn't sure if you could some how distinguish a new line in a mysql field in php You might be able to use the newline character as a delimiter however I highly recommend you don't because it could be very unreliable. Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023073 Share on other sites More sharing options...
aeroswat Posted March 8, 2010 Share Posted March 8, 2010 I'm not going to argue and that wasn't directed at you by the way. I just wrote that out as you typed up your's. But, I do completely disagree. To echo within a loop is the 'procedural' way. Say you enclosed this block of code within a function - you now have to change all your echo statements because you only want to return results, not echo them out as the function is run. Believe me, it's far more standard. as is using mysql_num_rows to check results which you didn't do - and only using "" where you want to parse php variables within strings, and indentation, and using mysql_fetch_assoc instead of mysql_fetch_array. By the way I believe, even tho PHP has OOP implementations, it is still mainly a procedural language. I didn't include mysql_num_rows check because I figured that was kind of obvious. The reason i included the setting of the result variable was to emphasize what $resultset was. Also I use mysql_fetch_array because it is the more flexible of the two and I believe you will find that many more people using the language out there use it as well. Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023075 Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 I'm not going to argue and that wasn't directed at you by the way. I just wrote that out as you typed up your's. But, I do completely disagree. To echo within a loop is the 'procedural' way. Say you enclosed this block of code within a function - you now have to change all your echo statements because you only want to return results, not echo them out as the function is run. Believe me, it's far more standard. as is using mysql_num_rows to check results which you didn't do - and only using "" where you want to parse php variables within strings, and indentation, and using mysql_fetch_assoc instead of mysql_fetch_array. By the way I believe, even tho PHP has OOP implementations, it is still mainly a procedural language. I didn't include mysql_num_rows check because I figured that was kind of obvious. The reason i included the setting of the result variable was to emphasize what $resultset was. Also I use mysql_fetch_array because it is the more flexible of the two and I believe you will find that many more people using the language out there use it as well. I see where you're coming from. But this OOP paradigm is here to stay. That's why php 5 was built with that in mind, and so is php 6. Just because something used by the majority doesn't make it correct. Majority of php developers are lazy who develop insecure, unscalable, unextendable code - that doesn't mean we should all be lazy and develop in a likewise manner. Again though, I completely see where you're coming from. But using this block of code within a function is NOT OOP at all as it could be used within a static class. That is still procedural, just more manageable and extend-able. Link to comment https://forums.phpfreaks.com/topic/194515-dynamic-list/#findComment-1023076 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.