thank you very much for this awesome framework! i allready did buy the book last week from amazon to learn from you and to support you…
the ar thing is a little bit kind of magic for me but i’ll get used to it…
anyway i want to show an image from mysql-db.
here is the standalone example:
<?php
$username = "root";
$password = "root";
$host = "localhost";
$database = "viralsnack";
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$id = $_GET['id'];
if(!isset($id) || empty($id)){
die("Please select your image!");
}else{
$query = mysql_query("SELECT * FROM tbl_post WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$img = $row['img'];
header('Content-type: image/gif');
echo $img;
}
?>
or simple:
header('Content-type: image/gif');
echo $img;
But how with Yii!?
I know for shure it has to do with the header line above, but i am trying and trying hard to do this with the blog demo example and i just can’t. I have putted it already in every method but still no improvements.
here is one of the many variations did try with yii:
// in protected/views/post/_view.php in some div class
// yes i did try with ".gif" after it too
// yes i did try with print too
// yes i did try with setting another variable
// yes i did try with another css
// yes i did try within the widget and without
// yes i did try on a single page like the about page
// yes i did try put header('Content-type: image/gif'); on every single method of the controller all together and one by one
// yes i am getting crazy
echo '<img src ="'.$data->img.'">';
i already found some posts about header stuff but it was too generic or to simple, anyway i don’t get it. i google and searched alot, but nothing helped.
and no, i would dont want any extensions. i would to hold this very simple.
I’m not sure I really understood what do you try to do. You should create a new action which should display your image content, and then in src attribute of your image tag you should create link on this action.
// In your PostController
public function actionShowimage()
{
$image = Post::model()->findByPk($_GET['id']);
header('Content-type: image/gif');
echo $image->img;
exit();
}
// In view
<?php echo CHtml::image($this->createUrl('post/showimage', array('id' => $imageId))); ?>
actionShowimage method takes the image directly from DB as you want:
$image = Post::model()->findByPk($_GET['id']);
I can’t understand why it doesn’t working. Try to run /yiiblog/index.php/post/showimage?id=2 separately ( not in src attribute ), what is it returning?