Syntax problem with search results

When the users searches in my app he will get back this:

  $('ul#doc').append(' <li><a href="appointments.php?id=' + this.id + '">' +this.name+' '+this.lastname+ '</a></li');    

This is a link where it leads to a profile…name and lastname come from the database and with this I access the data object of the success callback of an ajax request.

There are times though where name and lastname are null and i get back from the server coname(another db tabale field).
So…i try to do something like this when name,lastname are null and I get only coname from the server:

 $('ul#hair').append(' <li><a href="appointments.php?id=' + this.id + '">' + this.coname!==null?this.coname:this.name+''+this.lastname + '</a></li');    

The problem with the above is that the link destroyed…

How I could do it that when name,lastname are null coname will be displayed and still be that a link?

unless you use JSON (or any other serialisation format) for data transfer, you cannot get a NULL value.

But honestly, I’d solve that on the PHP/SQL side. let the script return only ID and Name and let PHP/SQL figure those out (you might find IFNULL() and CONCAT() (MySQL) useful as well).

I do use JSON…

And regarding php/mysql you have to provide me some more info…what do you have in your mind?

the same you want to do in JS, only much closer to the original data.

maybe something like

-- MySQL
SELECT
    id,
    IF(lastname IS NULL, 
        coname, 
        CONCAT(name, ' ', lastname)
    ) AS name
FROM
    ...

well I did not try exactly your code…I still got though what I wanted(as I see what’s printed in the browser)…what more your code offers from this:

SELECT   IFNULL( u.name,'') as name ,IFNULL(u.lastname,'')as lastname ,t.user_type,IFNULL (b.comp_name,'') as coname 
from users u...

I am confused a little by CONCAT…I know what it does of course.

the CONCAT() is used to reduce 2 items (name, lastname) into a single item, since the link only has one link text and thus there is no ambiguity in the JS code (for the JS code it doesn’t matter wich name you insert it only matters that there is something to insert).

How this could be applied to the code I showed you…IF this is possible of course?

this way.

as for SQL, I’ve already shown you an approach.

thanks…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.