controller:
function search()
{
$this->load->model('membership_model');
$query = $this->membership_model->search();
var_dump($query->result());}
Model:
function search()
{
$match = $this->input->post('Search');
$this->db->like('title',$match);
$q = $this->db->get('feeds');
return $q;
}
i want to select from my database all rows where title contains $match but it returns me all of the rows. I have a input form where i insert the word. And in model i want to search the word in every title and to return only those titles that contain the word from my input form.
form:
echo anchor('site/search','Search')."<br/><br/>";
echo form_input('search','search');
I can't really understand the question but i think this code is what you need
Model:
function search($match){
$this->db->like('title',$match);
$q = $this->db->get('feeds');
return $q->result();
}
Controller:
function search_title()
{
$this->load->model('membership_model');
$search_value = $this->input->post('search');
$this->data['title'] = $this->membership_model->search($search_value );
}
View:
<table>
<?php foreach($title as $value) : ?>
<tr>
<td> <?=$value->nameofcolumn; ?> </tr> //change the word nameofcolumn to the name of the column in your database what column you want to fetch
</tr>
<?php endforeach; ?>
</table>
Related
Database Table Structure
Data need to show
Please note: The city_from and city_to not fixed. It comes from city table. But single_or_group_id is fixed 1 = Single and 2 = Group.
Should try this. Think it will work according to your condition
select a.Trans_rent as Single, b.Trans_rent as Group
from table as a
inner join table as b on a.city_from = b.city_from and a.city_to = b.city_to
and b.single_or_group_id = 2
where a.single_or_group_id = 1
You can see this. Hope it will help you.
<?php
$map_city = ['2'=>'Madina','3'=>'Makkah'];
$single_group = ['1'=>'Single','2'=>'Group'];
$data = [
[
'city_form' => 2,
'city_to' => 3,
'single_or_group_id' => 1,
'rent' => 1000
],
[
'city_form' => 2,
'city_to' => 3,
'single_or_group_id' => 2,
'rent' => 4000
],
];
?>
<table style="width:25%" border=1>
<?php $is_first = true; ?>
<?php foreach($data as $single){ ?>
<?php if($is_first) { ?>
<tr>
<th colspan=2><?php echo $map_city[$single['city_form']] .'-'. $map_city[$single['city_to']];?></th>
</tr>
<tr>
<?php foreach($data as $single){ ?>
<td><?php echo $single_group[$single['single_or_group_id']];?></td>
<?php } ?>
</tr>
<tr>
<?php foreach($data as $single){ ?>
<td><?php echo 'S.R '.$single['rent'];?></td>
<?php } ?>
</tr>
<?php $is_first = false; } ?>
<?php } ?>
</table>
this is my simple registration. I want to send an email to the user's after submitting button in this registration form.How can i do that??Thanx in advance.
1. In the view i have made an simple registration form as u can see i have added some basic information.
2.In my controller i have added validations. And save them in database.
4.And model has simple insert query.
form_view.php
<table>
<tr>
<td>Name</td><td><?php echo form_input($name);?></td>
<td><?php echo form_error('name');?></td>
</tr>
<tr>
<td>Email</td><td><?php echo form_input($email);?></td>
<td><?php echo form_error('email');?></td>
</tr>
<tr>
<td>Phone</td><td><?php echo form_input($phone);?></td>
<td><?php echo form_error('phone');?></td>
</tr>
<tr>
<td>Address</td><td><?php echo form_input($address);?></td>
<td><?php echo form_error('address');?></td>
</tr>
<tr>
<td>Division</td><td><?php echo form_dropdown('division',$division,'Please Select');?></td>
</tr>
<td></td><td><?php echo form_submit($submit);?></td>
</tr>
</table>
main.php
<?php
class main extends CI_Controller{
public function viewForm(){
$this->load->view('form_view');
}
public function insertData(){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Full Name', 'required|min_length[5]|max_length[12]|is_unique[address.name]');
$this->form_validation->set_rules('phone', 'Contact field', 'required|min_length[5]|max_length[12]|is_unique[address.phone]');
$this->form_validation->set_rules('address', 'Full Address', 'required|min_length[5]|max_length[12]|is_unique[address.address]');
$this->form_validation->set_rules('email', 'Email Address', 'required|min_length[5]|max_length[12]|is_unique[address.email]');
if($this->form_validation->run()==FALSE){
$this->load->view('form_view');
}else{
$sql="Select MAX(id) from address";// Auto Increment
$query= mysql_query($sql);
$selectid= mysql_fetch_array($query);
$finalid=$selectid[0]+1;
$data=array();
$data['id']=$finalid;
$data=array();
$data['name']=$this->input->post('std_name');
$data['phone']=$this->input->post('std_phone');
$data['address']=$this->input->post('std_address');
$data['division']=$this->input->post('division');
$data['email']=$this->input->post('email');
$this->load->model('main_model');
$this->main_model->save_user($data);
redirect('main/viewForm');
}
}
main_model.php
<?php
class main_model extends CI_Model{
public function save_user($data){
$this->db->insert('address',$data);
}
?>
}
?>
You might want to consider adding two columns on you user info table. Something like varchar activation_code & tinyint activated. On save user info generate activation code and send user a mail with that code within a link of your application url as well as save that code for that specific user with activated false value. On verify that code using that link change activated to true.
That will be my approach.
Before we start, I just want to point out that you should not have queries in your controller. That part must be in a model.
To send an email, you can use the codeigniter's library :
https://ellislab.com/codeigniter/user-guide/libraries/email.html
Here's how you can use it in your case :
if($this->form_validation->run()==FALSE)
{
$this->load->view('form_view');
}
else
{
$email = $this->input->post('email');
//Your stuff before the redirect
//If you have any particular smtp configuration (here, gmail)
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_user' => 'myemail#gmail.com',
'smtp_pass' => 'mypassword',
'smtp_port' => '465',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from('MyName#MySite.com', 'MyName');
$this->email->to($email);
$this->email->subject('Mail subject');
$msg = "Welcome to my website !";
$this->email->message($msg);
if($this->email->send())
{
//Stuff if mail succeded
}
else
{
//Stuff if mail failed
}
redirect('main/viewForm');
}
Hi i have one table in my database which has list of states and i want to fetch this data from the table but my query is not executing properly it gives me some error
<?php
require_once('../Config/database.php');
$result1=$this->Signup->query("SELECT * FROM states");
//echo $popular;
while($post = mysql_fetch_array($result1))
{ ?>
<table width="380">
<tr>
<td class="table_txt"><a class="thickbox tn" href="demo.php?state_name=<?php echo $post['state_name']?>&state_id=<?php echo $post['state_id']?>&height=430&height=430&width=700&inlineId=myOnPageContent"><?php echo $post['state_name']?></a></td>
</tr>
</table>
<?php }
?>
But it gives me error
Warning (512): Method SignupHelper::query does not exist [CORE\Cake\View\Helper.php, line 192
Warning (2): mysql_fetch_array() expects parameter 1 to be resource, null given
Please read the documentation first.
It seems you are trying to get the states, inside the View, with a query.
You need to separate the view from the model.
Create a State model.
Use something like this in your controller:
$this->loadModel('State');
$states = $this->State->find('list'); // this will create a key => value array with the IDs and names
$this->set('states', $states);
In your view, use
<table width="380">
<tr>
<?php foreach ($states as $stateId => $stateName) {
<td class="table_txt"><a class="thickbox tn" href="demo.php?state_name=<?php echo $stateName?>&state_id=<?php echo $stateId?>&height=430&height=430&width=700&inlineId=myOnPageContent"><?php echo $stateName ?>></a></td>
<?php } ?>
</tr>
You might still need some changes, but this is the main idea.
I am trying to create a page in my WordPress Admin Area that displays excerpts of posts and various custom field meta in a table-style layout.
If this were a front-end WordPress Template, I could do this very easily using a WordPress Loop and Query, however, I am not so sure how I would go about doing this on a page in the admin area.
Would it be the same, or would I need to use a completely new method? If so, could someone please provide a working example of how I would do this?
The admin page will be created using an included file within my functions.php - or at least that is the plan at the moment, so I just need help in figuring out how to pull the WordPress Excerpts and Post Meta.
you can use the WP_Query object everytime after WordPress is initialized, so if you like you can even make thousands of nested queries in den WordPress backend if you want to do this.
This is the way to go:
Create an action to add your backend page - write a Plugin or put it into your functions.php
Setup the Menu Page - the code is an example for a full backend administration Page of your Theme
Include your queries using the WP_Query object - optionally make database queries directly (http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query). Possibly use the "widefat" class of WordPress, for pretty formatting.
Make sure that your changes are saved correctly
add_action('admin_menu', 'cis_create_menu');
function cis_create_menu() {
//create new top-level menu
add_menu_page(__('Theme Settings Page',TEXTDOMAIN),__('Configure Theme',TEXTDOMAIN), 'administrator', __FILE__, 'cis_settings_page', '');
//call register settings function
add_action('admin_init','cis_register_settings');
}
function cis_register_settings() {
register_setting('cis-settings-group','cis_options_1','cis_validate_settings');
}
function cis_settings_page() {
// All Text field settings
$op_fields = array(
array(__('Label 1','textdomain'),"Description 1")
);
?>
<div class="wrap">
<h2><?php echo THEME_NAME; _e(": Settings",TEXTDOMAIN); ?></h2>
<?php
settings_errors();
?>
<form method="post" action="options.php">
<?php
settings_fields( 'cis-settings-group' );
$options = get_option('cis_options_1');
?>
<h3><?php _e('General','textdomain'); ?></h3>
<table class="widefat">
<thead>
<tr valign="top">
<th scope="row"><?php _e('Setting','ultrasimpleshop'); ?></th>
<th scope="row"><?php _e('Value','ultrasimpleshop'); ?></th>
<th scope="row"><?php _e('Description','ultrasimpleshop'); ?></th>
<th scope="row"><?php _e('ID','ultrasimpleshop'); ?></th>
</tr>
</thead>
<tbody>
<?php
// the text-settings we define fast display
$i=1;
foreach($op_fields as $op) {?>
<tr valign="top">
<td><label for="cis_oset_<?php echo $i; ?>"><?php echo $op[0]; ?></label></td>
<td><input size="100" id="cis_oset_<?php echo $i; ?>" name="cis_options_1[cis_oset_<?php echo $i; ?>]" type="text" value="<?php echo esc_attr($options['cis_oset_'.$i]);?>" /></td>
<td class="description"><?php echo $op[1]; ?></td>
<td class="description"><?php echo $i; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes',TEXTDOMAIN) ?>" />
</p>
</form>
</div>
<?php }
// Validate the user input - if nothing to validate, just return
function cis_validate_settings( $input ) {
$valid = array();
$i= 1;
while(isset($input['cis_oset_'.$i])) {
$valid['cis_oset_'.$i] = $input['cis_oset_'.$i];
$i++;
}
$cis_additional_settings = get_option('cis_options_1');
foreach($input as $ikey => $ivalue) {
if($ivalue != $valid[$ikey]) {
add_settings_error(
$ikey, // setting title
"cis_oset_".$ikey, // error ID
str_replace("%s",$ikey,__('Invalid Setting in Settings Area ("%s"). The value was not changed.',TEXTDOMAIN)), // error message
'error' // type of message
);
$valid[$ikey] = $cis_additional_settings[$ikey];
}
}
return $valid;
}
outside the loop you would need to use
$post->post_excerpt
or try this
function get_the_excerpt_here($post_id)
{
global $wpdb;
$query = "SELECT post_excerpt FROM $wpdb->posts WHERE ID = $post_id LIMIT 1";
$result = $wpdb->get_results($query, ARRAY_A);
return $result[0]['post_excerpt'];
}
Actually I have a CGI form which consists of textfields and I need a combobox in which I can enter my own data dynamically. May be it seems very silly question but I am new to cgi-perl as well as HTML so no idea what to do. Here is my form:
#!C:\perl\bin\perl.exe
use CGI;
use CGI qw/:standard/;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my $q = new CGI;
use DBI;
use CGI qw(:all);
use strict;
use warnings;
print "Content-Type: text/html\n\n";
print $q->header ( );
if ( $q->param("submit") )
{
process_form ( );
}
else
{
display_form ( );
}
sub process_form
{
if ( validate_form ( ) )
{
display_form ( );
}
}
sub validate_form
{
my $User_Name = $q->param("User_Name");
my $User_Password= $q->param("User_Password");
my $User_Permission = $q->param("User_Permission");
my $User_Department= join(", ",$q->param("User_Department"));
my $error_message = "";
$error_message .= "Please enter your name<br/>" if( !$User_Name );
$error_message .= "Please enter your Password<br/>" if( ! $User_Password );
$error_message .= "Please Select a permission<br/>" if( !$User_Permission );
$error_message .= "Please select atleast 1 department<br/>" if(!$User_Department);
if ( $error_message )
{
display_form (
$error_message,$User_Name,$User_Password,$User_Permission,$User_Department);
return 0;
}
else
{
my $dbh = DBI->connect("dbi:SQLite:DEVICE.db","", "",{RaiseError => 1, AutoCommit =>
1 } );
my $sql = "SELECT COUNT(UserName) FROM UsersList WHERE UserName='$User_Name'";
my $sth = $dbh->prepare($sql) or die("\n\nPREPARE ERROR:\n\n$DBI::errstr");
$sth->execute or die("\n\nQUERY ERROR:\n\n$DBI::errstr");
my ($n) = $dbh->selectrow_array($sth);
$sth->finish();
if ($n > 0) {
print "Record Already Exists";
}
else {
my $sql = "INSERT INTO UsersList (UserName,Password,Permission,Department) VALUES
('$User_Name ',' $User_Password','$User_Permission','$User_Department')";
my $sth = $dbh->prepare($sql);
$sth->execute;
print "Record Added Successfully";
$sth->finish();
$dbh->commit or die $dbh->errstr;
}
$dbh->disconnect;
}
}
sub display_form
{
my $error_message = shift;
my $User_Name = shift;
my $User_Password = shift;
my $User_Permission= shift;
my $User_Department= shift;
my $User_Permission_Add_sel = $User_Permission eq "Add" ? " checked" : "";
my $User_Permission_Edit_sel =$User_Permission eq "Edit" ? " checked" : "";
my $User_Permission_Delete_sel =$User_Permission eq "Delete" ? " checked" : "";
my $User_Permission_View_sel =$User_Permission eq "View" ? " checked" : "";
my $User_Department_html = "";
my $dbh = DBI->connect("dbi:SQLite:DEVICE.db","", "",{RaiseError => 1, AutoCommit =>
1 } );
my $sql = "select DepartmentName from Departments order by DepartmentName";
my $sth = $dbh->prepare($sql);
$sth->execute() ;
while (my $User_Department_option= $sth->fetchrow_array)
{
$User_Department_html.= "<option value=\"$User_Department_option\"";
$User_Department_html.= " selected" if ( $User_Department_option eq
$User_Department );
$User_Department_html.= ">$User_Department_option</option>";
}
$sth->finish();
$dbh->commit or die $dbh->errstr;
print <<END_HTML;
<html>
<head><title>Form Validation</title></head>
<body>
<form action="AddUser.cgi" method="post">
<input type="hidden" name="submit" value="Submit">
<p>$error_message</p>
<TABLE BORDER="1" align="center">
<TR>
<TD>Name</TD>
<TD> <input type="text" name="User_Name" value="$User_Name"></TD>
</TR>
<TR>
<TD>Password</TD>
<TD colspan="2"><input type="password" name="User_Password" value="$User_Password"
size="20" maxlength="15" /></TD>
</TR>
<TR>
<TD>Role</TD>
<TD>"HERE I NEED A COMBOBOX"</TD>
</TR>
<TR>
<TD>Permission</TD>
<TD><input type="radio" name="User_Permission"
value="Add"$User_Permission_Add_sel>Add<input type="radio" name="User_Permission"
value="Edit"$User_Permission_Edit_sel>Edit<input type="radio"
name="User_Permission" value="Delete"$User_Permission_Delete_sel>Delete<input
type="radio" name="User_Permission" value="View"$User_Permission_View_sel>View</TD>
</TR>
<TR>
<TD>Department</TD>
<TD colspan="2"> <select name="User_Department" MULTIPLE
SIZE=4>$User_Department_html</select></TD>
</TR>
</TR>
<TR>
<TD align="center" colspan="2">
<input type="submit" name="submit" value="ADD">
</TD>
</TR>
</TABLE
</form>
</body></html>
END_HTML
}
What you're looking for here isn't done on the Perl side, but on the HTML+Javascript side. As noted by others, HTML does not have a built-in combo box form element. So, you're stuck with Javascript.
Personally, I like using JQuery whenever working with Javascript. It's a Javascript library which makes manipulating web pages elements much easier.
Specific to your question, you'll want to look at http://jqueryui.com/demos/autocomplete/ (there is an actual combobox demo linked on the right, if you really, really need a combobox instead of a Google-style autocomplete text field.
Not related to the combobox, but you might also want to look at Template::Toolkit - a templating system for Perl (and others) that will allow you to take the HTML out of your perl scripts. Believe me, having the HTML embedded in CGI scripts for anything beyond the most basic usages will turn into a nightmare soon enough.
In place of "HERE I NEED A COMBOBOX" you have to write :
<select name='User_Department' id='User_Department'>
$User_Department_html
</select>
However, you retrieve parameters within your sub display_form but you've never passed any.