简体   繁体   English

使用SQL从数据源生成html标签

[英]Generating an html tag from a datasource using SQL

We're doing an SQL assignment, using a web form to display information and then add values from the form. 我们正在执行SQL分配,使用Web表单显示信息,然后从表单中添加值。

Part of the assignment requires us to use a radio button list and link it to a database table, which ive done. 部分作业要求我们使用单选按钮列表并将其链接到数据库表,此操作已完成。 It also wants us to generate a photo relating to the text of each radio button. 它还希望我们生成与每个单选按钮的文本有关的照片。 For example: 例如:

  • Apple (picture of apple) 苹果(苹果的图片)
  • Banana (picture of banana) 香蕉(香蕉的图片)

But it all has to come from the database itself. 但这一切都必须来自数据库本身。

We were given the hint to bind the datasource to the database table and then in the SQL SELECT statement, use a html tag to insert the image. 我们得到了将数据源绑定到数据库表的提示,然后在SQL SELECT语句中,使用html标记插入图像。

For example the database has 3 columns: Fruit_Code, Fruit_Name and Fruit_Image. 例如,数据库具有3列:Fruit_Code,Fruit_Name和Fruit_Image。 In the Fruit_Image column we store the name of the fruit image (eg apple.jpg). 在Fruit_Image列中,我们存储水果图像的名称(例如apple.jpg)。

Then from there when we use the SELECT statement, it goes: 然后从那里开始,当我们使用SELECT语句时:

SELECT Fruit_Name, Fruit_Name + ' ' + '<img src = "+'/Images/' + Fruit_Image" + />'
FROM Fruit_Table

However whenever i try to do this i cant seem to get it to work. 但是,每当我尝试执行此操作时,我似乎都无法正常工作。 The image has to come from the database in some way and has to be at the end of the radio button text. 图像必须以某种方式来自数据库,并且必须位于单选按钮文本的末尾。

Could someone please point me in the right direction? 有人能指出我正确的方向吗?

You just have the string escaping slightly off on your SELECT statement: 您只需要在SELECT语句上略微转义字符串即可:

SELECT Fruit_Name
     , Fruit_Name + ' <img src=''/Images/' + Fruit_Image + '''/>'
FROM Fruit_Table

Added the extra escaping for the path as pointed out by @Goat_CO 添加了@Goat_CO指出的路径的额外转义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM