melting_dog Posted December 19, 2011 Share Posted December 19, 2011 Hi all, I posted a similar question before and got some of the way but still need a little help. I have a product database with a description column where there are strings broken into lines. I need to find a way to explode() by line break. Currently explode("/n") doesn't work - it only returns one array item. Can anyone help me out? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/ Share on other sites More sharing options...
trq Posted December 19, 2011 Share Posted December 19, 2011 Firstly, you shouldn't be storing multiple values in a single database field in the first place. Secondly, explode requires at least two params. A delimiter and the string to split. Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1299285 Share on other sites More sharing options...
Ivan Ivković Posted December 19, 2011 Share Posted December 19, 2011 Like thorpe said, you shouldn't be storing multiple variables as one row. $data = explode('\n', $fetch['description']); Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1299292 Share on other sites More sharing options...
litebearer Posted December 19, 2011 Share Posted December 19, 2011 Another point of view... we are presuming that the content is individual, separate 'content'; perhaps (as per its name) the strings are paragraphs, which would indicate that exploding is not really the objective. Rather that formatting the output may be the objective. Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1299293 Share on other sites More sharing options...
Ivan Ivković Posted December 19, 2011 Share Posted December 19, 2011 And if you really have to seperate variables somehow, don't use new line or blank space. Use '/' or '_'. Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1299294 Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 I need some help with this too. It works great, but I am getting annoying errors (although they do not effect functionality). I am trying to use the existing "SELECT * FROM product WHERE " . implode(' OR ', $searchWord); Which works great, without fault, but... I want to order it too. This is what I came up with: $query = "SELECT * FROM thumbs WHERE " . implode(' OR ', $searchTerm) . "ORDER BY uploaddate DESC"; $result = mysql_query($query); if(mysql_num_rows($result) == 0) { echo "Your search query did not return any results"; } But if it does not return anything, then I get the error: "Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\melloorr\search.php on line 97 Your search query did not return any results" As I have said, the error does not effect anything but it is unsightly. Is there any way around this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1299493 Share on other sites More sharing options...
Drummin Posted December 19, 2011 Share Posted December 19, 2011 Are you using both $searchWord and $searchTerm? Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1299527 Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 Are you using both $searchWord and $searchTerm? Nope, I am just using $searchTerm Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1299529 Share on other sites More sharing options...
melting_dog Posted December 24, 2011 Author Share Posted December 24, 2011 Hi guys, Thanks for all the help! Yep I fully agree that it's not good practice to store data this way - I certainly wouldn't do it. Unfortunately I am working with a clients pre-existing database so dont have much choice in the matter. Thanks for all the input but I still can't get it to work. I have: $data = explode('\n', $row['description']); echo '<span class="productdescription">'. $data . '</span>'; But all this returns is the word 'Array' Does anyone know how to improve this? Do I need to loop through this array? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1301054 Share on other sites More sharing options...
PFMaBiSmAd Posted December 24, 2011 Share Posted December 24, 2011 Why do you even need to explode the data? It appears that you want to display the entire description. You should likely look at using nl2br when you output the description to give you html <br> tags. Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1301056 Share on other sites More sharing options...
melting_dog Posted December 24, 2011 Author Share Posted December 24, 2011 Haha thanks PFMaBiSmAd! I came to the same conclusion too! and thats what I am running with: echo '<span class="productdescription">'. nl2br($row['description']).'</span>'; This works absolutely fine! I was initially looking at explode() as I wanted to loop the strings through a <ul> but this will do nicely Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1301061 Share on other sites More sharing options...
AGuyWithAthing Posted December 24, 2011 Share Posted December 24, 2011 Firstly, you shouldn't be storing multiple values in a single database field in the first place. Secondly, explode requires at least two params. A delimiter and the string to split. Actually, entirely incorrect. I and many others often store JSON or other serialized sets of data in fields in databases as the data gets modified regularly and therefore the database requires no modification when new code roll outs are made. It's a very robust way to store ever changing sets of data and to maintain clean and simple databases to prevent error and allow for efficient flexibility. The last thing I used it on was a browser state recorded where headers, referrers, cookies and various browser information which changed frequently. Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1301064 Share on other sites More sharing options...
trq Posted December 24, 2011 Share Posted December 24, 2011 You are kidding right? And how exactly do you propose to search through this data? You can't, not efficiently anyway. There are better ways of storing key value pairs. as the data gets modified regularly and therefore the database requires no modification when new code roll outs are made. You need to learn to handle these code rollouts better. I work for a company that has over a dozen applications. We make database changes on an almost daily basis. There are ways of managing this easily. Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1301206 Share on other sites More sharing options...
AGuyWithAthing Posted December 25, 2011 Share Posted December 25, 2011 You are kidding right? And how exactly do you propose to search through this data? You can't, not efficiently anyway. There are better ways of storing key value pairs. as the data gets modified regularly and therefore the database requires no modification when new code roll outs are made. You need to learn to handle these code rollouts better. I work for a company that has over a dozen applications. We make database changes on an almost daily basis. There are ways of managing this easily. No I am not, I work for a software house which manages large systems and this is a technique which is used frequently (mostly in application configurations). Mistakes are made by everyone and this is one way to cull mistakes. I am not saying this is perfect for every solution, I'm just saying telling people this is a bad technique is wrong as everything can serve its purpose. Some of the most efficient and reliable solutions can often be the easiest. Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1301212 Share on other sites More sharing options...
trq Posted December 25, 2011 Share Posted December 25, 2011 So again, how exactly do you propose to search through this data? Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1301286 Share on other sites More sharing options...
AGuyWithAthing Posted December 26, 2011 Share Posted December 26, 2011 I've never had to search an application config. Also, MySQL has this function called LIKE and REGEXP which allows searching of strings which I wouldn't use if this had searchable properties as it is not efficient but is more efficient for fast loading and dumping of data sets. Quote Link to comment https://forums.phpfreaks.com/topic/253467-help-with-explode/#findComment-1301481 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.