The14thGOD Posted February 13, 2009 Share Posted February 13, 2009 First off, I'm not sure where to put this but I think it belongs more so here than in any of the other forums. I've built a website using PHP and MySQL as the back-end and the firm designed the stories into 2 columns. The problem I'm having is how to display it nicely. There are 2 ways it is displayed. On the home page only about 130ish words are displayed (followed by a read more button). I originally set it up so it would put X amount of words in column 1 and then the rest in column 2. However there is a story that has 1 word on a new line and then the words start up at the top of the next column. On the page that corresponds to the story itself I just basically cut the article in half. Here's the code: <?php $words = explode(' ',$row['body']); for($i=0;$i<84;$i++) { $td1 .= $words[$i]." "; } for($i=84;$i<134;$i++) { $td2 .= $words[$i]." "; } echo "<td><p>$td1</p></td>\n"; echo "<td><p>$td2</p>\n"; ?> This is just the code for the home page that displays the first 134 words. The code for the other pages is basically the same. Anyone have any ideas? Thanks for any and all help, Justin Quote Link to comment https://forums.phpfreaks.com/topic/145077-dynamic-2-column-layout/ Share on other sites More sharing options...
printf Posted February 13, 2009 Share Posted February 13, 2009 Why not just do this with the database, then you don't have to add all kinds of silly PHP based hacks. You have many options of how the database can return data, like by index, sub string, character stop. I just don't see the need of processing data twice, when the database can do it most times much faster than you can script! Quote Link to comment https://forums.phpfreaks.com/topic/145077-dynamic-2-column-layout/#findComment-761297 Share on other sites More sharing options...
The14thGOD Posted February 13, 2009 Author Share Posted February 13, 2009 I didn't know I could do that in the query. The main concern though is getting the content to flow in 2 columns well. So it reaches close to the end of column rather than 1 word on a new line and then jumping to the next column. Here's an image demonstrating what I mean: http://the14thgod.com/misc/2columns.jpg On a side note If you don't mind could you provide an example of how to do that in the query? Would be great for future reference =D Thanks, Justin Quote Link to comment https://forums.phpfreaks.com/topic/145077-dynamic-2-column-layout/#findComment-761325 Share on other sites More sharing options...
printf Posted February 13, 2009 Share Posted February 13, 2009 A database is really smart, you can setup pagination by just telling the database how much text you want per page. You can use an array of STOP CHARACTERS that tell the database where to start and stop and all kinds of other things. Its all done at the engine level, not the scripting level so it's fast and easier to manage your code base.. Lets say for example you wanted to return 132 words... (simple) SELECT SUBSTRING_INDEX(data, ' ', 132) AS data FROM table WHERE article_id = some_id; Let's say you want to return paragraphs that split by "\r\n\r\n", then to return (3) paragraphs SELECT SUBSTRING_INDEX(data, '\r\n\r\n', 3) AS data FROM table WHERE article_id = some_id; That's just one function, there are so many more that allow you to format, equally split by ratio or other methods to return data formatted exactly how you want it or need it. A database isn't just storage system, it's also a super text processing, ultra smart mathematical engine too! Sure there are some limitation, but most of those are developer related. Quote Link to comment https://forums.phpfreaks.com/topic/145077-dynamic-2-column-layout/#findComment-761351 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.