How to add percent symbol to string value output from SQL query

This is probably simpler than I think, but it is very difficult for me to envelop my head. I'm trying to learn how to code after a 10-year hiatus, so hacks and codecademy are my teachers. Anyway,...

What I'm trying to do is just make a percentage from sql number.

I have a form that, when submitted, enters an integer sql, which is used to output the css value. I'm just trying to change the width value from an integer to a percentage to allow some semblance for a sensitive effect.

Here's the php preliminary output:

<video id="vp2_html5_rightSidePlaylist_'.$row["id"].'" width="'.$row["playerWidth"].'%" height="'.$row["playerHeight"].'" '.$preload_aux.'><div class="xplaylist">'.$playlist_str.'</div></video> 

The output is as follows:

  <video id="vp2_html5_rightSidePlaylist_1" width="100" height="297" preload="auto" src="http://localhost:888/test.mp4"> 

Thanks in advance for your help!

+4
source share
3 answers

If you are using mysql, you can use the CONCAT() function to add a character to the end of a specific field.

CONCAT(str1,str2,...) - returns the string that occurs when the arguments are combined.

For instance -

 SELECT `id`,`title`,CONCAT(`percentage`,'%') as percentage FROM `items` LIMIT 100 

Now, when any value has been stored in the percentage field, the % symbol will be added to it in the result set.

+2
source

I think this is not your coding problem.

It can be assumed that you received this:

 <video id="vp2_html5_rightSidePlaylist_1" width="100" height="297" preload="auto" src="http://localhost:888/test.mp4"> 

.. using a tool like firebug / tools-tools?

When yes, this is not the original source, invalid parts were cleaned up.

One of them is a percent sign, it is not valid inside the width attribute. To get the desired result, use css instead:

 'style="width:'.$row["playerWidth"].'%;height:'.$row["playerHeight"].'px;"' 
+2
source

Use this:

 Select CONCAT(`playerWidth`,'%') AS playerWidth, id .....etc FROM tableName 
0
source

All Articles