Jump to content

Help with explode()...


melting_dog

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.