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

  • you should use a good text editor that color codes for you depending on the language. NotePad++ is a good free one. – UnholyRanger Feb 28 '13 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 –  Feb 28 '13 at 13:49
  • also for your case, check out [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – UnholyRanger Feb 28 '13 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 ? –  Feb 28 '13 at 14:22

5 Answers5

2

You Quotes are totally wrong:

    'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] .'<img src=\'http://www.colinch.com/fut/'.$row['county'].'\' />',
Tim
  • 9,351
  • 1
  • 32
  • 48
0

Try this :

$data[] = array(
        'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] .' <img src="http://www.colinch.com/fut/'.$row['county'].'" />'  ,
        'value' => $row['zip']
    );
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
0

Try this

  $data[] = array(
        'label' => $row['zip'] .', '. $row['city'] .' '. $row['state'] .' '. '<img src="http://www.colinch.com/fut/'.$row['county'].'"'.'/>'  ,
        'value' => $row['zip']
internals-in
  • 4,798
  • 2
  • 21
  • 38
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 :)

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
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.

UnholyRanger
  • 1,977
  • 14
  • 24