Reputation: 407
recently i created a student profile data entry system in joomla module. this input form can collect data about student and store into my custom table. but here is something missing. the missing is student image. need to add a image insert field . here is my full code.
<?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;
}
}
}
?>
if here is a image input field then it will be good, but i have no idea about image form, i find about this topic tutorial in google, but not got any information about that, for that i ask here. pls someone help me. i want to create a total data entry module.
Upvotes: 0
Views: 1509
Reputation: 19733
You will first need to create the image input field to the user can add one like so:
<p><input type="file" id="image" name="image" /></p>
Then you will need to get the image that has been added and upload it like so:
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;
}
Then to add it to the database, you should simply upload the path + image name like so:
$data->image = $image;
Update:
To display the image, you can simply use the following inside your foreach
loop:
echo '<img src="' . $row->image . '" />';
Upvotes: 3