joomla 2.5模块中的图像插入字段

时间:2013-12-01 15:40:59

标签: php mysql image joomla module

最近我在joomla模块中创建了一个学生档案数据输入系统。此输入表单可以收集有关学生的数据并存储到我的自定义表中。但这里缺少一些东西。缺少的是学生形象。需要添加图像插入字段。这是我的完整代码。

<?php

class modHelloWorldHelper
{

public static function getHello( $params )
{ 
/** post form to db module **/
$input = new JInput;
$post = $input->getArray($_POST);
$name = $post["name"];
$father_name = $post["father_name"];
$mother_name = $post["mother_name"];
$roll = $post["roll"];
$class = $post["class"];
$subject = $post["subject"];
$phone = $post["phone"];
$address = $post["address"];
$email_id = $post["email_id"];
//--END POST YOUR FORM DATA---|

//--build the form------------>
?>
<form name="names" id="names" action="<?php echo JURI::current(); ?>" method="post">
<p><input type="text" name="name" id="name" value="" /></p>
<p><input type="text" name="father_name" id="father_name" value="" /></p>
<p><input type="text" name="mother_name" id="mother_name" value="" /></p>
<p><input type="text" name="roll" id="roll" value="" /></p>
<p><input type="text" name="class" id="class" value="" /></p>
<p><input type="text" name="subject" id="subject" value="" /></p>
<p><input type="text" name="phone" id="phone" value="" /></p>
<p><input type="text" name="address" id="address" value="" /></p>
<p><input type="text" name="email_id" id="email_id" value="" /></p>
<p><input id="submit" name="submit" type="submit" value="Submit Names" /></p>
</form>
<!--END BUILD THE FORM-->
<?

//first name or last name set, continue-->
$data =new stdClass();
$data->id = NULL;
$data->name = $name;
$data->father_name = $father_name;
$data->mother_name = $mother_name;
$data->roll = $roll;
$data->class= $class;
$data->subject = $subject;
$data->address = $address;
$data->email_id = $email_id;

$db = JFactory::getDBO();
$db->insertObject('#__my_student', $data, id);

/*
$db = JFactory::getDBO();
$query = "CREATE TABLE IF NOT EXISTS `#__my_student`  ( 
`id` int(10)NOT NULL AUTO_INCREMENT, 
`name` varchar(50) NOT NULL,
`father_name` varchar(40) NOT NULL,
`mother_name` varchar(40) NOT NULL,
`roll` int(4) NOT NULL,
`class` int(2) NOT NULL,
`subject` varchar(40) NOT NULL,
`phone` int(14) NOT NULL,
`address` varchar(40) NOT NULL,
`email_id` varchar(40) NOT NULL, 
PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$db->setQuery($query);
$result = $db->query();
$db->query();
*/

}

public static function getdb($params)
{
// Get a database object
$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('*');
$query->from('#__helloworld');

// sets up a database query for later execution
$db->setQuery($query);

// fetch result as an object list
$result = $db->loadObjectList();
foreach ($result as $row){
echo $row->name;
}
}
}
?>

如果这是一个图像输入字段,那么它会很好,但我不知道图像形式,我在谷歌找到这个主题教程,但没有得到任何有关的信息,因为我在这里问。请别人帮助我。我想创建一个总数据输入模块。

1 个答案:

答案 0 :(得分:3)

首先需要创建图像输入字段,用户可以像这样添加一个:

<p><input type="file" id="image" name="image" /></p>

然后您需要获取已添加的图像并将其上传为:

jimport('joomla.filesystem.file');

$fileInput = new JInput($_FILES);
$file = $fileInput->get('image', null, 'array');

if(isset($file) && !empty($file['name'])) { 
    $filename = JFile::makeSafe($file['name']);
    $src = $file['tmp_name'];
    $data['image']=$filename;

    $dest = JPATH_SITE . '/modules/mod_helloworld/images/' . $filename;

    JFile::upload($src, $dest);

    $image = JUri::root() . 'modules/mod_helloworld/images/' . $filename;

}

然后将其添加到数据库中,您只需上传路径+图像名称,如下所示:

$data->image = $image;

<强>更新

要显示图像,只需在foreach循环中使用以下内容:

echo '<img src="' . $row->image . '" />';