Show Image Based On Integer Value

Hi guys.

i’m creating a kind of blog system, in which the poster will have to choose what “type” his post is. There is about 7-10 different types of posts (I haven’t figured them all out yet) and the poster chooses what type it is in a drop-down list.

In the table called “tbl_posts” i’m having a column called “type”. There is a relation between this column and the table called “tbl_type” so that the type the poster chooses in the drop-down list has the integer value that matches the id of the type in the “tbl_type” table.

The problem i’m having is how to figure out how to make the application so that a certain picture (that is specified to each type) is shown besides the post. Can anybody help me with this?

Oh, and the images is stored ind the images folder

In my app (Bugitor) I use image sprites, and uses a helper class (static class) to display images:


	public static function namedImage($name, $big = false) {

		$style = 'icons-' . $name;

    	if($big) {

        	return CHtml::tag('span', array('title' => $name,'class' => "{$name}-icon"));

    	} else {

        	return CHtml::tag('span', array('title' => $name,'class' => "icons-{$name}"));

    	}

	}



I use CSS because it’s better IMO than hardcoding it in the views.

I would store the path of the image file in one of the fields of the ‘tbl_type’ table.

Then for example in my view I would use:




<?php echo "<img src = '".$data->imagefile.".jpg' width=240 height=180>"; ?>



where ‘imagefile’ is the required fieldname in ‘tbl_type’. I am assuming here that the image is a jpg.

I recommend using cascading style sheets - it’s easier to change image properties (name, location), and overall style.

But, if you must, use CHtml::image.

i’m not sure of what you mean by using css instead of CHtml::image? I’m fairly new at yii so could you please explain at an easy level please? :)

Like:


#header {

	background: #000000 url(../../images/blue/header.png);

	width: 100%;/*960px;*/

	height: 120px;

	margin: 0px 0px 0px 0px;

	padding: 0px;

	overflow: hidden;

}



In a css file.

Instead of using CHtml::image, use CHtml::tag and assign an id/class to a span - like in the previous example I posted here.

You can then handle the image path and style in the style sheet instead of doing it in code.

Especially good for elements you reuse a lot.

ahh, thanks :)