Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with if

Status
Not open for further replies.

MichaelHooker

Programmer
Mar 17, 2006
70
GB
The last three times I have started drafting a question here I've thought of the answer before I got to the end, so here's hoping.

This has me baffled. I take a row of data out of the MySQL database and put it together into a string, like so:
Code:
$details = $row[4] . ' ' . $row[5] . $row[6] . ' ' . $row[7] . ' [' . $row[8] . ']';
Note that square brackets are inserted around $row[8]. This creates a nicely-formatted string in the standard style for this type of data.

$details is later inserted into a standard html data table cell.

Sometimes, there is no data to retrieve, the $row array is empty and so $details consists only of the square brackets, as '[]'. In that case I would like to display '---' in the table cell instead. So please will someone tell me why the following substitution does not work?
Code:
if ($details == '[]') {
         $details = '---' ;
   }
"[]" stubbornly remains "[]". I have checked very carefully for unwanted spaces (like "[ ]" or " [ ]" but can't find any.

I have thought of a work-around, which works:
Code:
   if ($row[4] == '') {
         $details = '---' ;
   }
but I'd still like to know why the first code does not work. Is it not possible to reassign the value of a string used in the if test? I do this all the time in Delphi :)

Many thanks

Michael Hooker
 
If I'm understanding this right, your variable $details has the contents of several other variables that come from a database.

So I'm assuming those aren't empty even if $row[8] is empty.

Since they aren't empty, $details is not equal to just 2 brackets '[]'. but has the values from the other $row[] variables as well.

They would all have to be empty, and even then, you have spaces, so: "[red]____[red][][red]__[red]" is not equal to just "[]".








----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi & thanks.

All the $row[] variables are empty because the sql query found no data to return - there was no match.

I agree that the concatenation process inserts spaces but "[]" is exactly what gets printed in the table cell when there is no match. I experimented by adding "#" to the beginning and end of $details. This results in "# []#" appearing in the table cell. One space only (I just copied and pasted it). But trying:
Code:
if ($details == '# []#') {     // one space
         $details = '---' ;
   }
still didn't work. I then tried:
Code:
if ($details == '#  []#') {      // three spaces
         $details = '---' ;    
   }
and it did. So basically the problem is me believing what the html was putting in the table cell and not my own concatenation process :)

I then took out the hashes:
Code:
if ($details == '  []') {      // three spaces
         $details = '---' ;    
   }
and guess what? The php refused to compile, complaining that the '[' was unexpected! <sigh>

I think I may do this a neater way anyway now: test $row[4] first, if it has a value create $details from the array else assign it as "---".

Thanks again.

Michael Hooker
 
HTML is white space agnostic, that means all white spaces are converted to a single space when displayed. However your string still has spaces even if they aren't shown.

The php refused to compile, complaining that the '[' was unexpected!

Not possible given your code, this likely is caused by something else.

Additionally, are you sure all $row[] variables will be empty? I mean is it not possible to have a row that has some of the fields filled in but not all of them?









----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks.

Not possible given your code, this likely is caused by something else.

What sort of something else? All I did was delete the hashes. Doesn't really matter any more though - that bit of code is history.

is it not possible to have a row that has some of the fields filled in but not all of them?

If it is, you'll need to explain to me how. The $row array is filled with data returned from a MySQL select statement which looks for a unique ID. If it finds the unique ID, then data is returned and $details is created from the data. If the unique ID does not exist then surely all the $row array elements will be empty because no data has been returned?

There are so many ways of doing things it's very hard for someone like me to know what's best. It is probably possible to create the concatenated item within the SQL statement, but frankly I don't like long ugly SQL queries :). Or I could test the number of rows returned. It can only be 0 or 1. There is probably an even easier way of telling that no data has been found. I have spent years either letting the Borland BDE, Delphi data-are controls & string processing do all my database work or just working with csv data and string lists, surprisingly fast in the right circumstances. Now I have to have a database out on the internet and the learning curve is steeper than my diminishing brain cells can cope with [sad]. html, php, PEAR, MySQL, javascript, AJAX...

Thanks again

Michael Hooker
 
quote]There is probably an even easier way of telling that no data has been found
[/quote]
There is, since you say you are using MYSQL, you can check either the number of rows returned like you say, or just check the variable returned by the mysql_query() function. If its false no data was returned.


If it is, you'll need to explain to me how. The $row array is filled with data returned from a MySQL select statement which looks for a unique ID. If it finds the unique ID, then data is returned and $details is created from the data.

Well I don't know how data is inserted into your DB, or what kind of data is being handled there, so I don't know if its possible or not, that's why I'm asking you.

Say your data handles, Oh I don't know, personal info maybe, and your table looks like this:

id name phone age
1 John 554321 25
2 Paul 521421
3 Peter 212141 32
4 Jane 654212 45


Well what do you know, Paul forgot to fill in his age, its empty, that does not automatically mean the entire row is empty. Just that a particular field, in this case age is missing for that row.

I would still want to display that row, and maybe "add three dashes to indicate its missing".

It call comes down to checking your data.

Just so you know, I have developed in Delphi as well, not very extensively but I have, and I still checked what the database queries where returning if they where returning anything at all.

None of us Knew what the best way to do things was when we started, but that's why we ask and that's why places like this exist.












----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Well what do you know, Paul forgot to fill in his age, its empty, that does not automatically mean the entire row is empty. Just that a particular field, in this case age is missing for that row.

I would still want to display that row, and maybe "add three dashes to indicate its missing".

Ah... No, that would not work at all in this context. Yes, if we have a row with data we want to display it but $details would look silly with dashes in it. It is perfectly normal for some of the fields to be empty. Imagine that 4 fields are first name, middle names and surname. So...

"John" + " " + "Henry" + " " + "Arthur" + " " + "Smith"

If someone does not have any middle names we do not want their name to appear as "John --- --- Smith".

In my application it is a bit more complicated, but $details is still simply a piece of textual data created from the fragments in the available data fields, and the system is carefully designed so that it complies with normal formatting for this sort of data when put together. There is also a good deal of input error checking to make sure that the elements which go to make up $details will fit together the way we want, and a version of $details is created at that point and displayed to the user (me) for confirmation/correction before its constituent parts go into the actual table. In other circumstances all my database fields have a default value of "---" guaranteed by error-checking at the input stage. But not in this particular case, where we want deliberately empty fields to be invisible. Indeed, as you pointed out earlier, the effect of html is to make extra spaces disappear as well, so any double spaces appear as one and for once this is exactly what we want.

So hopefully now you will understand that there are very good reasons in this case to have empty fields imported into $details.

You may be wondering "Why enter these elements as separate fields, and put them together later?" Well, go back to the names. Supposing you need to be able to search/sort on the middle name(s)? That's the sort of thing I need to do with my data. The elements come in useful in their own right.

Michael Hooker

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top