0

The title is a bit confusing I guess but this is what I need:

I have a jquery autocomplete textfield and it gets its data from a database. Everything works, I can type a part of a zipcode in the textfield and when that part exists in the database it shows all available options together with the city, state etc.

But what I want is to show the flag of the country (as an image) in the list of options. I made a new row: county (which contains the url to an image file of the flag)

And this is where it goes wrong:


$rs = mysql_query('select zip, city, county, state from zipcode where zip like "'. mysql_real_escape_string($_REQUEST['term']) .'%" order by zip asc limit 0,10', $dblink);

$data = array();
if ( $rs && mysql_num_rows($rs) )
{
while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
{


    $data[] = array(
        'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] .' '. <img src='http://www.colinch.com/fut/$row['county'].'' />  ,
        'value' => $row['zip']
    );
}
}

I'm not a php expert so I think it's just a simple thing that went wrong. When I remove the < img src="......." part it will just print the $row['county'] which looks like this: USA.png

I hope someone can help me!

Thanks in advance

4
  • you should use a good text editor that color codes for you depending on the language. NotePad++ is a good free one. Commented Feb 28, 2013 at 13:33
  • Thanks for the tip! and I also think a lot of people want to know how to do a similar thing like this so I hope this will help for them Commented Feb 28, 2013 at 13:49
  • also for your case, check out Why shouldn't I use mysql_* functions in PHP? Commented Feb 28, 2013 at 13:53
  • Oh I didn't know that, I'm not good with php yet so what would be an alternative thing to use ? Commented Feb 28, 2013 at 14:22

5 Answers 5

2

You Quotes are totally wrong:

    'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] .'<img src=\'http://www.colinch.com/fut/'.$row['county'].'\' />',
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

$data[] = array(
        'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] .' <img src="http://www.colinch.com/fut/'.$row['county'].'" />'  ,
        'value' => $row['zip']
    );

Comments

0

Try this

  $data[] = array(
        'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] .' '. '<img src="http://www.colinch.com/fut/'.$row['county'].'"'.'/>'  ,
        'value' => $row['zip']

Comments

0

You've put the quotes in a wrong way. That's why the syntax highlighters are so helpful. Take a look at the code you wrote in your question. It got marked by SO and dark red / brown colors mark where the text is. You want <img... to be put into a text! Or even better, if possible, echo the stuff directly into html like so:

foreach($countries as $code)
{
    ?><img src="<?php echo $flag; ?>.png/><?php
}

If you decide that you don't want to / cannot do it this way then your code should look like this:

'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] .' <img src=\'http://www.colinch.com/fut/'.$row['county'].'\' />'

P.S. I would advise using " for html quotes and ' for php quotes. This way you will not need any escaping :)

Comments

0

This may be easier to read

$data[] = array(
        'label' => "{$row['zip']}, {$row['city']} {$row['state']} <img src='http://www.colinch.com/fut/{$row['county']}' />"  ,
        'value' => $row['zip']
    );

The use of double quotes allows you to evaluate the variables used without escaping the string.

Your issue lies with your quoting method

.' '. <img src='http://www.colinch.com/fut/$row['county'].'' />
    ^--no start^--start                         ^--closed

You wanted to continue to build the string but did not include <img in the text. PHP then decided to continue again at 'http.... This is not correct.

4 Comments

Thanks for the replies! What do you think is the best way to style the image a bit more?
style the code for it or style how the browser will display it?
Yeah I mean how the browser displays it, so for example vertical-align for the image and things like that, should I write that in the php file or make a class with css?
that would be handled by the CSS class/style

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.